Skip to content

Commit

Permalink
Allowed to use DateTimeInterface in DateTimePicker form type (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Dec 16, 2024
1 parent fc748c7 commit 4f18fbb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/Form/DataTransformer/DateTimePickerTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\AdminUi\Form\DataTransformer;

use DateTime;
use DateTimeInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

Expand All @@ -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', get_debug_type($value), DateTimeInterface::class)
);
}

Expand Down
75 changes: 75 additions & 0 deletions tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\AdminUi\Form\DataTransformer;

use DateTime;
use DateTimeImmutable;
use Ibexa\AdminUi\Form\DataTransformer\DateTimePickerTransformer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;

final class DateTimePickerTransformerTest extends TestCase
{
/**
* @dataProvider dataProviderForTestTransform
*/
public function testTransform(): void
{
$transformer = new DateTimePickerTransformer();
$dateTime = new DateTime('2021-01-01 00:00:00');
$this->assertSame($dateTime->getTimestamp(), $transformer->transform($dateTime));
}

/**
* @return iterable<string, array{mixed, ?int}>
*/
public function dataProviderForTestTransform(): 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 dataProviderForTestReverseTransform
*/
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<string, array{?int, ?DateTime}>
*/
public function dataProviderForTestReverseTransform(): 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');
}
}

0 comments on commit 4f18fbb

Please sign in to comment.