|
4 | 4 |
|
5 | 5 | use DateTime;
|
6 | 6 | use DateTimeImmutable;
|
| 7 | +use DateTimeZone; |
7 | 8 | use Doctrine\DBAL\ParameterType;
|
8 | 9 | use Doctrine\DBAL\Platforms\AbstractPlatform;
|
9 | 10 | use Doctrine\DBAL\Types\ConversionException;
|
10 | 11 | use Doctrine\DBAL\Types\DateImmutableType;
|
| 12 | +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; |
| 13 | +use InvalidArgumentException; |
11 | 14 | use PHPUnit\Framework\MockObject\MockObject;
|
12 | 15 | use PHPUnit\Framework\TestCase;
|
13 | 16 |
|
14 | 17 | use function get_class;
|
15 | 18 |
|
16 | 19 | class DateImmutableTypeTest extends TestCase
|
17 | 20 | {
|
| 21 | + use VerifyDeprecations; |
| 22 | + |
18 | 23 | /** @var AbstractPlatform&MockObject */
|
19 | 24 | private AbstractPlatform $platform;
|
20 | 25 |
|
@@ -48,10 +53,18 @@ public function testConvertsDateTimeImmutableInstanceToDatabaseValue(): void
|
48 | 53 | $this->platform->expects(self::once())
|
49 | 54 | ->method('getDateFormatString')
|
50 | 55 | ->willReturn('Y-m-d');
|
51 |
| - $date->expects(self::once()) |
| 56 | + $date->expects(self::exactly(2)) |
52 | 57 | ->method('format')
|
53 |
| - ->with('Y-m-d') |
54 |
| - ->willReturn('2016-01-01'); |
| 58 | + ->willReturnCallback(static function (string $format): string { |
| 59 | + switch ($format) { |
| 60 | + case 'O': |
| 61 | + return 'UTC'; |
| 62 | + case 'Y-m-d': |
| 63 | + return '2016-01-01'; |
| 64 | + default: |
| 65 | + throw new InvalidArgumentException(); |
| 66 | + } |
| 67 | + }); |
55 | 68 |
|
56 | 69 | self::assertSame(
|
57 | 70 | '2016-01-01',
|
@@ -117,4 +130,42 @@ public function testRequiresSQLCommentHint(): void
|
117 | 130 | {
|
118 | 131 | self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
|
119 | 132 | }
|
| 133 | + |
| 134 | + /** @dataProvider provideDateTimeInstancesWithTimezone */ |
| 135 | + public function testTimezoneDeprecationFromConvertsToDatabaseValue(DateTimeImmutable $date): void |
| 136 | + { |
| 137 | + $defaultOffset = (new DateTimeImmutable())->format('O'); |
| 138 | + |
| 139 | + self::assertFalse($defaultOffset === $date->format('O')); |
| 140 | + |
| 141 | + $this->platform->expects(self::once()) |
| 142 | + ->method('getDateFormatString') |
| 143 | + ->willReturn('Y-m-d'); |
| 144 | + |
| 145 | + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6020'); |
| 146 | + |
| 147 | + $this->type->convertToDatabaseValue($date, $this->platform); |
| 148 | + } |
| 149 | + |
| 150 | + /** @psalm-return iterable<string, array<0: DateTimeImmutable>> */ |
| 151 | + public function provideDateTimeInstancesWithTimezone(): iterable |
| 152 | + { |
| 153 | + yield 'timezone' => [ |
| 154 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('America/Argentina/Buenos_Aires')), |
| 155 | + ]; |
| 156 | + yield 'offset' => [ |
| 157 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('-0300')), |
| 158 | + ]; |
| 159 | + } |
| 160 | + |
| 161 | + public function testNoTimezoneFromConvertsToDatabaseValue(): void |
| 162 | + { |
| 163 | + $this->platform->expects(self::once()) |
| 164 | + ->method('getDateFormatString') |
| 165 | + ->willReturn('Y-m-d'); |
| 166 | + |
| 167 | + $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6020'); |
| 168 | + |
| 169 | + $this->type->convertToDatabaseValue(new DateTimeImmutable(), $this->platform); |
| 170 | + } |
120 | 171 | }
|
0 commit comments