|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CuyZ\Valinor\Tests\Integration\Normalizer\Configurator; |
| 4 | + |
| 5 | +use CuyZ\Valinor\Normalizer\Configurator\ConvertDateTime; |
| 6 | +use CuyZ\Valinor\Normalizer\Format; |
| 7 | +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; |
| 8 | +use DateTimeInterface; |
| 9 | +use DateTimeZone; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use Safe\DateTimeImmutable; |
| 12 | + |
| 13 | +class ConvertDateTimeTest extends IntegrationTestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @param array<string, non-empty-string> $expectedValue |
| 17 | + * @param non-empty-string $format |
| 18 | + */ |
| 19 | + #[DataProvider('date_time_data_provider')] |
| 20 | + public function test_date_time_converts_values(array $expectedValue, string $format, object $object): void |
| 21 | + { |
| 22 | + $result = $this->normalizerBuilder() |
| 23 | + ->configureWith(new ConvertDateTime($format)) |
| 24 | + ->normalizer(Format::array()) |
| 25 | + ->normalize($object); |
| 26 | + |
| 27 | + self::assertSame($expectedValue, $result); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return iterable<string, array{array, non-empty-string, object}> |
| 32 | + */ |
| 33 | + public static function date_time_data_provider(): iterable |
| 34 | + { |
| 35 | + $object = new class () { |
| 36 | + public DateTimeInterface $someDateTime; |
| 37 | + }; |
| 38 | + $object->someDateTime = new DateTimeImmutable('1955-11-05T21:10:14', new DateTimeZone('UTC')); |
| 39 | + |
| 40 | + yield 'to ATOM format' => [['someDateTime' => '1955-11-05T21:10:14+00:00'], DateTimeInterface::ATOM, $object]; |
| 41 | + yield 'to RFC1036 format' => [['someDateTime' => 'Sat, 05 Nov 55 21:10:14 +0000'], DateTimeInterface::RFC1036, $object]; |
| 42 | + yield 'to custom format' => [['someDateTime' => '05.11.1955 21:10:14'], 'd.m.Y H:i:s', $object]; |
| 43 | + } |
| 44 | +} |
0 commit comments