<?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));
        }
    }
}
