Files
chill-bundles/src/Bundle/ChillTicketBundle/tests/Controller/FindCallerControllerTest.php
2025-06-20 17:35:19 +02:00

129 lines
3.6 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\MainBundle\Phonenumber\PhonenumberHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use Chill\TicketBundle\Controller\FindCallerController;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* @internal
*
* @coversNothing
*/
class FindCallerControllerTest extends TestCase
{
use ProphecyTrait;
/**
* @dataProvider provideFindCaller
*/
public function testFindCaller(string $caller, array $persons, array $expected): void
{
$controller = $this->buildController($persons);
$request = new Request(query: ['caller' => $caller]);
$response = $controller->findCaller($request);
$actual = json_decode($response->getContent(), true);
self::assertEqualsCanonicalizing($expected, $actual);
}
public static function provideFindCaller(): iterable
{
yield [
'32486540600',
[],
['found' => false, 'name' => null],
];
yield [
'32486540600',
[new Person()],
['found' => true, 'name' => 'pppp'],
]
;
yield [
'32486540600',
[new Person(), new Person()],
['found' => true, 'name' => 'multiple'],
];
}
public function testFindCallerWithoutCallerArgument(): void
{
self::expectException(BadRequestHttpException::class);
$controller = $this->buildController([]);
$request = new Request(query: []);
$controller->findCaller($request);
}
public function testFindCallerWithEmptyCallerArgument(): void
{
self::expectException(BadRequestHttpException::class);
$controller = $this->buildController([]);
$request = new Request(query: ['caller' => '']);
$controller->findCaller($request);
}
public function testFindCallerWithInvalidCaller(): void
{
self::expectException(BadRequestHttpException::class);
$controller = $this->buildController([]);
$request = new Request(query: ['caller' => 'abcde']);
$controller->findCaller($request);
}
private function buildController(array $personsFound): FindCallerController
{
$phonenumberHelper =
$subject = new PhonenumberHelper(
new ArrayAdapter(),
new ParameterBag([
'chill_main.phone_helper' => [
'default_carrier_code' => 'BE',
],
]),
new NullLogger()
);
$personRepository = $this->prophesize(PersonRepository::class);
$personRepository->findByPhoneNumber(Argument::any(), Argument::type('int'), Argument::type('int'))->willReturn($personsFound);
$personRender = $this->prophesize(PersonRenderInterface::class);
$personRender->renderString(Argument::type(Person::class), Argument::type('array'))->willReturn('pppp');
return new FindCallerController($phonenumberHelper, $personRepository->reveal(), $personRender->reveal());
}
}