|
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,94 @@ 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(string $defaultTimeZone, DateTimeImmutable $date): void |
| 136 | + { |
| 137 | + $this->iniSet('date.timezone', $defaultTimeZone); |
| 138 | + |
| 139 | + $defaultOffset = (new DateTimeImmutable())->format('O'); |
| 140 | + |
| 141 | + self::assertFalse($defaultOffset === $date->format('O')); |
| 142 | + |
| 143 | + $this->platform->expects(self::once()) |
| 144 | + ->method('getDateFormatString') |
| 145 | + ->willReturn('Y-m-d'); |
| 146 | + |
| 147 | + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6020'); |
| 148 | + |
| 149 | + $this->type->convertToDatabaseValue($date, $this->platform); |
| 150 | + } |
| 151 | + |
| 152 | + /** @dataProvider provideDateTimeInstancesWithTimezone */ |
| 153 | + public function testTimezoneDeprecationFromConvertToPHPValue(string $defaultTimeZone, DateTimeImmutable $date): void |
| 154 | + { |
| 155 | + $this->iniSet('date.timezone', $defaultTimeZone); |
| 156 | + |
| 157 | + $defaultOffset = (new DateTimeImmutable())->format('O'); |
| 158 | + |
| 159 | + self::assertFalse($defaultOffset === $date->format('O')); |
| 160 | + |
| 161 | + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6020'); |
| 162 | + |
| 163 | + $this->type->convertToPHPValue($date, $this->platform); |
| 164 | + } |
| 165 | + |
| 166 | + /** @psalm-return iterable<string, array{0: string, 1: DateTimeImmutable}> */ |
| 167 | + public function provideDateTimeInstancesWithTimezone(): iterable |
| 168 | + { |
| 169 | + yield 'timezone_utc' => [ |
| 170 | + 'UTC', |
| 171 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('America/Argentina/Buenos_Aires')), |
| 172 | + ]; |
| 173 | + |
| 174 | + yield 'timezone_amsterdam' => [ |
| 175 | + 'Europe/Amsterdam', |
| 176 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('America/Argentina/Buenos_Aires')), |
| 177 | + ]; |
| 178 | + |
| 179 | + yield 'offset_utc' => [ |
| 180 | + 'UTC', |
| 181 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('-0300')), |
| 182 | + ]; |
| 183 | + } |
| 184 | + |
| 185 | + /** @dataProvider provideDateTimeInstancesWithNoTimezoneDiff */ |
| 186 | + public function testNoTimezoneInValueConversion(string $defaultTimeZone, DateTimeImmutable $date): void |
| 187 | + { |
| 188 | + $this->iniSet('date.timezone', $defaultTimeZone); |
| 189 | + |
| 190 | + $this->platform->expects(self::once()) |
| 191 | + ->method('getDateFormatString') |
| 192 | + ->willReturn('Y-m-d'); |
| 193 | + |
| 194 | + $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6020'); |
| 195 | + |
| 196 | + $this->type->convertToDatabaseValue($date, $this->platform); |
| 197 | + $this->type->convertToPHPValue($date, $this->platform); |
| 198 | + } |
| 199 | + |
| 200 | + /** @psalm-return iterable<string, array{0: string, 1: DateTimeImmutable}> */ |
| 201 | + public function provideDateTimeInstancesWithNoTimezoneDiff(): iterable |
| 202 | + { |
| 203 | + yield 'timezone_utc' => [ |
| 204 | + 'UTC', |
| 205 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('UTC')), |
| 206 | + ]; |
| 207 | + |
| 208 | + yield 'timezone_buenos_aires' => [ |
| 209 | + 'America/Argentina/Buenos_Aires', |
| 210 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('America/Argentina/Buenos_Aires')), |
| 211 | + ]; |
| 212 | + |
| 213 | + yield 'same_offset_with_different_timezones' => [ |
| 214 | + 'America/Sao_Paulo', |
| 215 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('America/Argentina/Buenos_Aires')), |
| 216 | + ]; |
| 217 | + |
| 218 | + yield 'offset_-0300' => [ |
| 219 | + 'America/Argentina/Buenos_Aires', |
| 220 | + (new DateTimeImmutable())->setTimezone(new DateTimeZone('-0300')), |
| 221 | + ]; |
| 222 | + } |
120 | 223 | }
|
0 commit comments