Completed
Push — master ( 9e086f...04cb1b )
by Sergei
21s queued 12s
created

DefaultExpressionTest::testCurrentDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Platform;
6
7
use Doctrine\DBAL\FetchMode;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Platforms\MySqlPlatform;
10
use Doctrine\DBAL\Platforms\OraclePlatform;
11
use Doctrine\DBAL\Schema\Table;
12
use Doctrine\DBAL\Types\Types;
13
use Doctrine\Tests\DbalFunctionalTestCase;
14
use function sprintf;
15
16
class DefaultExpressionTest extends DbalFunctionalTestCase
17
{
18
    public function testCurrentDate() : void
19
    {
20
        $platform = $this->connection->getDatabasePlatform();
21
22
        if ($platform instanceof MySqlPlatform) {
23
            self::markTestSkipped('Not supported on MySQL');
24
        }
25
26
        $this->assertDefaultExpression(Types::DATE_MUTABLE, static function (AbstractPlatform $platform) : string {
27
            return $platform->getCurrentDateSQL();
28
        });
29
    }
30
31
    public function testCurrentTime() : void
32
    {
33
        $platform = $this->connection->getDatabasePlatform();
34
35
        if ($platform instanceof MySqlPlatform) {
36
            self::markTestSkipped('Not supported on MySQL');
37
        }
38
39
        if ($platform instanceof OraclePlatform) {
40
            self::markTestSkipped('Not supported on Oracle');
41
        }
42
43
        $this->assertDefaultExpression(Types::TIME_MUTABLE, static function (AbstractPlatform $platform) : string {
44
            return $platform->getCurrentTimeSQL();
45
        });
46
    }
47
48
    public function testCurrentTimestamp() : void
49
    {
50
        $this->assertDefaultExpression(Types::DATETIME_MUTABLE, static function (AbstractPlatform $platform) : string {
51
            return $platform->getCurrentTimestampSQL();
52
        });
53
    }
54
55
    private function assertDefaultExpression(string $type, callable $expression) : void
56
    {
57
        $platform   = $this->connection->getDatabasePlatform();
58
        $defaultSql = $expression($platform, $this);
59
60
        $table = new Table('default_expr_test');
61
        $table->addColumn('actual_value', $type);
62
        $table->addColumn('default_value', $type, ['default' => $defaultSql]);
63
        $this->connection->getSchemaManager()->dropAndCreateTable($table);
64
65
        $this->connection->exec(
66
            sprintf(
67
                'INSERT INTO default_expr_test (actual_value) VALUES (%s)',
68
                $defaultSql
69
            )
70
        );
71
72
        [$actualValue, $defaultValue] = $this->connection->query(
73
            'SELECT default_value, actual_value FROM default_expr_test'
74
        )->fetch(FetchMode::NUMERIC);
75
76
        self::assertEquals($actualValue, $defaultValue);
77
    }
78
}
79