mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
89 lines
3.6 KiB
PHP
89 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Serializer\Normalizer;
|
|
|
|
use Chill\MainBundle\Serializer\Normalizer\DateNormalizer;
|
|
use Prophecy\Prophet;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
class DateNormalizerTest extends KernelTestCase
|
|
{
|
|
private Prophet $prophet;
|
|
|
|
public function setUp()
|
|
{
|
|
$this->prophet = new Prophet();
|
|
}
|
|
|
|
public function testSupports()
|
|
{
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(new \DateTime(), 'json'));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(new \DateTimeImmutable(), 'json'));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(new \DateTime(), 'docgen'));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(new \DateTimeImmutable(), 'docgen'));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(null, 'docgen', ['docgen:expects' => \DateTimeImmutable::class]));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(null, 'docgen', ['docgen:expects' => \DateTimeInterface::class]));
|
|
$this->assertTrue($this->buildDateNormalizer()->supportsNormalization(null, 'docgen', ['docgen:expects' => \DateTime::class]));
|
|
$this->assertFalse($this->buildDateNormalizer()->supportsNormalization(new \stdClass(), 'docgen'));
|
|
$this->assertFalse($this->buildDateNormalizer()->supportsNormalization(new \DateTime(), 'xml'));
|
|
}
|
|
|
|
/**
|
|
* @dataProvider generateDataNormalize
|
|
*/
|
|
public function testNormalize($expected, $date, $format, $locale, $msg)
|
|
{
|
|
$this->assertEquals($expected, $this->buildDateNormalizer($locale)->normalize($date, $format, []), $msg);
|
|
}
|
|
|
|
private function buildDateNormalizer(string $locale = null): DateNormalizer
|
|
{
|
|
$requestStack = $this->prophet->prophesize(RequestStack::class);
|
|
$parameterBag = new ParameterBag();
|
|
$parameterBag->set('kernel.default_locale', 'fr');
|
|
|
|
if ($locale === null) {
|
|
$requestStack->getCurrentRequest()->willReturn(null);
|
|
} else {
|
|
$request = $this->prophet->prophesize(Request::class);
|
|
$request->getLocale()->willReturn($locale);
|
|
$requestStack->getCurrentRequest()->willReturn($request->reveal());
|
|
}
|
|
|
|
return new DateNormalizer($requestStack->reveal(), $parameterBag);
|
|
}
|
|
|
|
public function generateDataNormalize()
|
|
{
|
|
$datetime = \DateTime::createFromFormat('Y-m-d H:i:sO', '2021-06-05 15:05:01+02:00');
|
|
$date = \DateTime::createFromFormat('Y-m-d H:i:sO', '2021-06-05 00:00:00+02:00');
|
|
yield [
|
|
['datetime' => '2021-06-05T15:05:01+0200'],
|
|
$datetime, 'json', null, 'simple normalization to json'
|
|
];
|
|
|
|
yield [
|
|
['long' => '5 juin 2021', 'short' => '05/06/2021'],
|
|
$date, 'docgen', 'fr', 'normalization to docgen for a date, with current request'
|
|
];
|
|
|
|
yield [
|
|
['long' => '5 juin 2021', 'short' => '05/06/2021'],
|
|
$date, 'docgen', null, 'normalization to docgen for a date, without current request'
|
|
];
|
|
|
|
yield [
|
|
['long' => '5 juin 2021 à 15:05', 'short' => '05/06/2021 15:05'],
|
|
$datetime, 'docgen', null, 'normalization to docgen for a datetime, without current request'
|
|
];
|
|
|
|
yield [
|
|
['long' => '', 'short' => ''],
|
|
null, 'docgen', null, 'normalization to docgen for a null datetime'
|
|
];
|
|
}
|
|
}
|