Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed to use DateTimeInterface in DateTimePicker form type #1411

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
Loading