mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-01-16 14:21:23 +00:00
[Ticket] Add documents to Motive
This commit is contained in:
@@ -11,20 +11,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\TicketBundle\Tests\Controller;
|
||||
|
||||
use Chill\TicketBundle\Action\Ticket\Handler\ChangeEmergencyStateCommandHandler;
|
||||
use Chill\TicketBundle\Action\Ticket\Handler\ReplaceMotiveCommandHandler;
|
||||
use Chill\TicketBundle\Controller\ReplaceMotiveController;
|
||||
use Chill\TicketBundle\Action\Ticket\ReplaceMotiveCommand;
|
||||
use Chill\TicketBundle\Entity\Motive;
|
||||
use Chill\TicketBundle\Entity\MotiveHistory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Chill\TicketBundle\Controller\ReplaceMotiveController;
|
||||
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\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
@@ -32,88 +31,62 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ReplaceMotiveControllerTest extends KernelTestCase
|
||||
class ReplaceMotiveControllerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private SerializerInterface $serializer;
|
||||
|
||||
private ValidatorInterface $validator;
|
||||
|
||||
protected function setUp(): void
|
||||
private function buildController(Ticket $ticket, string $body, Motive $motive): ReplaceMotiveController
|
||||
{
|
||||
self::bootKernel();
|
||||
$this->serializer = self::getContainer()->get(SerializerInterface::class);
|
||||
$this->validator = self::getContainer()->get(ValidatorInterface::class);
|
||||
$command = new ReplaceMotiveCommand($motive);
|
||||
|
||||
// Mock Security
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('ROLE_USER')->willReturn(true);
|
||||
|
||||
// Mock EntityManager
|
||||
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
||||
$entityManager->flush()->shouldBeCalled();
|
||||
|
||||
$replaceMotiveCommandHandler = $this->prophesize(ReplaceMotiveCommandHandler::class);
|
||||
$replaceMotiveCommandHandler->handle($ticket, $command)->shouldBeCalled();
|
||||
|
||||
// Mock Validator
|
||||
$validator = $this->prophesize(ValidatorInterface::class);
|
||||
$validator->validate($command)
|
||||
->shouldBeCalled()
|
||||
->willReturn(new ConstraintViolationList([]));
|
||||
|
||||
$serializer = $this->prophesize(SerializerInterface::class);
|
||||
$serializer->deserialize($body, ReplaceMotiveCommand::class, 'json', [AbstractNormalizer::GROUPS => ['write']])
|
||||
->willReturn($command);
|
||||
$serializer->serialize($ticket, 'json', ['groups' => ['read']])
|
||||
->shouldBeCalled()
|
||||
->willReturn('{"type": "ticket", "id": 1}');
|
||||
|
||||
return new ReplaceMotiveController(
|
||||
$security->reveal(),
|
||||
$replaceMotiveCommandHandler->reveal(),
|
||||
$serializer->reveal(),
|
||||
$validator->reveal(),
|
||||
$entityManager->reveal()
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
self::ensureKernelShutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateMotiveId
|
||||
*/
|
||||
public function testAddValidMotive(int $motiveId): void
|
||||
public function testAddValidMotive(): void
|
||||
{
|
||||
$ticket = new Ticket();
|
||||
$payload = <<<JSON
|
||||
{"motive": {"type": "ticket_motive", "id": {$motiveId}}}
|
||||
$motive = new Motive();
|
||||
|
||||
$payload = <<<'JSON'
|
||||
{"motive": {"type": "ticket_motive", "id": 1}}
|
||||
JSON;
|
||||
|
||||
$request = new Request(content: $payload);
|
||||
|
||||
$controller = $this->buildController();
|
||||
$controller = $this->buildController($ticket, $payload, $motive);
|
||||
|
||||
$response = $controller($ticket, $request);
|
||||
|
||||
self::assertEquals(201, $response->getStatusCode());
|
||||
}
|
||||
|
||||
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()];
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
$changeEmergencyCommandHandler = $this->prophesize(ChangeEmergencyStateCommandHandler::class);
|
||||
$changeEmergencyCommandHandler->__invoke(Argument::any(), Argument::any())->shouldBeCalled()
|
||||
->will(fn (array $args) => $args[0]);
|
||||
|
||||
$handler = new ReplaceMotiveCommandHandler(
|
||||
new MockClock(),
|
||||
$entityManager->reveal(),
|
||||
$changeEmergencyCommandHandler->reveal(),
|
||||
);
|
||||
|
||||
return new ReplaceMotiveController(
|
||||
$security->reveal(),
|
||||
$handler,
|
||||
$this->serializer,
|
||||
$this->validator,
|
||||
$entityManager->reveal(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user