File: CarbonTypesTest.php

package info (click to toggle)
php-nesbot-carbon 3.10.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,132 kB
  • sloc: php: 163,896; xml: 119; makefile: 32; sh: 14
file content (226 lines) | stat: -rw-r--r-- 8,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

declare(strict_types=1);

/**
 * This file is part of the Carbon package.
 *
 * (c) Brian Nesbitt <brian@nesbot.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tests\Doctrine;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\Doctrine\CarbonImmutableType;
use Carbon\Doctrine\CarbonType;
use Carbon\Doctrine\CarbonTypeConverter;
use Carbon\Doctrine\DateTimeDefaultPrecision;
use Carbon\Doctrine\DateTimeImmutableType;
use Carbon\Doctrine\DateTimeType;
use DateTimeImmutable;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Types\ConversionException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\AbstractTestCase;
use Tests\Fixtures\CarbonTypeCase;

class CarbonTypesTest extends AbstractTestCase
{
    private static ?array $types = null;

    public static function setUpBeforeClass(): void
    {
        foreach (static::dataForTypes() as [$case]) {
            $case->initialize();
        }
    }

    public static function dataForTypes(): array
    {
        return self::$types ??= self::generateDataForTypes();
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testGetSQLDeclaration(CarbonTypeCase $case): void
    {
        $type = $case->getType();

        $adaptPrecisionToPlatform = method_exists(CarbonTypeConverter::class, 'getMaximumPrecision');
        $precision = DateTimeDefaultPrecision::get();
        $this->assertSame(6, $precision);
        $supportZeroPrecision = self::supportsZeroPrecision();

        $this->assertSame('DATETIME', $type->getSQLDeclaration($supportZeroPrecision ? [
            'precision' => 0,
        ] : [
            'precision' => null,
            'secondPrecision' => true,
        ], $this->getMySQLPlatform()));

        $this->assertSame('DATETIME(3)', $type->getSQLDeclaration([
            'precision' => 3,
        ], $this->getMySQLPlatform()));

        $this->assertSame('TIMESTAMP(0)', $type->getSQLDeclaration($supportZeroPrecision ? [
            'precision' => 0,
        ] : [
            'precision' => null,
            'secondPrecision' => true,
        ], new DB2Platform()));

        $this->assertSame('TIMESTAMP(6)', $type->getSQLDeclaration([
            'precision' => null,
        ], new DB2Platform()));

        $this->assertSame('TIMESTAMP(6)', $type->getSQLDeclaration($supportZeroPrecision ? [
            'precision' => null,
        ] : [
            'precision' => 0,
        ], new DB2Platform()));

        $this->assertSame('DATETIME(6)', $type->getSQLDeclaration($supportZeroPrecision ? [
            'precision' => null,
        ] : [
            'precision' => 0,
        ], $this->getMySQLPlatform()));

        $this->assertSame('DATETIME(6)', $type->getSQLDeclaration([
            'precision' => null,
        ], $this->getMySQLPlatform()));

        DateTimeDefaultPrecision::set(4);
        $this->assertSame('DATETIME(4)', $type->getSQLDeclaration([
            'precision' => null,
        ], $this->getMySQLPlatform()));

        DateTimeDefaultPrecision::set(9);
        $this->assertSame($adaptPrecisionToPlatform ? 'DATETIME(6)' : 'DATETIME(9)', $type->getSQLDeclaration([
            'precision' => null,
        ], $this->getMySQLPlatform()));

        DateTimeDefaultPrecision::set(0);
        $this->assertSame('DATETIME', $type->getSQLDeclaration([
            'precision' => null,
        ], $this->getMySQLPlatform()));

        DateTimeDefaultPrecision::set($precision);
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testConvertToPHPValue(CarbonTypeCase $case): void
    {
        $type = $case->getType();

        $this->assertNull($type->convertToPHPValue(null, $this->getMySQLPlatform()));

        $date = $type->convertToPHPValue(Carbon::parse('2020-06-23 18:47'), $this->getMySQLPlatform());
        $this->assertInstanceOf($case->class, $date);
        $this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));

        $date = $type->convertToPHPValue(new DateTimeImmutable('2020-06-23 18:47'), $this->getMySQLPlatform());
        $this->assertInstanceOf($case->class, $date);
        $this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));

        $date = $type->convertToPHPValue('2020-06-23 18:47', $this->getMySQLPlatform());
        $this->assertInstanceOf($case->class, $date);
        $this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testConvertToPHPValueFailure(CarbonTypeCase $case): void
    {
        $conversion = version_compare(self::getDbalVersion(), '4.0.0', '>=')
            ? "to \"$case->typeClass\" as an error was triggered by the unserialization: "
            : "\"2020-0776-23 18:47\" to Doctrine Type $case->name. Expected format: ";
        $this->expectExceptionObject(new ConversionException(
            'Could not convert database value '.$conversion.
            "Y-m-d H:i:s.u or any format supported by $case->class::parse()",
        ));

        $case->getType()->convertToPHPValue('2020-0776-23 18:47', $this->getMySQLPlatform());
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testConvertToDatabaseValue(CarbonTypeCase $case): void
    {
        $type = $case->getType();

        $this->assertNull($type->convertToDatabaseValue(null, $this->getMySQLPlatform()));
        $this->assertSame(
            '2020-06-23 18:47:00.000000',
            $type->convertToDatabaseValue(new DateTimeImmutable('2020-06-23 18:47'), $this->getMySQLPlatform()),
        );
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testConvertToDatabaseValueFailure(CarbonTypeCase $case): void
    {
        $quote = class_exists('Doctrine\\DBAL\\Version') ? "'" : '';
        $conversion = version_compare(self::getDbalVersion(), '4.0.0', '>=')
            ? "array to type $case->typeClass. "
            : "{$quote}array{$quote} to type {$quote}$case->name{$quote}. ";
        $this->expectExceptionObject(new ConversionException(
            'Could not convert PHP value of type '.$conversion.
            'Expected one of the following types: null, DateTime, Carbon',
        ));

        $case->getType()->convertToDatabaseValue([2020, 06, 23], $this->getMySQLPlatform());
    }

    #[Group('doctrine')]
    #[DataProvider('dataForTypes')]
    public function testRequiresSQLCommentHint(CarbonTypeCase $case): void
    {
        if (version_compare(self::getDbalVersion(), '4.0.0', '>=')) {
            $this->markTestSkipped('requiresSQLCommentHint dropped since DBAL 4');
        }

        $this->assertSame(
            $case->hintRequired,
            $case->getType()->requiresSQLCommentHint($this->getMySQLPlatform()),
        );
    }

    private static function getDbalVersion(): string
    {
        static $dbalVersion = '4.3.4';
        return $dbalVersion;
    }

    private static function supportsZeroPrecision(): bool
    {
        return version_compare(self::getDbalVersion(), '3.7.0', '>=');
    }

    private function getMySQLPlatform(): AbstractMySQLPlatform
    {
        return class_exists(MySQLPlatform::class) ? new MySQLPlatform() : new MySQL57Platform();
    }

    private static function generateDataForTypes(): array
    {
        $supportZeroPrecision = self::supportsZeroPrecision();

        $types = [
            [new CarbonTypeCase($supportZeroPrecision ? 'date_time' : 'datetime', Carbon::class, DateTimeType::class, false)],
            [new CarbonTypeCase($supportZeroPrecision ? 'date_time_immutable' : 'datetime_immutable', CarbonImmutable::class, DateTimeImmutableType::class, true)],
            [new CarbonTypeCase('carbon', Carbon::class, CarbonType::class, !$supportZeroPrecision)],
            [new CarbonTypeCase('carbon_immutable', CarbonImmutable::class, CarbonImmutableType::class, true)],
        ];

        return array_combine(array_column($types, 0), $types);
    }
}