Add Events when a ticket is updated, and trigger asynchronously post update events

This commit is contained in:
2025-10-16 12:34:12 +00:00
parent 4765d4fe28
commit 98902bdeb8
19 changed files with 604 additions and 17 deletions

View File

@@ -16,9 +16,13 @@ use Chill\TicketBundle\Action\Ticket\Handler\ChangeEmergencyStateCommandHandler;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\EmergencyStatusHistory;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\EmergencyStatusUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateEvent;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @internal
@@ -36,7 +40,10 @@ final class ChangeEmergencyStateCommandHandlerTest extends TestCase
// Create a YES emergency status history
new EmergencyStatusHistory(EmergencyStatusEnum::YES, $ticket);
$handler = new ChangeEmergencyStateCommandHandler(new MockClock());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::type(EmergencyStatusUpdateEvent::class), TicketUpdateEvent::class)->shouldNotBeCalled();
$handler = new ChangeEmergencyStateCommandHandler(new MockClock(), $eventDispatcher->reveal());
$command = new ChangeEmergencyStateCommand(EmergencyStatusEnum::YES);
$result = $handler->__invoke($ticket, $command);
@@ -57,7 +64,17 @@ final class ChangeEmergencyStateCommandHandlerTest extends TestCase
// Create a YES emergency status history
new EmergencyStatusHistory(EmergencyStatusEnum::YES, $ticket);
$handler = new ChangeEmergencyStateCommandHandler(new MockClock());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(
fn ($e) => $e instanceof EmergencyStatusUpdateEvent
&& EmergencyStatusEnum::YES === $e->previousEmergencyStatus
&& EmergencyStatusEnum::NO === $e->newEmergencyStatus
),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = new ChangeEmergencyStateCommandHandler(new MockClock(), $eventDispatcher->reveal());
$command = new ChangeEmergencyStateCommand(EmergencyStatusEnum::NO);
$result = $handler->__invoke($ticket, $command);
@@ -90,7 +107,17 @@ final class ChangeEmergencyStateCommandHandlerTest extends TestCase
// Create a NO emergency status history
new EmergencyStatusHistory(EmergencyStatusEnum::NO, $ticket);
$handler = new ChangeEmergencyStateCommandHandler(new MockClock());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(
fn ($e) => $e instanceof EmergencyStatusUpdateEvent
&& EmergencyStatusEnum::NO === $e->previousEmergencyStatus
&& EmergencyStatusEnum::YES === $e->newEmergencyStatus
),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = new ChangeEmergencyStateCommandHandler(new MockClock(), $eventDispatcher->reveal());
$command = new ChangeEmergencyStateCommand(EmergencyStatusEnum::YES);
$result = $handler->__invoke($ticket, $command);

View File

