mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-10 07:57:46 +00:00
Add ticket filtering "byTicketId"
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
<?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\MainBundle\Pagination\PaginatorFactoryInterface;
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
use Chill\MainBundle\Repository\UserGroupRepositoryInterface;
|
||||
use Chill\MainBundle\Repository\UserRepositoryInterface;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Chill\TicketBundle\Controller\TicketListApiController;
|
||||
use Chill\TicketBundle\Entity\Ticket;
|
||||
use Chill\TicketBundle\Repository\MotiveRepository;
|
||||
use Chill\TicketBundle\Repository\TicketACLAwareRepositoryInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\Clock\MockClock;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @covers \Chill\TicketBundle\Controller\TicketListApiController
|
||||
*/
|
||||
final class TicketListApiControllerByTicketIdTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testListTicketWithByTicketIdFilter(): void
|
||||
{
|
||||
// Mock dependencies
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('ROLE_USER')->willReturn(true);
|
||||
|
||||
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
|
||||
$tickets = [new Ticket(), new Ticket()];
|
||||
$ticketRepository->countTickets(
|
||||
Argument::that(fn ($params) => isset($params['byTicketId']) && 5 === $params['byTicketId'])
|
||||
)
|
||||
->shouldBeCalled()
|
||||
->willReturn(2);
|
||||
$ticketRepository->findTickets(
|
||||
Argument::that(fn ($params) => isset($params['byTicketId']) && 5 === $params['byTicketId']),
|
||||
0,
|
||||
10
|
||||
)->shouldBeCalled()->willReturn($tickets);
|
||||
|
||||
$paginator = $this->prophesize(PaginatorInterface::class);
|
||||
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
|
||||
$paginator->getItemsPerPage()->willReturn(10);
|
||||
|
||||
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
|
||||
$paginatorFactory->create(2)->willReturn($paginator->reveal());
|
||||
|
||||
$serializer = $this->prophesize(SerializerInterface::class);
|
||||
$serializer->serialize(
|
||||
Argument::that(fn (Collection $collection) => $collection->getItems() === $tickets),
|
||||
'json',
|
||||
['groups' => 'read:simple']
|
||||
)->willReturn('{"items":[{},{}],"pagination":{}}');
|
||||
|
||||
$personRepository = $this->prophesize(PersonRepository::class);
|
||||
$motiveRepository = $this->prophesize(MotiveRepository::class);
|
||||
|
||||
// Needed for controller constructor but not used here
|
||||
$userRepository = $this->prophesize(UserRepositoryInterface::class);
|
||||
$userGroupRepository = $this->prophesize(UserGroupRepositoryInterface::class);
|
||||
|
||||
// Create controller
|
||||
$controller = new TicketListApiController(
|
||||
$security->reveal(),
|
||||
$ticketRepository->reveal(),
|
||||
$paginatorFactory->reveal(),
|
||||
$serializer->reveal(),
|
||||
$personRepository->reveal(),
|
||||
$motiveRepository->reveal(),
|
||||
new MockClock(),
|
||||
new ParameterBag(['chill_ticket' => ['ticket' => ['response_time_exceeded_delay' => 'PT12H']]]),
|
||||
$userRepository->reveal(),
|
||||
$userGroupRepository->reveal()
|
||||
);
|
||||
|
||||
// Create request with byTicketId filter
|
||||
$request = new Request(
|
||||
query: ['byTicketId' => '5']
|
||||
);
|
||||
|
||||
// Call controller method
|
||||
$response = $controller->listTicket($request);
|
||||
|
||||
// Assert response
|
||||
$this->assertInstanceOf(JsonResponse::class, $response);
|
||||
$this->assertEquals('{"items":[{},{}],"pagination":{}}', $response->getContent());
|
||||
}
|
||||
|
||||
public function testListTicketWithInvalidByTicketIdThrows(): void
|
||||
{
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
|
||||
// Mock dependencies
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('ROLE_USER')->willReturn(true);
|
||||
|
||||
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
|
||||
|
||||
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
|
||||
$serializer = $this->prophesize(SerializerInterface::class);
|
||||
$personRepository = $this->prophesize(PersonRepository::class);
|
||||
$motiveRepository = $this->prophesize(MotiveRepository::class);
|
||||
$userRepository = $this->prophesize(UserRepositoryInterface::class);
|
||||
$userGroupRepository = $this->prophesize(UserGroupRepositoryInterface::class);
|
||||
|
||||
$controller = new TicketListApiController(
|
||||
$security->reveal(),
|
||||
$ticketRepository->reveal(),
|
||||
$paginatorFactory->reveal(),
|
||||
$serializer->reveal(),
|
||||
$personRepository->reveal(),
|
||||
$motiveRepository->reveal(),
|
||||
new MockClock(),
|
||||
new ParameterBag(['chill_ticket' => ['ticket' => ['response_time_exceeded_delay' => 'PT12H']]]),
|
||||
$userRepository->reveal(),
|
||||
$userGroupRepository->reveal()
|
||||
);
|
||||
|
||||
// Use -1 to trigger the controller's validation error
|
||||
$request = new Request(query: ['byTicketId' => '-1']);
|
||||
|
||||
// This should throw BadRequestHttpException
|
||||
$controller->listTicket($request);
|
||||
}
|
||||
}
|
||||
@@ -185,4 +185,11 @@ class TicketACLAwareRepositoryTest extends KernelTestCase
|
||||
|
||||
self::assertIsArray($actual);
|
||||
}
|
||||
|
||||
public function testFindByTicketid(): void
|
||||
{
|
||||
$actual = $this->repository->findTickets(['byTicketId' => 1]);
|
||||
|
||||
self::assertIsArray($actual);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user