Add api endpoint to open and close ticket

This commit is contained in:
2025-06-20 15:42:43 +00:00
parent 95975fae55
commit c72432efae
21 changed files with 685 additions and 84 deletions

View File

@@ -0,0 +1,118 @@
<?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\TicketBundle\Action\Ticket\ChangeStateCommand;
use Chill\TicketBundle\Action\Ticket\Handler\ChangeStateCommandHandler;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\StateHistory;
use Chill\TicketBundle\Entity\Ticket;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
/**
* @internal
*
* @coversNothing
*/
final class ChangeStateCommandHandlerTest extends TestCase
{
use ProphecyTrait;
public function testInvokeWithAlreadyClosedTicket(): void
{
$ticket = new Ticket();
// Create a closed state history
new StateHistory(StateEnum::CLOSED, $ticket);
$handler = new ChangeStateCommandHandler(new MockClock());
$command = new ChangeStateCommand(StateEnum::CLOSED);
$result = $handler->__invoke($ticket, $command);
// Assert that the ticket is returned unchanged
$this->assertSame($ticket, $result);
$this->assertSame(StateEnum::CLOSED, $ticket->getState());
// Assert that no new state history was created
$stateHistories = $ticket->getStateHistories();
$this->assertCount(1, $stateHistories);
}
public function testInvokeWithOpenTicketToClose(): void
{
$ticket = new Ticket();
// Create an open state history
new StateHistory(StateEnum::OPEN, $ticket);
$handler = new ChangeStateCommandHandler(new MockClock());
$command = new ChangeStateCommand(StateEnum::CLOSED);
$result = $handler->__invoke($ticket, $command);
// Assert that the ticket is returned
$this->assertSame($ticket, $result);
// Assert that the ticket state is now closed
$this->assertSame(StateEnum::CLOSED, $ticket->getState());
// Assert that the old state history was ended and a new one was created
$stateHistories = $ticket->getStateHistories();
$this->assertCount(2, $stateHistories);
// The first state history should be ended
$openStateHistory = $stateHistories->first();
$this->assertNotNull($openStateHistory->getEndDate());
$this->assertSame(StateEnum::OPEN, $openStateHistory->getState());
// The last state history should be closed and active
$closedStateHistory = $stateHistories->last();
$this->assertNull($closedStateHistory->getEndDate());
$this->assertSame(StateEnum::CLOSED, $closedStateHistory->getState());
}
public function testInvokeWithClosedTicketToOpen(): void
{
$ticket = new Ticket();
// Create a closed state history
new StateHistory(StateEnum::CLOSED, $ticket);
$handler = new ChangeStateCommandHandler(new MockClock());
$command = new ChangeStateCommand(StateEnum::OPEN);
$result = $handler->__invoke($ticket, $command);
// Assert that the ticket is returned
$this->assertSame($ticket, $result);
// Assert that the ticket state is now open
$this->assertSame(StateEnum::OPEN, $ticket->getState());
// Assert that the old state history was ended and a new one was created
$stateHistories = $ticket->getStateHistories();
$this->assertCount(2, $stateHistories);
// The first state history should be ended
$closedStateHistory = $stateHistories->first();
$this->assertNotNull($closedStateHistory->getEndDate());
$this->assertSame(StateEnum::CLOSED, $closedStateHistory->getState());
// The last state history should be open and active
$openStateHistory = $stateHistories->last();
$this->assertNull($openStateHistory->getEndDate());
$this->assertSame(StateEnum::OPEN, $openStateHistory->getState());
}
}

View File

