Completed
Pull Request — master (#3547)
by Sergei
15:44
created

DefaultExpressionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 31
dl 0
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A expressionProvider() 0 32 4
A testDefaultExpression() 0 21 1
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
    /**
19
     * @dataProvider expressionProvider
20
     */
21
    public function testDefaultExpression(string $type, callable $expression) : void
22
    {
23
        $platform   = $this->connection->getDatabasePlatform();
24
        $defaultSql = $expression($platform, $this);
25
26
        $table = new Table('default_expr_test');
27
        $table->addColumn('actual_value', $type);
28
        $table->addColumn('default_value', $type, ['default' => $defaultSql]);
29
        $this->connection->getSchemaManager()->dropAndCreateTable($table);
30
31
        $query = sprintf(
32
            'INSERT INTO default_expr_test (actual_value) VALUES (%s)',
33
            $defaultSql
34
        );
35
36
        $this->connection->exec($query);
37
38
        $query                        = sprintf('SELECT default_value, actual_value FROM default_expr_test');
39
        [$actualValue, $defaultValue] = $this->connection->query($query)->fetch(FetchMode::NUMERIC);
40
41
        self::assertEquals($actualValue, $defaultValue);
42
    }
43
44
    /**
45
     * @return mixed[][]
46
     */
47
    public static function expressionProvider() : iterable
48
    {
49
        yield 'current-date' => [
0 ignored issues
show
Bug Best Practice introduced by
The expression yield 'current-date' => ...ion(...) { /* ... */ }) returns the type Generator which is incompatible with the documented return type array<mixed,array<mixed,mixed>>.
Loading history...
50
            Types::DATE_MUTABLE,
51
            static function (AbstractPlatform $platform) : string {
52
                if ($platform instanceof MySqlPlatform) {
53
                    self::markTestSkipped('Not supported on MySQL');
54
                }
55
56
                return $platform->getCurrentDateSQL();
57
            },
58
        ];
59
60
        yield 'current-time' => [
61
            Types::TIME_MUTABLE,
62
            static function (AbstractPlatform $platform) : string {
63
                if ($platform instanceof MySqlPlatform) {
64
                    self::markTestSkipped('Not supported on MySQL');
65
                }
66
67
                if ($platform instanceof OraclePlatform) {
68
                    self::markTestSkipped('Not supported on Oracle');
69
                }
70
71
                return $platform->getCurrentTimeSQL();
72
            },
73
        ];
74
75
        yield 'current-timestamp' => [
76
            Types::DATETIME_MUTABLE,
77
            static function (AbstractPlatform $platform) : string {
78
                return $platform->getCurrentTimestampSQL();
79
            },
80
        ];
81
    }
82
}
83