Passed
Pull Request — master (#3108)
by Sergei
15:56
created

DateExpressionTest::dateDiffProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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
     * @test
14
     * @dataProvider dateDiffProvider
15
     */
16
    public function diff(string $date1, string $date2, int $expected)
17
    {
18
        $table = new Table('date_expr_test');
19
        $table->addColumn('date1', 'date');
20
        $table->addColumn('date2', 'date');
21
        $this->_conn->getSchemaManager()->dropAndCreateTable($table);
22
        $this->_conn->insert('date_expr_test', [
23
            'date1' => $date1,
24
            'date2' => $date2,
25
        ]);
26
27
        $platform = $this->_conn->getDatabasePlatform();
28
29
        $sql  = sprintf('SELECT %s FROM date_expr_test', $platform->getDateDiffExpression('date1', 'date2'));
30
        $diff = $this->_conn->query($sql)->fetchColumn();
31
32
        self::assertEquals($expected, $diff);
33
    }
34
35
    public static function dateDiffProvider()
36
    {
37
        $date1    = new DateTimeImmutable();
38
        $date2    = new DateTimeImmutable('2018-04-10 10:10:10');
39
        $expected = $date1->modify('midnight')->diff(
40
            $date2->modify('midnight')
41
        )->days;
42
43
        return [
44
            'dynamic'  => [
45
                $date1->format('Y-m-d H:i:s'),
46
                $date2->format('Y-m-d H:i:s'),
47
                $expected,
48
            ],
49
            'same day' => ['2018-04-14 23:59:59', '2018-04-14 00:00:00', 0],
50
            'midnight' => ['2018-04-14 00:00:00', '2018-04-13 23:59:59', 1],
51
        ];
52
    }
53
}
54