chill-bundles/src/Bundle/ChillTicketBundle/tests/Controller/UpdateCommentDeletedStatusControllerTest.php

115 lines
3.7 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\Comment\Handler\UpdateCommentDeletedStatusCommandHandlerInterface;
use Chill\TicketBundle\Controller\UpdateCommentDeletedStatusController;
use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Security\Voter\CommentVoter;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @internal
*
* @coversNothing
*/
class UpdateCommentDeletedStatusControllerTest extends KernelTestCase
{
use ProphecyTrait;
private SerializerInterface $serializer;
protected function setUp(): void
{
self::bootKernel();
$this->serializer = self::getContainer()->get(SerializerInterface::class);
}
public function testDeleteComment(): void
{
$ticket = new Ticket();
$comment = new Comment('content', $ticket);
$controller = $this->buildController(willFlush: true, isGranted: true, comment: $comment);
$response = $controller->deleteComment($comment);
self::assertEquals(200, $response->getStatusCode());
}
public function testRestoreComment(): void
{
$ticket = new Ticket();
$comment = new Comment('content', $ticket);
$controller = $this->buildController(willFlush: true, isGranted: true, comment: $comment);
$response = $controller->restoreComment($comment);
self::assertEquals(200, $response->getStatusCode());
}
public function testDeleteCommentWithoutAuthorization(): void
{
$ticket = new Ticket();
$comment = new Comment('content', $ticket);
$controller = $this->buildController(willFlush: false, isGranted: false, comment: $comment);
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('You are not allowed to edit this comment.');
$controller->deleteComment($comment);
}
public function testRestoreCommentWithoutAuthorization(): void
{
$ticket = new Ticket();
$comment = new Comment('content', $ticket);
$controller = $this->buildController(willFlush: false, isGranted: false, comment: $comment);
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('You are not allowed to edit this comment.');
$controller->restoreComment($comment);
}
private function buildController(bool $willFlush, bool $isGranted, Comment $comment): UpdateCommentDeletedStatusController
{
$security = $this->prophesize(Security::class);
$security->isGranted(CommentVoter::EDIT, $comment)->willReturn($isGranted);
$entityManager = $this->prophesize(EntityManagerInterface::class);
if ($willFlush) {
$entityManager->flush()->shouldBeCalled();
}
$commandHandler = $this->prophesize(UpdateCommentDeletedStatusCommandHandlerInterface::class);
if ($isGranted && $willFlush) {
$commandHandler->handle($comment, Argument::any())->shouldBeCalled();
}
return new UpdateCommentDeletedStatusController(
$security->reveal(),
$this->serializer,
$commandHandler->reveal(),
$entityManager->reveal(),
);
}
}