mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-06 05:19:43 +00:00
[WIP] Refactor AddressReferenceRepository to use interface and add tests for AddressReferenceAggregatedApiController
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?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\MainBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Controller\AddressReferenceAggregatedApiController;
|
||||
use Chill\MainBundle\Repository\AddressReferenceRepositoryInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @covers \Chill\MainBundle\Controller\AddressReferenceAggregatedApiController
|
||||
*/
|
||||
final class AddressReferenceAggregatedApiControllerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testAccessDeniedWhenNotAuthenticated(): void
|
||||
{
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('IS_AUTHENTICATED')->willReturn(false);
|
||||
|
||||
$repo = $this->prophesize(AddressReferenceRepositoryInterface::class);
|
||||
|
||||
$controller = new AddressReferenceAggregatedApiController($security->reveal(), $repo->reveal());
|
||||
|
||||
$request = new Request(query: ['q' => 'anything']);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$controller->search($request);
|
||||
}
|
||||
|
||||
public function testBadRequestWhenQueryIsMissing(): void
|
||||
{
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('IS_AUTHENTICATED')->willReturn(true);
|
||||
|
||||
$repo = $this->prophesize(AddressReferenceRepositoryInterface::class);
|
||||
|
||||
$controller = new AddressReferenceAggregatedApiController($security->reveal(), $repo->reveal());
|
||||
|
||||
$request = new Request();
|
||||
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
$this->expectExceptionMessage('Parameter "q" is required.');
|
||||
|
||||
$controller->search($request);
|
||||
}
|
||||
|
||||
public function testBadRequestWhenQueryIsEmpty(): void
|
||||
{
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('IS_AUTHENTICATED')->willReturn(true);
|
||||
|
||||
$repo = $this->prophesize(AddressReferenceRepositoryInterface::class);
|
||||
|
||||
$controller = new AddressReferenceAggregatedApiController($security->reveal(), $repo->reveal());
|
||||
|
||||
$request = new Request(query: ['q' => ' ']);
|
||||
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
$this->expectExceptionMessage('Parameter "q" is required and cannot be empty.');
|
||||
|
||||
$controller->search($request);
|
||||
}
|
||||
|
||||
public function testSuccessfulSearchReturnsJsonAndCallsRepositoryWithTrimmedQuery(): void
|
||||
{
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted('IS_AUTHENTICATED')->willReturn(true);
|
||||
|
||||
$expectedQuery = 'foo';
|
||||
$iterableResult = new \ArrayIterator([
|
||||
[
|
||||
'street' => 'Main Street',
|
||||
'postcode_id' => 123,
|
||||
'code' => '1000',
|
||||
'label' => 'Brussels',
|
||||
'positions' => ['1' => '12', '2' => '14'],
|
||||
'row_number' => 1,
|
||||
],
|
||||
]);
|
||||
|
||||
$repo = $this->prophesize(AddressReferenceRepositoryInterface::class);
|
||||
$repo->findAggregatedBySearchString($expectedQuery)->willReturn($iterableResult)->shouldBeCalledOnce();
|
||||
|
||||
$controller = new AddressReferenceAggregatedApiController($security->reveal(), $repo->reveal());
|
||||
|
||||
// Provide spaces around to ensure trimming is applied
|
||||
$request = new Request(query: ['q' => " {$expectedQuery} "]);
|
||||
|
||||
$response = $controller->search($request);
|
||||
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
$data = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
||||
self::assertIsArray($data);
|
||||
self::assertCount(1, $data);
|
||||
self::assertSame('Main Street', $data[0]['street']);
|
||||
self::assertSame(123, $data[0]['postcode_id']);
|
||||
self::assertSame('1000', $data[0]['code']);
|
||||
self::assertSame('Brussels', $data[0]['label']);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user