mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-03 20:49:41 +00:00
Serialization of tickets with history
This commit is contained in:
@@ -64,7 +64,7 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
|
||||
{
|
||||
$motive = new Motive();
|
||||
$ticket = new Ticket();
|
||||
$ticket->addMotiveHistory(new MotiveHistory(new Motive(), $ticket));
|
||||
$history = new MotiveHistory(new Motive(), $ticket);
|
||||
|
||||
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
||||
$entityManager->persist(Argument::that(static function ($arg) use ($motive): bool {
|
||||
@@ -87,7 +87,7 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
|
||||
{
|
||||
$motive = new Motive();
|
||||
$ticket = new Ticket();
|
||||
$ticket->addMotiveHistory(new MotiveHistory($motive, $ticket));
|
||||
$history = new MotiveHistory($motive, $ticket);
|
||||
|
||||
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
||||
$entityManager->persist(Argument::that(static function ($arg) use ($motive): bool {
|
||||
|
||||
@@ -11,8 +11,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\TicketBundle\Tests\Entity;
|
||||
|
||||
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 Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
@@ -31,7 +33,6 @@ class TicketTest extends KernelTestCase
|
||||
self::assertNull($ticket->getMotive());
|
||||
|
||||
$history = new MotiveHistory($motive, $ticket);
|
||||
$ticket->addMotiveHistory($history);
|
||||
|
||||
self::assertSame($motive, $ticket->getMotive());
|
||||
self::assertCount(1, $ticket->getMotiveHistories());
|
||||
@@ -39,9 +40,22 @@ class TicketTest extends KernelTestCase
|
||||
// replace motive
|
||||
$motive2 = new Motive();
|
||||
$history->setEndDate(new \DateTimeImmutable());
|
||||
$ticket->addMotiveHistory(new MotiveHistory($motive2, $ticket));
|
||||
$history2 = new MotiveHistory($motive2, $ticket);
|
||||
|
||||
self::assertCount(2, $ticket->getMotiveHistories());
|
||||
self::assertSame($motive2, $ticket->getMotive());
|
||||
}
|
||||
|
||||
public function testGetPerson(): void
|
||||
{
|
||||
$ticket = new Ticket();
|
||||
$person = new Person();
|
||||
|
||||
self::assertEquals([], $ticket->getPersons());
|
||||
|
||||
$history = new PersonHistory($person, $ticket, new \DateTimeImmutable('now'));
|
||||
|
||||
self::assertCount(1, $ticket->getPersons());
|
||||
self::assertSame($person, $ticket->getPersons()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user