2021-11-30 13:54:58 +01:00

138 lines
3.7 KiB
PHP

<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Iterator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use function array_pop;
use function json_decode;
use function shuffle;
/**
* @internal
* @coversNothing
*/
final class PersonApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
public function dataGetPersonFromCenterA(): Iterator
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$personIds = $em->createQuery('SELECT p.id FROM ' . Person::class . ' p ' .
'JOIN p.center c ' .
'WHERE c.name = :center')
->setParameter('center', 'Center A')
->setMaxResults(100)
->getScalarResult();
shuffle($personIds);
yield array_pop($personIds);
yield array_pop($personIds);
yield array_pop($personIds);
yield array_pop($personIds);
}
public function dataGetPersonFromCenterB(): Iterator
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$personIds = $em->createQuery('SELECT p.id FROM ' . Person::class . ' p ' .
'JOIN p.center c ' .
'WHERE c.name = :center')
->setParameter('center', 'Center B')
->setMaxResults(100)
->getScalarResult();
shuffle($personIds);
yield array_pop($personIds);
yield array_pop($personIds);
}
/**
* @dataProvider dataGetPersonFromCenterA
*
* @param mixed $personId
*/
public function testPersonAddressSuggestion($personId): void
{
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, "/api/1.0/person/address/suggest/by-person/{$personId}.json");
$this->assertResponseIsSuccessful();
}
/**
* @dataProvider dataGetPersonFromCenterB
*
* @param mixed $personId
*/
public function testPersonAddressSuggestionUnauthorized($personId): void
{
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, "/api/1.0/person/address/suggest/by-person/{$personId}.json");
$response = $client->getResponse();
$this->assertEquals(403, $response->getStatusCode());
}
/**
* @dataProvider dataGetPersonFromCenterA
*
* @param mixed $personId
*/
public function testPersonGet($personId): void
{
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, "/api/1.0/person/person/{$personId}.json");
$response = $client->getResponse();
$this->assertResponseIsSuccessful();
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('type', $data);
$this->assertArrayHasKey('id', $data);
$this->assertEquals('person', $data['type']);
$this->assertEquals($personId, $data['id']);
}
/**
* @dataProvider dataGetPersonFromCenterB
*
* @param mixed $personId
*/
public function testPersonGetUnauthorized($personId): void
{
$client = $this->getClientAuthenticated();
$client->request(Request::METHOD_GET, "/api/1.0/person/person/{$personId}.json");
$response = $client->getResponse();
$this->assertEquals(403, $response->getStatusCode());
}
}