add unit test for GenderDocGenNormalizer and move file to MainBundle

This commit is contained in:
Julie Lenaerts 2024-11-26 18:04:12 +01:00
parent f4f5153ed0
commit 5afb92d549
3 changed files with 73 additions and 4 deletions

View File

@ -21,13 +21,13 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Table(name: 'chill_main_gender')] #[ORM\Table(name: 'chill_main_gender')]
class Gender class Gender
{ {
#[Serializer\Groups(['read', 'docgen:read'])] #[Serializer\Groups(['read'])]
#[ORM\Id] #[ORM\Id]
#[ORM\GeneratedValue] #[ORM\GeneratedValue]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
private ?int $id = null; private ?int $id = null;
#[Serializer\Groups(['read', 'docgen:read'])] #[Serializer\Groups(['read'])]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON)] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON)]
private array $label = []; private array $label = [];
@ -36,7 +36,7 @@ class Gender
private bool $active = true; private bool $active = true;
#[Assert\NotNull(message: 'You must choose a gender translation')] #[Assert\NotNull(message: 'You must choose a gender translation')]
#[Serializer\Groups(['read', 'docgen:read'])] #[Serializer\Groups(['read'])]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, enumType: GenderEnum::class)] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, enumType: GenderEnum::class)]
private GenderEnum $genderTranslation; private GenderEnum $genderTranslation;

View File

@ -9,7 +9,7 @@ declare(strict_types=1);
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
*/ */
namespace Chill\PersonBundle\Serializer\Normalizer; namespace Chill\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Gender; use Chill\MainBundle\Entity\Gender;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/*
* 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\Entity\Gender;
use Chill\MainBundle\Entity\GenderEnum;
use Chill\MainBundle\Serializer\Normalizer\GenderDocGenNormalizer;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
final class GenderDocGenNormalizerTest extends TestCase
{
private GenderDocGenNormalizer $normalizer;
private TranslatableStringHelperInterface $translatableStringHelper;
protected function setUp(): void
{
$this->translatableStringHelper = $this->createMock(TranslatableStringHelperInterface::class);
$this->normalizer = new GenderDocGenNormalizer($this->translatableStringHelper);
}
public function testSupportsNormalizationReturnsTrueForGenderInstance(): void
{
$gender = $this->createMock(Gender::class);
$this->assertTrue($this->normalizer->supportsNormalization($gender));
}
public function testSupportsNormalizationReturnsFalseForNonGenderInstance(): void
{
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testNormalizeReturnsCorrectData(): void
{
$gender = $this->createMock(Gender::class);
$gender->method('getId')->willReturn(1);
$gender->method('getLabel')->willReturn(['fr' => 'homme', 'en' => 'man']);
$gender->method('getGenderTranslation')->willReturn(GenderEnum::MALE);
$this->translatableStringHelper
->method('localize')
->with(['fr' => 'homme', 'en' => 'man'])
->willReturn('homme');
$expected = [
'id' => 1,
'label' => 'homme',
'genderTranslation' => GenderEnum::MALE,
];
$this->assertEquals($expected, $this->normalizer->normalize($gender));
}
}