chill-bundles/src/Bundle/ChillTicketBundle/tests/Controller/ReplaceMotiveControllerTest.php
Julien Fastré 670b8eb82b
Implement functionality to replace ticket's motive
The commit introduces several features related to ticket motive management in the Chill-TicketBundle:
- Adds capability to replace a ticket's motive with a new one.
- Provides ticket motive history management features.
- Implements relevant changes in Controller, Action Handler, and Entity levels.
- Incorporates new API endpoints and updates the API specification file for the new feature.
- Includes tests to ensure the new functionality works as expected.
2024-04-18 13:13:08 +02:00

114 lines
3.1 KiB
PHP

<?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\Handler\ReplaceMotiveCommandHandler;
use Chill\TicketBundle\Controller\ReplaceMotiveController;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @internal
*
* @coversNothing
*/
class ReplaceMotiveControllerTest extends KernelTestCase
{
use ProphecyTrait;
private SerializerInterface $serializer;
private ValidatorInterface $validator;
protected function setUp(): void
{
self::bootKernel();
$this->serializer = self::getContainer()->get(SerializerInterface::class);
$this->validator = self::getContainer()->get(ValidatorInterface::class);
}
protected function tearDown(): void
{
self::ensureKernelShutdown();
}
/**
* @dataProvider generateMotiveId
*/
public function testAddValidMotive(int $motiveId): void
{
$ticket = new Ticket();
$payload = <<<JSON
{"motive": {"type": "ticket_motive", "id": {$motiveId}}}
JSON;
$request = new Request(content: $payload);
$controller = $this->buildController();
$response = $controller($ticket, $request);
self::assertEquals(201, $response->getStatusCode());
}
private function buildController(): ReplaceMotiveController
{
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::type(MotiveHistory::class))->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();
$handler = new ReplaceMotiveCommandHandler(
new MockClock(),
$entityManager->reveal()
);
return new ReplaceMotiveController(
$security->reveal(),
$handler,
$this->serializer,
$this->validator,
$entityManager->reveal(),
);
}
public static function generateMotiveId(): iterable
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$motive = $em->createQuery('SELECT m FROM '.Motive::class.' m ')
->setMaxResults(1)
->getOneOrNullResult();
if (null === $motive) {
throw new \RuntimeException('the motive table seems to be empty');
}
self::ensureKernelShutdown();
yield [$motive->getId()];
}
}