Serialization of tickets with history

This commit is contained in:
2024-04-18 11:35:07 +02:00
parent 670b8eb82b
commit 467bea7cde
13 changed files with 353 additions and 12 deletions

View File

@@ -0,0 +1,144 @@
<?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 Chill\TicketBundle\Tests\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Serializer\Normalizer\TicketNormalizer;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
*
* @coversNothing
*/
class TicketNormalizerTest extends KernelTestCase
{
use ProphecyTrait;
/**
* @dataProvider provideTickets
*/
public function testNormalize(Ticket $ticket, array $expected): void
{
$actual = $this->buildNormalizer()->normalize($ticket, 'json', ['groups' => 'read']);
self::assertEqualsCanonicalizing(array_keys($expected), array_keys($actual));
foreach (array_keys($expected) as $k) {
if ('history' === $k) {
continue;
}
self::assertEqualsCanonicalizing($expected[$k], $actual[$k], sprintf("assert the content of the '%s' key", $k));
}
self::assertArrayHasKey('history', $actual);
self::assertIsArray($actual['history']);
foreach ($actual['history'] as $k => $eventType) {
self::assertEquals($expected['history'][$k]['event_type'], $eventType['event_type']);
}
}
private function buildNormalizer(): TicketNormalizer
{
$normalizer = $this->prophesize(NormalizerInterface::class);
// empty array
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 0 === count($arg)),
'json',
Argument::type('array')
)->willReturn([]);
// array of mixed objects
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_object($arg[0])),
'json',
Argument::type('array')
)->will(function ($args) {
return array_fill(0, count($args[0]), 'embedded');
});
// array of event type
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 0 < count($arg) && is_array($arg[0]) && array_key_exists('event_type', $arg[0])),
'json',
Argument::type('array')
)->will(function ($args): array {
$events = [];
foreach ($args[0] as $event) {
$events[] = $event['event_type'];
}
return $events;
});
// datetime
$normalizer->normalize(Argument::type(\DateTimeImmutable::class), 'json', Argument::type('array'))
->will(function ($args) { return $args[0]->getTimestamp(); });
$normalizer->normalize(Argument::type(Motive::class), 'json', Argument::type('array'))->willReturn(['type' => 'motive', 'id' => 0]);
$normalizer->normalize(Argument::type(PersonHistory::class), 'json', Argument::type('array'))
->willReturn(['personHistory']);
$normalizer->normalize(Argument::type(MotiveHistory::class), 'json', Argument::type('array'))
->willReturn(['motiveHistory']);
$normalizer->normalize(null, 'json', Argument::type('array'))->willReturn(null);
$ticketNormalizer = new TicketNormalizer();
$ticketNormalizer->setNormalizer($normalizer->reveal());
return $ticketNormalizer;
}
public static function provideTickets(): iterable
{
yield [
new Ticket(),
[
'type' => 'ticket_ticket',
'id' => null,
'externalRef' => '',
'currentPersons' => [],
'currentAddressees' => [],
'currentInputs' => [],
'currentMotive' => null,
'history' => [],
],
];
$ticket = new Ticket();
$ticket->setExternalRef('2134');
$personHistory = new PersonHistory(new Person(), $ticket, new \DateTimeImmutable('2024-04-01T12:00:00'));
$ticketHistory = new MotiveHistory(new Motive(), $ticket, new \DateTimeImmutable('2024-04-01T12:02:00'));
yield [
$ticket,
[
'type' => 'ticket_ticket',
'id' => null,
'externalRef' => '2134',
'currentPersons' => ['embedded'],
'currentAddressees' => [],
'currentInputs' => [],
'currentMotive' => ['type' => 'motive', 'id' => 0],
'history' => [['event_type' => 'add_person'], ['event_type' => 'set_motive']],
],
];
}
}