Skip to content

Commit

Permalink
Merge pull request #1593 from laboro/fix/BAP-3825
Browse files Browse the repository at this point in the history
BAP-3825: Fix failed unit tests
  • Loading branch information
dxops committed Mar 31, 2014
2 parents 7a6bb3f + 17e6983 commit 1e67901
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,36 @@ public function testThisQuarterModify()
$expression = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_QUARTER));
$result = $expression->getValue();

$curMonth = date('m');
$curQuarter = (int)ceil($curMonth / 3);

$this->assertSame($curQuarter, (int)$result->quarter);
$expectedQuarter = (int)ceil(date('m') / 3);
$this->assertSame($expectedQuarter, (int)$result->quarter);

$expression->add(new ExpressionResult(1));
$expected = (int)ceil((\DateTime::createFromFormat('U', strtotime('today +3 month'))->format('m')) / 3);
$this->assertSame($expected, (int)$result->quarter);
$expectedQuarter += 1;
$this->assertSame($expectedQuarter, (int)$result->quarter);

$expression->subtract(new ExpressionResult(3));
$expected = (int)ceil((\DateTime::createFromFormat('U', strtotime('today -6 month'))->format('m')) / 3);
$this->assertSame($expected, (int)$result->quarter);
$expectedQuarter -= 3;
if ($expectedQuarter < 0) {
$expectedQuarter += 4;
}
$this->assertSame($expectedQuarter, (int)$result->quarter);
}

public function testThisMonthModify()
{
$expression = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_MONTH));
$result = $expression->getValue();

$curMonth = (int)date('m');
$this->assertSame($curMonth, (int)$result->month);
$expectedMonth = (int)date('m');
$this->assertSame($expectedMonth, (int)$result->month);

$expression->add(new ExpressionResult(3));
$expected = (int)date('m', strtotime('today +3 month'));
$this->assertSame($expected, (int)$result->month);
$expectedMonth += 3;
$this->assertSame($expectedMonth, (int)$result->month);

$expression->subtract(new ExpressionResult(2));
$expected = (int)date('m', strtotime('today +1 month'));
$this->assertSame($expected, (int)$result->month);
$expectedMonth -= 2;
$this->assertSame($expectedMonth, (int)$result->month);
}

public function testThisYearModify()
Expand Down Expand Up @@ -207,49 +208,74 @@ public function testReverseAddition()
$this->assertSame((int)$expectedResult, (int)$result->day);
}

public function testReverseSubtraction()
public function testReverseSubtractionDay()
{
$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));

$expression = new ExpressionResult(33);
$expressionModify = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_DAY));
$expression->subtract($expressionModify);

$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
$result = $expression->getValue();
$expectedDay = 33 - $dateTime->format('d');
$this->assertSame($expectedDay, (int)$result);
}

$result = $expression->getValue();
$this->assertSame(33 - (int)$dateTime->format('d'), (int)$result);
public function testReverseSubtractionMonth()
{
$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));

$expressionModify = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_YEAR));
$expression = new ExpressionResult(5000);
$expression = new ExpressionResult(12);
$expressionModify = new ExpressionResult(
new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_MONTH)
);
$expression->subtract($expressionModify);

$year = date('Y');
$result = $expression->getValue();
$this->assertSame(5000 - (int)$year, (int)$result);
$result = $expression->getValue();
$expectedMonth = 12 - (int)$dateTime->format('m');
$this->assertSame($expectedMonth, (int)$result);
}

$expressionModify
= new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_MONTH));
$expression = new ExpressionResult(12);
public function testReverseSubtractionYear()
{
$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));

$expression = new ExpressionResult(5000);
$expressionModify = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_YEAR));
$expression->subtract($expressionModify);

$month = date('m');
$result = $expression->getValue();
$this->assertSame(12 - (int)$month, (int)$result);
$result = $expression->getValue();
$expectedMonth = 5000 - (int)$dateTime->format('Y');
$this->assertSame($expectedMonth, (int)$result);
}

public function testReverseSubtractionQuarter()
{
$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));

$expressionModify
= new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_QUARTER));
$expression = new ExpressionResult(4);
$expression = new ExpressionResult(4);
$expressionModify = new ExpressionResult(
new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_QUARTER)
);
$expression->subtract($expressionModify);

$curMonth = date('m');
$curQuarter = (int)ceil($curMonth / 3);
$result = $expression->getValue();
$this->assertSame(4 - (int)$curQuarter, (int)$result);
$result = $expression->getValue();
$expectedMonth = 4 - (int)ceil((int)$dateTime->format('m')/3);
$this->assertSame($expectedMonth, (int)$result);
}

public function testReverseSubtractionWeek()
{
$dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
// Needed because Oro\Bundle\FilterBundle\Expression\Date\ExpressionResult changes first day of week
$dateTime->modify('this week');

$expressionModify = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_WEEK));
$expression = new ExpressionResult(200);
$expressionModify = new ExpressionResult(new Token(Token::TYPE_VARIABLE, DateModifierInterface::VAR_THIS_WEEK));
$expression->subtract($expressionModify);
$expectedResult = date('W');
$result = $expression->getValue();
$this->assertSame(200 - (int)$expectedResult, (int)$result);

$result = $expression->getValue();
$expectedWeek = 200 - (int)$dateTime->format('W');
$this->assertSame($expectedWeek, (int)$result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function testPreSubmit(array $data, array $expectedData, $valueSubforms =
*/
public function dataProvider()
{
$weekDateTime = new \DateTime('now', new \DateTimeZone('UTC'));
$weekDateTime->modify('this week');
// Needed because Oro\Bundle\FilterBundle\Expression\Date\ExpressionResult changes first day of week
$weekNumber = $weekDateTime->format('W');

return [
'should process date value' => [
['part' => DateModifierInterface::PART_VALUE, 'value' => ['start' => '2001-01-01']],
Expand All @@ -94,7 +99,10 @@ public function dataProvider()
'part' => DateModifierInterface::PART_WEEK,
'value' => ['start' => 3, 'end' => sprintf('{{%d}}', DateModifierInterface::VAR_THIS_WEEK)]
],
['part' => DateModifierInterface::PART_WEEK, 'value' => ['start' => 3, 'end' => date('W')]],
[
'part' => DateModifierInterface::PART_WEEK,
'value' => ['start' => 3, 'end' => $weekNumber]
],
['start' => 'start subform', 'end' => 'end subform'],
['start' => null, 'end' => null]
],
Expand Down

0 comments on commit 1e67901

Please sign in to comment.