mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 22:35:01 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,14 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Serializer\Normalizer\DateNormalizer;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Prophecy\Prophet;
|
||||
use stdClass;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class DateNormalizerTest extends KernelTestCase
|
||||
{
|
||||
private Prophet $prophet;
|
||||
@@ -18,34 +33,71 @@ class DateNormalizerTest extends KernelTestCase
|
||||
$this->prophet = new Prophet();
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
public function generateDataNormalize()
|
||||
{
|
||||
$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'));
|
||||
$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',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateDataNormalize
|
||||
*
|
||||
* @param mixed $expected
|
||||
* @param mixed $date
|
||||
* @param mixed $format
|
||||
* @param mixed $locale
|
||||
* @param mixed $msg
|
||||
*/
|
||||
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
|
||||
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'));
|
||||
}
|
||||
|
||||
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) {
|
||||
if (null === $locale) {
|
||||
$requestStack->getCurrentRequest()->willReturn(null);
|
||||
} else {
|
||||
$request = $this->prophet->prophesize(Request::class);
|
||||
@@ -55,34 +107,4 @@ class DateNormalizerTest extends KernelTestCase
|
||||
|
||||
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'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Serializer\Normalizer;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Serializer\Normalizer\DoctrineExistingEntityNormalizer;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class DoctrineExistingEntityNormalizerTest extends KernelTestCase
|
||||
{
|
||||
protected DoctrineExistingEntityNormalizer $normalizer;
|
||||
@@ -16,23 +26,12 @@ class DoctrineExistingEntityNormalizerTest extends KernelTestCase
|
||||
protected function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$serializerFactory = self::$container->get(ClassMetadataFactoryInterface::class);
|
||||
|
||||
$this->normalizer = new DoctrineExistingEntityNormalizer($em, $serializerFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderUserId
|
||||
*/
|
||||
public function testGetMappedClass($userId)
|
||||
{
|
||||
$data = [ 'type' => 'user', 'id' => $userId];
|
||||
$supports = $this->normalizer->supportsDenormalization($data, User::class);
|
||||
|
||||
$this->assertTrue($supports);
|
||||
}
|
||||
|
||||
public function dataProviderUserId()
|
||||
{
|
||||
self::bootKernel();
|
||||
@@ -43,9 +42,21 @@ class DoctrineExistingEntityNormalizerTest extends KernelTestCase
|
||||
->select('u.id')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
->getResult();
|
||||
|
||||
yield [ $userIds[0]['id'] ];
|
||||
yield [$userIds[0]['id']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderUserId
|
||||
*
|
||||
* @param mixed $userId
|
||||
*/
|
||||
public function testGetMappedClass($userId)
|
||||
{
|
||||
$data = ['type' => 'user', 'id' => $userId];
|
||||
$supports = $this->normalizer->supportsDenormalization($data, User::class);
|
||||
|
||||
$this->assertTrue($supports);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user