mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-29 01:55:01 +00:00
Add integration and unit tests for PersonJsonNormalizer
to verify normalization behavior
- Introduce `PersonJsonNormalizerIntegrationTest` to test database-driven normalization scenarios. - Expand `PersonJsonNormalizerTest` with cases covering minimal group normalization and extended keys. - Refactor test setup to use mock objects and improve coverage of normalization logic.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?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\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
final class PersonJsonNormalizerIntegrationTest extends KernelTestCase
|
||||
{
|
||||
public function testNormalizeExistingPersonFromDatabase(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
$container = self::getContainer();
|
||||
|
||||
/** @var PersonRepository $repo */
|
||||
$repo = $container->get(PersonRepository::class);
|
||||
$person = $repo->findOneBy([]);
|
||||
|
||||
if (!$person instanceof Person) {
|
||||
self::markTestSkipped('No person found in test database. Load fixtures to enable this test.');
|
||||
}
|
||||
|
||||
/** @var SerializerInterface $serializer */
|
||||
$serializer = $container->get(SerializerInterface::class);
|
||||
|
||||
// Should not throw
|
||||
$data = $serializer->normalize($person, 'json');
|
||||
Assert::assertIsArray($data);
|
||||
|
||||
// Spot check some expected keys exist
|
||||
foreach ([
|
||||
'type', 'id', 'text', 'textAge', 'firstName', 'lastName', 'birthdate', 'age', 'gender', 'civility',
|
||||
] as $key) {
|
||||
Assert::assertArrayHasKey($key, $data, sprintf('Expected key %s in normalized payload', $key));
|
||||
}
|
||||
|
||||
// Minimal group should also work
|
||||
$minimal = $serializer->normalize($person, 'json', ['groups' => 'minimal']);
|
||||
Assert::assertIsArray($minimal);
|
||||
foreach ([
|
||||
'type', 'id', 'text', 'textAge', 'firstName', 'lastName',
|
||||
] as $key) {
|
||||
Assert::assertArrayHasKey($key, $minimal, sprintf('Expected key %s in minimal normalized payload', $key));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user