Manage the persons' assocation with ticket through SetPersonsCommand and dedicated handler

This commit is contained in:
2024-06-03 13:53:28 +02:00
parent a777588bb8
commit 631f047338
5 changed files with 283 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
<?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\Action\Ticket\Handler;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Action\Ticket\Handler\SetPersonsCommandHandler;
use Chill\TicketBundle\Action\Ticket\SetPersonsCommand;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Security\Core\Security;
/**
* @internal
*
* @coversNothing
*/
final class SetPersonsCommandHandlerTest extends TestCase
{
use ProphecyTrait;
public function testHandleOnEmptyAddresses(): void
{
$ticket = new Ticket();
$command = new SetPersonsCommand([$person1 = new Person(), $group1 = new Person()]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($person1) {
return $arg instanceof PersonHistory && $arg->getPerson() === $person1;
}))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(function ($arg) use ($group1) {
return $arg instanceof PersonHistory && $arg->getPerson() === $group1;
}))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertCount(2, $ticket->getPersons());
}
public function testHandleExistingPersonIsNotRemovedNorCreatingDouble(): void
{
$ticket = new Ticket();
$person = new Person();
$history = new PersonHistory($person, $ticket, new \DateTimeImmutable('1 month ago'));
$command = new SetPersonsCommand([$person]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($person) {
return $arg instanceof PersonHistory && $arg->getPerson() === $person;
}))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertNull($history->getEndDate());
self::assertCount(1, $ticket->getPersons());
}
public function testHandleRemoveExistingPerson(): void
{
$ticket = new Ticket();
$person = new Person();
$person2 = new Person();
$history = new PersonHistory($person, $ticket, new \DateTimeImmutable('1 month ago'));
$command = new SetPersonsCommand([$person2]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($person2) {
return $arg instanceof PersonHistory && $arg->getPerson() === $person2;
}))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertNotNull($history->getEndDate());
self::assertContains($person2, $ticket->getPersons());
}
public function testAddingDoublingPersonsDoesNotCreateDoubleHistories(): void
{
$ticket = new Ticket();
$person = new Person();
$command = new SetPersonsCommand([$person, $person]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($person) {
return $arg instanceof PersonHistory && $arg->getPerson() === $person;
}))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertCount(1, $ticket->getPersons());
}
private function buildHandler(EntityManagerInterface $entityManager): SetPersonsCommandHandler
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
return new SetPersonsCommandHandler(new MockClock(), $entityManager, $security->reveal());
}
}