Skip to content

Commit

Permalink
Merge pull request doctrine#7055 from lcobucci/fix-date-issues-once-a…
Browse files Browse the repository at this point in the history
…nd-for-all

Fix date issues once and for all
  • Loading branch information
lcobucci authored Feb 9, 2018
2 parents 4509c77 + 9753421 commit 06ffd85
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\AbstractQuery;
use Doctrine\Tests\Models\Company\CompanyManager;
use Doctrine\Tests\OrmFunctionalTestCase;
use function sprintf;

/**
* Functional Query tests.
Expand Down Expand Up @@ -294,7 +295,7 @@ public function testDateDiff()
*
* @dataProvider dateAddSubProvider
*/
public function testDateAdd(string $unit, int $amount, int $expectedValue, int $delta = 0) : void
public function testDateAdd(string $unit, int $amount, int $delta = 0) : void
{
$query = sprintf(
'SELECT CURRENT_TIMESTAMP() as now, DATE_ADD(CURRENT_TIMESTAMP(), %d, \'%s\') AS add FROM %s m',
Expand All @@ -310,9 +311,12 @@ public function testDateAdd(string $unit, int $amount, int $expectedValue, int $
self::assertArrayHasKey('now', $result);
self::assertArrayHasKey('add', $result);

$diff = strtotime($result['add']) - strtotime($result['now']);

self::assertEquals($expectedValue, $diff, '', $delta);
self::assertEquals(
(new \DateTimeImmutable($result['now']))->modify(sprintf('+%d %s', $amount, $unit)),
new \DateTimeImmutable($result['add']),
'',
$delta
);
}

/**
Expand All @@ -321,7 +325,7 @@ public function testDateAdd(string $unit, int $amount, int $expectedValue, int $
*
* @dataProvider dateAddSubProvider
*/
public function testDateSub(string $unit, int $amount, int $expectedValue, int $delta = 0) : void
public function testDateSub(string $unit, int $amount, int $delta = 0) : void
{
$query = sprintf(
'SELECT CURRENT_TIMESTAMP() as now, DATE_SUB(CURRENT_TIMESTAMP(), %d, \'%s\') AS sub FROM %s m',
Expand All @@ -337,19 +341,23 @@ public function testDateSub(string $unit, int $amount, int $expectedValue, int $
self::assertArrayHasKey('now', $result);
self::assertArrayHasKey('sub', $result);

$diff = strtotime($result['now']) - strtotime($result['sub']);

self::assertEquals($expectedValue, $diff, '', $delta);
self::assertEquals(
(new \DateTimeImmutable($result['now']))->modify(sprintf('-%d %s', $amount, $unit)),
new \DateTimeImmutable($result['sub']),
'',
$delta
);
}

public function dateAddSubProvider() : array
{
$secondsInDay = 86400;

return [
'year' => ['year', 1, 365 * $secondsInDay, 3 * $secondsInDay],
'month' => ['month', 1, 30 * $secondsInDay, 2.5 * $secondsInDay],
'week' => ['week', 1, 7 * $secondsInDay, $secondsInDay],
'year' => ['year', 1, $secondsInDay],
'month' => ['month', 1, $secondsInDay],
'week' => ['week', 1, $secondsInDay],
'day' => ['day', 2, $secondsInDay],
'hour' => ['hour', 1, 3600],
'minute' => ['minute', 1, 60],
'second' => ['second', 10, 10],
Expand Down

0 comments on commit 06ffd85

Please sign in to comment.