From e1b8b4881dee31047daae64e1e4b1873ef9217f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Sun, 15 Dec 2024 12:08:09 +0100 Subject: [PATCH 1/3] Allowed to use DateTimeInterface in DateTimePicker form type --- .../DateTimePickerTransformer.php | 5 +- .../DateTimePickerTransformerTest.php | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php diff --git a/src/lib/Form/DataTransformer/DateTimePickerTransformer.php b/src/lib/Form/DataTransformer/DateTimePickerTransformer.php index 128c814699..cbe49493c4 100644 --- a/src/lib/Form/DataTransformer/DateTimePickerTransformer.php +++ b/src/lib/Form/DataTransformer/DateTimePickerTransformer.php @@ -9,6 +9,7 @@ namespace Ibexa\AdminUi\Form\DataTransformer; use DateTime; +use DateTimeInterface; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; @@ -29,9 +30,9 @@ public function transform($value) return null; } - if (!$value instanceof DateTime) { + if (!$value instanceof DateTimeInterface) { throw new TransformationFailedException( - sprintf('Found %s instead of %s', gettype($value), DateTime::class) + sprintf('Found %s instead of %s', gettype($value), DateTimeInterface::class) ); } diff --git a/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php b/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php new file mode 100644 index 0000000000..b4d12faf72 --- /dev/null +++ b/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php @@ -0,0 +1,75 @@ +assertSame($dateTime->getTimestamp(), $transformer->transform($dateTime)); + } + + /** + * @return iterable + */ + public function dataProviderForTransform(): iterable + { + yield 'null' => [null, null]; + yield 'DateTime' => [new DateTime('2021-01-01 00:00:00'), 1609459200]; + yield 'DateTimeImmutable' => [new DateTimeImmutable('2021-01-01 00:00:00'), 1609459200]; + } + + public function testTransformWithInvalidValue(): void + { + $this->expectException(TransformationFailedException::class); + $this->expectExceptionMessage('Found string instead of DateTimeInterface'); + + $transformer = new DateTimePickerTransformer(); + $transformer->transform('invalid'); + } + + /** + * @dataProvider dataProviderForReverseTransform + */ + public function testReverseTransform(): void + { + $transformer = new DateTimePickerTransformer(); + $dateTime = new DateTime('2021-01-01 00:00:00'); + $this->assertEquals($dateTime, $transformer->reverseTransform($dateTime->getTimestamp())); + } + + /** + * @return iterable + */ + public function dataProviderForReverseTransform(): iterable + { + yield 'null' => [null, null]; + yield 'DateTime' => [1609459200, new DateTime('2021-01-01 00:00:00')]; + } + + public function testReverseTransformWithInvalidValue(): void + { + $this->expectException(TransformationFailedException::class); + $this->expectExceptionMessage('Found string instead of a numeric value'); + + $transformer = new DateTimePickerTransformer(); + $transformer->reverseTransform('invalid'); + } +} From 9230e25646ef97757835931e6ffdfa8f6257fdab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 16 Dec 2024 09:56:04 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Konrad Oboza --- .../DataTransformer/DateTimePickerTransformerTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php b/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php index b4d12faf72..5bfce9f805 100644 --- a/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php +++ b/tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php @@ -17,7 +17,7 @@ final class DateTimePickerTransformerTest extends TestCase { /** - * @dataProvider dataProviderForTransform + * @dataProvider dataProviderForTestTransform */ public function testTransform(): void { @@ -29,7 +29,7 @@ public function testTransform(): void /** * @return iterable */ - public function dataProviderForTransform(): iterable + public function dataProviderForTestTransform(): iterable { yield 'null' => [null, null]; yield 'DateTime' => [new DateTime('2021-01-01 00:00:00'), 1609459200]; @@ -46,7 +46,7 @@ public function testTransformWithInvalidValue(): void } /** - * @dataProvider dataProviderForReverseTransform + * @dataProvider dataProviderForTestReverseTransform */ public function testReverseTransform(): void { @@ -58,7 +58,7 @@ public function testReverseTransform(): void /** * @return iterable */ - public function dataProviderForReverseTransform(): iterable + public function dataProviderForTestReverseTransform(): iterable { yield 'null' => [null, null]; yield 'DateTime' => [1609459200, new DateTime('2021-01-01 00:00:00')]; From b9bb63b27f278805f99f2e8395e98359a8e5f9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 16 Dec 2024 10:11:26 +0100 Subject: [PATCH 3/3] Update src/lib/Form/DataTransformer/DateTimePickerTransformer.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paweł Niedzielski --- src/lib/Form/DataTransformer/DateTimePickerTransformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Form/DataTransformer/DateTimePickerTransformer.php b/src/lib/Form/DataTransformer/DateTimePickerTransformer.php index cbe49493c4..436ca1e410 100644 --- a/src/lib/Form/DataTransformer/DateTimePickerTransformer.php +++ b/src/lib/Form/DataTransformer/DateTimePickerTransformer.php @@ -32,7 +32,7 @@ public function transform($value) if (!$value instanceof DateTimeInterface) { throw new TransformationFailedException( - sprintf('Found %s instead of %s', gettype($value), DateTimeInterface::class) + sprintf('Found %s instead of %s', get_debug_type($value), DateTimeInterface::class) ); }