@@ -13,8 +13,10 @@ namespace Chill\TicketBundle\Tests\Action\Ticket\Handler;
use Chill\TicketBundle\Action\Ticket\CreateTicketCommand;
use Chill\TicketBundle\Action\Ticket\Handler\CreateTicketCommandHandler;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\Ticket;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MockClock;
/**
* @internal
@@ -25,7 +27,7 @@ class CreateTicketCommandHandlerTest extends TestCase
{
private function getHandler(): CreateTicketCommandHandler
{
return new CreateTicketCommandHandler();
return new CreateTicketCommandHandler(new MockClock());
}
public function testHandleWithoutReference(): void
@@ -35,6 +37,7 @@ class CreateTicketCommandHandlerTest extends TestCase
self::assertInstanceOf(Ticket::class, $actual);
self::assertEquals('', $actual->getExternalRef());
self::assertEquals(StateEnum::OPEN, $actual->getState());
}
public function testHandleWithReference(): void
@@ -44,5 +47,6 @@ class CreateTicketCommandHandlerTest extends TestCase
self::assertInstanceOf(Ticket::class, $actual);
self::assertEquals($ref, $actual->getExternalRef());
self::assertEquals(StateEnum::OPEN, $actual->getState());
}
}

View File

@@ -39,12 +39,8 @@ final class SetAddressesCommandHandlerTest extends TestCase
$command = new SetAddresseesCommand([$user1 = new User(), $group1 = new UserGroup()]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($user1) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $user1;
}))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(function ($arg) use ($group1) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group1;
}))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof AddresseeHistory && $arg->getAddressee() === $user1))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof AddresseeHistory && $arg->getAddressee() === $group1))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
@@ -61,9 +57,7 @@ final class SetAddressesCommandHandlerTest extends TestCase
$command = new SetAddresseesCommand([$user]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($user) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $user;
}))->shouldNotBeCalled();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof AddresseeHistory && $arg->getAddressee() === $user))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
@@ -82,9 +76,7 @@ final class SetAddressesCommandHandlerTest extends TestCase
$command = new SetAddresseesCommand([$group]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($group) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group;
}))->shouldBeCalled();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof AddresseeHistory && $arg->getAddressee() === $group))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
@@ -101,9 +93,7 @@ final class SetAddressesCommandHandlerTest extends TestCase
$command = new SetAddresseesCommand([$group, $group]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($group) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group;
}))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof AddresseeHistory && $arg->getAddressee() === $group))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());

View File

@@ -39,12 +39,8 @@ final class SetPersonsCommandHandlerTest extends TestCase
$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();
$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());
@@ -61,9 +57,7 @@ final class SetPersonsCommandHandlerTest extends TestCase
$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();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
@@ -82,9 +76,7 @@ final class SetPersonsCommandHandlerTest extends TestCase
$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();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person2))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
@@ -101,9 +93,7 @@ final class SetPersonsCommandHandlerTest extends TestCase
$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();
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonHistory && $arg->getPerson() === $person))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());

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\Controller;
use Chill\TicketBundle\Action\Ticket\ChangeStateCommand;
use Chill\TicketBundle\Action\Ticket\Handler\ChangeStateCommandHandler;
use Chill\TicketBundle\Controller\ChangeStateApiController;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @internal
*
* @coversNothing
*/
final class ChangeStateApiControllerTest extends TestCase
{
use ProphecyTrait;
public function testCloseWithoutPermission(): void
{
$ticket = new Ticket();
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(false);
$changeStateCommandHandler = $this->prophesize(ChangeStateCommandHandler::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$serializer = $this->prophesize(SerializerInterface::class);
$controller = new ChangeStateApiController(
$changeStateCommandHandler->reveal(),
$security->reveal(),
$entityManager->reveal(),
$serializer->reveal(),
);
$this->expectException(AccessDeniedHttpException::class);
$controller->close($ticket);
}
public function testCloseWithPermission(): void
{
$ticket = new Ticket();
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$changeStateCommandHandler = $this->prophesize(ChangeStateCommandHandler::class);
$changeStateCommandHandler->__invoke(
$ticket,
Argument::that(fn (ChangeStateCommand $command) => StateEnum::CLOSED === $command->newState)
)->willReturn($ticket);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->flush()->shouldBeCalled();
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize($ticket, 'json', ['groups' => ['read']])
->willReturn('{}')
->shouldBeCalled();
$controller = new ChangeStateApiController(
$changeStateCommandHandler->reveal(),
$security->reveal(),
$entityManager->reveal(),
$serializer->reveal(),
);
$response = $controller->close($ticket);
$this->assertInstanceOf(JsonResponse::class, $response);
}
public function testOpenWithoutPermission(): void
{
$ticket = new Ticket();
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(false);
$changeStateCommandHandler = $this->prophesize(ChangeStateCommandHandler::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$serializer = $this->prophesize(SerializerInterface::class);
$controller = new ChangeStateApiController(
$changeStateCommandHandler->reveal(),
$security->reveal(),
$entityManager->reveal(),
$serializer->reveal(),
);
$this->expectException(AccessDeniedHttpException::class);
$controller->open($ticket);
}
public function testOpenWithPermission(): void
{
$ticket = new Ticket();
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$changeStateCommandHandler = $this->prophesize(ChangeStateCommandHandler::class);
$changeStateCommandHandler->__invoke(
$ticket,
Argument::that(fn (ChangeStateCommand $command) => StateEnum::OPEN === $command->newState)
)->willReturn($ticket);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->flush()->shouldBeCalled();
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize($ticket, 'json', ['groups' => ['read']])
->willReturn('{}')
->shouldBeCalled();
$controller = new ChangeStateApiController(
$changeStateCommandHandler->reveal(),
$security->reveal(),
$entityManager->reveal(),
$serializer->reveal(),
);
$response = $controller->open($ticket);
$this->assertInstanceOf(JsonResponse::class, $response);
}
}

View File

@@ -81,7 +81,7 @@ class SetAddresseesControllerTest extends KernelTestCase
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::arrayHasKey('violations', $asArray);
self::arrayHasKey('violations');
self::assertGreaterThan(0, count($asArray['violations']));
}
@@ -147,7 +147,7 @@ class SetAddresseesControllerTest extends KernelTestCase
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::arrayHasKey('violations', $asArray);
self::arrayHasKey('violations');
self::assertGreaterThan(0, count($asArray['violations']));
}

