1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Platform; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
7
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
8
|
|
|
use function sprintf; |
9
|
|
|
|
10
|
|
|
class DateExpressionTest extends DbalFunctionalTestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider differenceProvider |
14
|
|
|
*/ |
15
|
|
|
public function testDifference(string $date1, string $date2, int $expected) : void |
16
|
|
|
{ |
17
|
|
|
$table = new Table('date_expr_test'); |
18
|
|
|
$table->addColumn('date1', 'date'); |
19
|
|
|
$table->addColumn('date2', 'date'); |
20
|
|
|
$this->_conn->getSchemaManager()->dropAndCreateTable($table); |
21
|
|
|
$this->_conn->insert('date_expr_test', [ |
22
|
|
|
'date1' => $date1, |
23
|
|
|
'date2' => $date2, |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
$platform = $this->_conn->getDatabasePlatform(); |
27
|
|
|
|
28
|
|
|
$sql = sprintf('SELECT %s FROM date_expr_test', $platform->getDateDiffExpression('date1', 'date2')); |
29
|
|
|
$diff = $this->_conn->query($sql)->fetchColumn(); |
30
|
|
|
|
31
|
|
|
self::assertEquals($expected, $diff); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string[][]|int[][] |
36
|
|
|
*/ |
37
|
|
|
public static function differenceProvider() : iterable |
38
|
|
|
{ |
39
|
|
|
$date1 = new DateTimeImmutable(); |
40
|
|
|
$date2 = new DateTimeImmutable('2018-04-10 10:10:10'); |
41
|
|
|
$expected = $date1->modify('midnight')->diff( |
42
|
|
|
$date2->modify('midnight') |
43
|
|
|
)->days; |
44
|
|
|
|
45
|
|
|
return [ |
46
|
|
|
'dynamic' => [ |
47
|
|
|
$date1->format('Y-m-d H:i:s'), |
48
|
|
|
$date2->format('Y-m-d H:i:s'), |
49
|
|
|
$expected, |
50
|
|
|
], |
51
|
|
|
'same day' => ['2018-04-14 23:59:59', '2018-04-14 00:00:00', 0], |
52
|
|
|
'midnight' => ['2018-04-14 00:00:00', '2018-04-13 23:59:59', 1], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|