@@ -19,16 +19,19 @@ use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\MotiveUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateEvent;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @internal
*
* @coversNothing
* @covers \Chill\TicketBundle\Action\Ticket\Handler\ReplaceMotiveCommandHandler
*/
final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
{
@@ -37,14 +40,18 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
private function buildHandler(
EntityManagerInterface $entityManager,
?ChangeEmergencyStateCommandHandler $changeEmergencyStateCommandHandler = null,
?EventDispatcherInterface $eventDispatcher = null,
): ReplaceMotiveCommandHandler {
$clock = new MockClock();
if (null === $changeEmergencyStateCommandHandler) {
$changeEmergencyStateCommandHandler = $this->prophesize(ChangeEmergencyStateCommandHandler::class)->reveal();
}
if (null === $eventDispatcher) {
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class)->reveal();
}
return new ReplaceMotiveCommandHandler($clock, $entityManager, $changeEmergencyStateCommandHandler);
return new ReplaceMotiveCommandHandler($clock, $entityManager, $changeEmergencyStateCommandHandler, $eventDispatcher);
}
public function testHandleOnTicketWithoutMotive(): void
@@ -61,7 +68,16 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
return $arg->getMotive() === $motive;
}))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(fn ($event) => $event instanceof MotiveUpdateEvent
&& $event->newMotive === $motive
&& null === $event->previousMotive
&& $event->hasChanges()),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), null, $eventDispatcher->reveal());
$handler->handle($ticket, new ReplaceMotiveCommand($motive));
@@ -83,7 +99,17 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
return $arg->getMotive() === $motive;
}))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$previous = $history->getMotive();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(fn ($event) => $event instanceof MotiveUpdateEvent
&& $event->newMotive === $motive
&& $previous === $event->previousMotive
&& $event->hasChanges()),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), null, $eventDispatcher->reveal());
$handler->handle($ticket, new ReplaceMotiveCommand($motive));
@@ -106,7 +132,10 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
return $arg->getMotive() === $motive;
}))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::any(), TicketUpdateEvent::class)->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), null, $eventDispatcher->reveal());
$handler->handle($ticket, new ReplaceMotiveCommand($motive));
@@ -134,10 +163,15 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
Argument::that(fn (ChangeEmergencyStateCommand $command) => EmergencyStatusEnum::YES === $command->newEmergencyStatus)
)->shouldBeCalled();
// Expect event dispatch for motive update
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::type(MotiveUpdateEvent::class), TicketUpdateEvent::class)->shouldBeCalled();
// Create the handler with our mocks
$handler = $this->buildHandler(
$entityManager->reveal(),
$changeEmergencyStateCommandHandler->reveal()
$changeEmergencyStateCommandHandler->reveal(),
$eventDispatcher->reveal()
);
// Handle the command
@@ -166,10 +200,15 @@ final class ReplaceMotiveCommandHandlerTest extends KernelTestCase
Argument::cetera()
)->shouldNotBeCalled();
// Expect event dispatch for motive update
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::type(MotiveUpdateEvent::class), TicketUpdateEvent::class)->shouldBeCalled();
// Create the handler with our mocks
$handler = $this->buildHandler(
$entityManager->reveal(),
$changeEmergencyStateCommandHandler->reveal()
$changeEmergencyStateCommandHandler->reveal(),
$eventDispatcher->reveal()
);
// Handle the command

View File

@@ -17,12 +17,15 @@ use Chill\TicketBundle\Action\Ticket\Handler\SetPersonsCommandHandler;
use Chill\TicketBundle\Action\Ticket\SetPersonsCommand;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\PersonsUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateEvent;
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;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @internal
@@ -42,7 +45,16 @@ final class SetPersonsCommandHandlerTest extends TestCase
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person1))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $group1))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(fn ($arg) => $arg instanceof PersonsUpdateEvent
&& in_array($person1, $arg->personsAdded, true)
&& in_array($group1, $arg->personsAdded, true)
&& [] === $arg->personsRemoved),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), $eventDispatcher->reveal());
$handler->handle($ticket, $command);
@@ -59,7 +71,15 @@ final class SetPersonsCommandHandlerTest extends TestCase
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(
fn ($arg) => $arg instanceof PersonsUpdateEvent
),
TicketUpdateEvent::class
)->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), $eventDispatcher->reveal());
$handler->handle($ticket, $command);
@@ -78,7 +98,17 @@ final class SetPersonsCommandHandlerTest extends TestCase
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person2))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(
fn ($arg) => $arg instanceof PersonsUpdateEvent
&& in_array($person, $arg->personsRemoved, true) && 1 === count($arg->personsRemoved)
&& in_array($person2, $arg->personsAdded, true) && 1 === count($arg->personsAdded)
),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), $eventDispatcher->reveal());
$handler->handle($ticket, $command);
@@ -95,18 +125,28 @@ final class SetPersonsCommandHandlerTest extends TestCase
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(
Argument::that(
fn ($arg) => $arg instanceof PersonsUpdateEvent
&& in_array($person, $arg->personsAdded, true) && 1 === count($arg->personsAdded)
&& [] === $arg->personsRemoved
),
TicketUpdateEvent::class
)->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal(), $eventDispatcher->reveal());
$handler->handle($ticket, $command);
self::assertCount(1, $ticket->getPersons());
}
private function buildHandler(EntityManagerInterface $entityManager): SetPersonsCommandHandler
private function buildHandler(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher): SetPersonsCommandHandler
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
return new SetPersonsCommandHandler(new MockClock(), $entityManager, $security->reveal());
return new SetPersonsCommandHandler(new MockClock(), $entityManager, $security->reveal(), $eventDispatcher);
}
}