View File

@@ -114,25 +114,23 @@ class TicketTest extends KernelTestCase
public function testGetState(): void
{
$ticket = new Ticket();
$openState = new StateEnum(StateEnum::OPEN, ['en' => 'Open', 'fr' => 'Ouvert']);
// Initially, the ticket has no state
self::assertNull($ticket->getState());
// Create a state history entry with the open state
$history = new StateHistory($openState, $ticket);
$history = new StateHistory(StateEnum::OPEN, $ticket);
// Verify that the ticket now has the open state
self::assertSame($openState, $ticket->getState());
self::assertSame(StateEnum::OPEN, $ticket->getState());
self::assertCount(1, $ticket->getStateHistories());
// Change the state to closed
$closedState = new StateEnum(StateEnum::CLOSED, ['en' => 'Closed', 'fr' => 'Fermé']);
$history->setEndDate(new \DateTimeImmutable());
$history2 = new StateHistory($closedState, $ticket);
$history2 = new StateHistory(StateEnum::CLOSED, $ticket);
// Verify that the ticket now has the closed state
self::assertCount(2, $ticket->getStateHistories());
self::assertSame($closedState, $ticket->getState());
self::assertSame(StateEnum::CLOSED, $ticket->getState());
}
}

View File

@@ -19,6 +19,8 @@ use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\StateHistory;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Serializer\Normalizer\TicketNormalizer;
use Prophecy\Argument;
@@ -41,7 +43,6 @@ class TicketNormalizerTest extends KernelTestCase
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) {
@@ -61,23 +62,67 @@ class TicketNormalizerTest extends KernelTestCase
public static function provideTickets(): iterable
{
$t = new Ticket();
// added by action
new StateHistory(StateEnum::OPEN, $t, new \DateTimeImmutable('2024-06-16T00:00:00Z'));
// those are added by doctrine listeners
$t->setCreatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$t->setCreatedBy(new User());
$t->setUpdatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$t->setUpdatedBy(new User());
yield [
// this a nearly empty ticket
new Ticket(),
$t,
[
'type' => 'ticket_ticket',
'createdAt' => $t->getCreatedAt()?->getTimestamp(),
'createdBy' => ['user'],
'id' => null,
'externalRef' => '',
'currentPersons' => [],
'currentAddressees' => [],
'currentInputs' => [],
'currentMotive' => null,
'history' => [],
'history' => [
[
'event_type' => 'create_ticket',
'at' => 1718495999,
'by' => [
0 => 'user',
],
'data' => [],
],
[
'event_type' => 'state_change',
'at' => 1718495999,
'by' => [
0 => 'user',
],
'data' => [
'new_state' => 'open',
],
],
],
'currentState' => 'open',
'updatedAt' => $t->getUpdatedAt()->getTimestamp(),
'updatedBy' => ['user'],
],
];
// ticket with more features
$ticket = new Ticket();
// added by action
new StateHistory(StateEnum::OPEN, $ticket, new \DateTimeImmutable('2024-06-16T00:00:00Z'));
// those are added by doctrine listeners
$ticket->setCreatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$ticket->setCreatedBy(new User());
$ticket->setUpdatedAt(new \DateTimeImmutable('2024-06-16T00:00:00Z'));
$ticket->setUpdatedBy(new User());
$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'));
@@ -92,6 +137,8 @@ class TicketNormalizerTest extends KernelTestCase
$ticket,
[
'type' => 'ticket_ticket',
'createdAt' => $ticket->getCreatedAt()?->getTimestamp(),
'createdBy' => ['user'],
'id' => null,
'externalRef' => '2134',
'currentPersons' => ['embedded'],
@@ -100,12 +147,18 @@ class TicketNormalizerTest extends KernelTestCase
'currentMotive' => ['type' => 'motive', 'id' => 0],
'history' => [
['event_type' => 'add_person'],
['event_type' => 'persons_state'],
['event_type' => 'set_motive'],
['event_type' => 'add_comment'],
['event_type' => 'add_addressee'],
['event_type' => 'remove_addressee'],
['event_type' => 'add_addressee'],
['event_type' => 'addressees_state'],
['event_type' => 'addressees_state'],
['event_type' => 'addressees_state'],
['event_type' => 'create_ticket'],
['event_type' => 'state_change'],
],
'currentState' => 'open',
'updatedAt' => $ticket->getUpdatedAt()->getTimestamp(),
'updatedBy' => ['user'],
],
];
}
@@ -126,9 +179,7 @@ class TicketNormalizerTest extends KernelTestCase
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');
});
)->will(fn ($args) => array_fill(0, count($args[0]), 'embedded'));
// array of event type
$normalizer->normalize(
@@ -144,10 +195,28 @@ class TicketNormalizerTest extends KernelTestCase
return $events;
});
// array of persons
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 1 === count($arg) && array_key_exists('persons', $arg)),
'json',
['groups' => 'read']
)->will(fn ($args): array => ['persons' => []]);
// array of addresses
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 1 === count($arg) && array_key_exists('addressees', $arg)),
'json',
['groups' => 'read']
)->will(fn ($args): array => ['addressees' => []]);
// state data
$normalizer->normalize(
Argument::that(fn ($arg) => is_array($arg) && 1 === count($arg) && array_key_exists('new_state', $arg)),
'json',
['groups' => 'read']
)->will(fn ($args): array => $args[0]);
// datetime
$normalizer->normalize(Argument::type(\DateTimeImmutable::class), 'json', Argument::type('array'))
->will(function ($args) { return $args[0]->getTimestamp(); });
->will(fn ($args) => $args[0]->getTimestamp());
// user
$normalizer->normalize(Argument::type(User::class), 'json', Argument::type('array'))
->willReturn(['user']);