chill-bundles/src/Bundle/ChillTicketBundle/tests/Controller/AddCommentControllerTest.php
Julien Fastré 613ee8b186
Add functionality to add comments to tickets
A new controller, 'AddCommentController', has been added. This controller implements the 'AddCommentCommandHandler', allowing users to add comments to tickets. Additionally, corresponding test cases were implemented. The Ticket entity was also updated to accept and manage comments. API endpoint specs were updated to reflect these changes.
2024-04-18 21:57:55 +02:00

105 lines
3.0 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\AddCommentCommandHandler;
use Chill\TicketBundle\Controller\AddCommentController;
use Chill\TicketBundle\Entity\Comment;
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\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @internal
*
* @coversNothing
*/
class AddCommentControllerTest extends KernelTestCase
{
use ProphecyTrait;
private SerializerInterface $serializer;
private ValidatorInterface $validator;
protected function setUp(): void
{
self::bootKernel();
$this->validator = self::getContainer()->get(ValidatorInterface::class);
$this->serializer = self::getContainer()->get(SerializerInterface::class);
}
public function testAddComment(): void
{
$controller = $this->buildController(willFlush: true);
$ticket = new Ticket();
$request = new Request(content: <<<'JSON'
{"content": "test"}
JSON);
$response = $controller->__invoke($ticket, $request);
self::assertEquals(201, $response->getStatusCode());
}
public function testAddCommentWithBlankContent(): void
{
$controller = $this->buildController(willFlush: false);
$ticket = new Ticket();
$request = new Request(content: <<<'JSON'
{"content": ""}
JSON);
$response = $controller->__invoke($ticket, $request);
self::assertEquals(422, $response->getStatusCode());
$request = new Request(content: <<<'JSON'
{"content": null}
JSON);
$response = $controller->__invoke($ticket, $request);
self::assertEquals(422, $response->getStatusCode());
}
private function buildController(bool $willFlush): AddCommentController
{
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$entityManager = $this->prophesize(EntityManagerInterface::class);
if ($willFlush) {
$entityManager->persist(Argument::type(Comment::class))->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();
}
$commandHandler = new AddCommentCommandHandler($entityManager->reveal());
return new AddCommentController(
$security->reveal(),
$this->serializer,
$this->validator,
$commandHandler,
$entityManager->reveal(),
);
}
}