View File

@@ -0,0 +1,58 @@
<?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\Event\EventSubscriber;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\EventSubscriber\GeneratePostUpdateTicketEventSubscriber;
use Chill\TicketBundle\Event\TicketUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateKindEnum;
use Chill\TicketBundle\Messenger\PostTicketUpdateMessage;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @covers \Chill\TicketBundle\Event\EventSubscriber\GeneratePostUpdateTicketEventSubscriber
*
* @internal
*/
class GeneratePostUpdateTicketEventSubscriberTest extends TestCase
{
use ProphecyTrait;
public function testOnTicketUpdate(): void
{
$ticket = new Ticket();
$reflection = new \ReflectionClass(Ticket::class);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($ticket, 1);
$event = new class (TicketUpdateKindEnum::UPDATE_MOTIVE, $ticket) extends TicketUpdateEvent {};
$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::that(fn ($arg) => $arg instanceof PostTicketUpdateMessage && TicketUpdateKindEnum::UPDATE_MOTIVE === $arg->updateKind && 1 === $arg->ticketId))
->will(fn ($args) => new Envelope($args[0]))
->shouldBeCalled();
$eventSubscriber = new GeneratePostUpdateTicketEventSubscriber($messageBus->reveal());
$eventSubscriber->onTicketUpdate($event);
$kernel = $this->prophesize(KernelInterface::class);
$terminate = new TerminateEvent($kernel->reveal(), new Request(), new Response());
$eventSubscriber->onKernelTerminate($terminate);
}
}

View File

@@ -0,0 +1,92 @@
<?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\Messenger\Handler;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\PostTicketUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateKindEnum;
use Chill\TicketBundle\Messenger\PostTicketUpdateMessage;
use Chill\TicketBundle\Messenger\Handler\PostTicketUpdateMessageHandler;
use Chill\TicketBundle\Repository\TicketRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @covers \Chill\TicketBundle\Messenger\Handler\PostTicketUpdateMessageHandler
*
* @internal
*/
class PostTicketUpdateMessageHandlerTest extends TestCase
{
use ProphecyTrait;
public function testDispatchesEventWhenTicketExists(): void
{
// Arrange: a Ticket with an ID
$ticket = new Ticket();
$reflection = new \ReflectionClass(Ticket::class);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($ticket, 123);
$message = new PostTicketUpdateMessage($ticket, TicketUpdateKindEnum::UPDATE_MOTIVE);
// Mock repository to return the Ticket when searching by id
$ticketRepository = $this->prophesize(TicketRepositoryInterface::class);
$ticketRepository->find(123)->willReturn($ticket)->shouldBeCalledOnce();
// Expect the dispatcher to dispatch a PostTicketUpdateEvent with correct data
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::that(fn ($event) => $event instanceof PostTicketUpdateEvent
&& TicketUpdateKindEnum::UPDATE_MOTIVE === $event->updateKind
&& $event->ticket === $ticket))
->will(fn ($args) => $args[0])
->shouldBeCalledOnce();
$handler = new PostTicketUpdateMessageHandler($eventDispatcher->reveal(), $ticketRepository->reveal());
// Act
$handler($message);
// Assert: expectations asserted by Prophecy
self::assertTrue(true);
}
public function testThrowsWhenTicketNotFound(): void
{
// Arrange: a Ticket with an ID for the message, but repository will return null
$ticket = new Ticket();
$reflection = new \ReflectionClass(Ticket::class);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($ticket, 999);
$message = new PostTicketUpdateMessage($ticket, TicketUpdateKindEnum::UPDATE_MOTIVE);
$ticketRepository = $this->prophesize(TicketRepositoryInterface::class);
$ticketRepository->find(999)->willReturn(null)->shouldBeCalledOnce();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();
$handler = new PostTicketUpdateMessageHandler($eventDispatcher->reveal(), $ticketRepository->reveal());
// Assert: exception is thrown
$this->expectException(UnrecoverableMessageHandlingException::class);
$this->expectExceptionMessage('Ticket not found');
// Act
$handler($message);
}
}