-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowed to use DateTimeInterface in DateTimePicker form type (#1411)
- Loading branch information
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tests/lib/Form/DataTransformer/DateTimePickerTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |