mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-21 06:05:00 +00:00
Add an api list of available person identifiers
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
<?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\PersonBundle\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
|
||||||
|
use Chill\MainBundle\Serializer\Model\Collection;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
|
final readonly class PersonIdentifierListApiController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private Security $security,
|
||||||
|
private SerializerInterface $serializer,
|
||||||
|
private PersonIdentifierManagerInterface $personIdentifierManager,
|
||||||
|
private PaginatorFactoryInterface $paginatorFactory,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
#[Route('/api/1.0/person/identifiers/workers', name: 'person_person_identifiers_worker_list')]
|
||||||
|
public function list(): Response
|
||||||
|
{
|
||||||
|
if (!$this->security->isGranted('ROLE_USER')) {
|
||||||
|
throw new AccessDeniedHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$workers = $this->personIdentifierManager->getWorkers();
|
||||||
|
$paginator = $this->paginatorFactory->create(count($workers));
|
||||||
|
$paginator->setItemsPerPage(count($workers));
|
||||||
|
$collection = new Collection($workers, $paginator);
|
||||||
|
|
||||||
|
return new JsonResponse($this->serializer->serialize($collection, 'json'), json: true);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,38 @@
|
|||||||
|
<?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\PersonBundle\PersonIdentifier\Normalizer;
|
||||||
|
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
|
final readonly class PersonIdentifierWorkerNormalizer implements NormalizerInterface
|
||||||
|
{
|
||||||
|
public function normalize($object, ?string $format = null, array $context = []): array
|
||||||
|
{
|
||||||
|
if (!$object instanceof PersonIdentifierWorker) {
|
||||||
|
throw new \Symfony\Component\Serializer\Exception\UnexpectedValueException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'type' => 'person_identifier_worker',
|
||||||
|
'id' => $object->getDefinition()->getId(),
|
||||||
|
'engine' => $object->getDefinition()->getEngine(),
|
||||||
|
'label' => $object->getDefinition()->getLabel(),
|
||||||
|
'isActive' => $object->getDefinition()->isActive(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsNormalization($data, ?string $format = null): bool
|
||||||
|
{
|
||||||
|
return $data instanceof PersonIdentifierWorker;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,141 @@
|
|||||||
|
<?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\PersonBundle\Tests\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
|
||||||
|
use Chill\MainBundle\Serializer\Normalizer\CollectionNormalizer;
|
||||||
|
use Chill\PersonBundle\Controller\PersonIdentifierListApiController;
|
||||||
|
use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\Normalizer\PersonIdentifierWorkerNormalizer;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||||
|
use Symfony\Component\Serializer\Serializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
class PersonIdentifierListApiControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use ProphecyTrait;
|
||||||
|
|
||||||
|
public function testListAccessDenied(): void
|
||||||
|
{
|
||||||
|
$security = $this->prophesize(Security::class);
|
||||||
|
$security->isGranted('ROLE_USER')->willReturn(false)->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
$serializer = new Serializer([new PersonIdentifierWorkerNormalizer(), new CollectionNormalizer()], [new JsonEncoder()]);
|
||||||
|
|
||||||
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
||||||
|
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
|
||||||
|
|
||||||
|
$controller = new PersonIdentifierListApiController(
|
||||||
|
$security->reveal(),
|
||||||
|
$serializer,
|
||||||
|
$personIdentifierManager->reveal(),
|
||||||
|
$paginatorFactory->reveal(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->expectException(AccessDeniedHttpException::class);
|
||||||
|
$controller->list();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testListSuccess(): void
|
||||||
|
{
|
||||||
|
// Build 3 workers
|
||||||
|
$engine = new class () implements PersonIdentifierEngineInterface {
|
||||||
|
public static function getName(): string
|
||||||
|
{
|
||||||
|
return 'dummy';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canonicalizeValue(array $value, PersonIdentifierDefinition $definition): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, PersonIdentifierDefinition $personIdentifierDefinition): void {}
|
||||||
|
|
||||||
|
public function renderAsString(?\Chill\PersonBundle\Entity\Identifier\PersonIdentifier $identifier, PersonIdentifierDefinition $definition): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$definition1 = new PersonIdentifierDefinition(['en' => 'Label 1'], 'dummy');
|
||||||
|
$definition2 = new PersonIdentifierDefinition(['en' => 'Label 2'], 'dummy');
|
||||||
|
$definition3 = new PersonIdentifierDefinition(['en' => 'Label 3'], 'dummy');
|
||||||
|
// simulate persisted ids
|
||||||
|
$r = new \ReflectionProperty(PersonIdentifierDefinition::class, 'id');
|
||||||
|
$r->setAccessible(true);
|
||||||
|
$r->setValue($definition1, 1);
|
||||||
|
$r->setValue($definition2, 2);
|
||||||
|
$r->setValue($definition3, 3);
|
||||||
|
|
||||||
|
$workers = [
|
||||||
|
new PersonIdentifierWorker($engine, $definition1),
|
||||||
|
new PersonIdentifierWorker($engine, $definition2),
|
||||||
|
new PersonIdentifierWorker($engine, $definition3),
|
||||||
|
];
|
||||||
|
|
||||||
|
$security = $this->prophesize(Security::class);
|
||||||
|
$security->isGranted('ROLE_USER')->willReturn(true)->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
||||||
|
$personIdentifierManager->getWorkers()->willReturn($workers)->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
$paginator = $this->prophesize(\Chill\MainBundle\Pagination\PaginatorInterface::class);
|
||||||
|
$paginator->setItemsPerPage(3)->shouldBeCalledOnce();
|
||||||
|
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
|
||||||
|
$paginator->getItemsPerPage()->willReturn(count($workers));
|
||||||
|
$paginator->getTotalItems()->willReturn(count($workers));
|
||||||
|
$paginator->hasNextPage()->willReturn(false);
|
||||||
|
$paginator->hasPreviousPage()->willReturn(false);
|
||||||
|
|
||||||
|
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
|
||||||
|
$paginatorFactory->create(3)->willReturn($paginator->reveal())->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
$serializer = new Serializer([
|
||||||
|
new PersonIdentifierWorkerNormalizer(),
|
||||||
|
new CollectionNormalizer(),
|
||||||
|
], [new JsonEncoder()]);
|
||||||
|
|
||||||
|
$controller = new PersonIdentifierListApiController(
|
||||||
|
$security->reveal(),
|
||||||
|
$serializer,
|
||||||
|
$personIdentifierManager->reveal(),
|
||||||
|
$paginatorFactory->reveal(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $controller->list();
|
||||||
|
self::assertSame(200, $response->getStatusCode());
|
||||||
|
$body = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
||||||
|
self::assertIsArray($body);
|
||||||
|
self::assertArrayHasKey('count', $body);
|
||||||
|
self::assertArrayHasKey('pagination', $body);
|
||||||
|
self::assertArrayHasKey('results', $body);
|
||||||
|
self::assertSame(3, $body['count']);
|
||||||
|
self::assertCount(3, $body['results']);
|
||||||
|
// spot check one item
|
||||||
|
self::assertSame('person_identifier_worker', $body['results'][0]['type']);
|
||||||
|
self::assertSame(1, $body['results'][0]['id']);
|
||||||
|
self::assertSame('dummy', $body['results'][0]['engine']);
|
||||||
|
self::assertSame(['en' => 'Label 1'], $body['results'][0]['label']);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,101 @@
|
|||||||
|
<?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\PersonBundle\Tests\PersonIdentifier\Normalizer;
|
||||||
|
|
||||||
|
use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\Normalizer\PersonIdentifierWorkerNormalizer;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface;
|
||||||
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
class PersonIdentifierWorkerNormalizerTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testSupportsNormalization(): void
|
||||||
|
{
|
||||||
|
$engine = new class () implements PersonIdentifierEngineInterface {
|
||||||
|
public static function getName(): string
|
||||||
|
{
|
||||||
|
return 'dummy';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canonicalizeValue(array $value, PersonIdentifierDefinition $definition): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, PersonIdentifierDefinition $personIdentifierDefinition): void {}
|
||||||
|
|
||||||
|
public function renderAsString(?\Chill\PersonBundle\Entity\Identifier\PersonIdentifier $identifier, PersonIdentifierDefinition $definition): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$definition = new PersonIdentifierDefinition(label: ['en' => 'SSN'], engine: 'string');
|
||||||
|
$worker = new PersonIdentifierWorker($engine, $definition);
|
||||||
|
|
||||||
|
$normalizer = new PersonIdentifierWorkerNormalizer();
|
||||||
|
|
||||||
|
self::assertTrue($normalizer->supportsNormalization($worker));
|
||||||
|
self::assertFalse($normalizer->supportsNormalization(new \stdClass()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNormalizeReturnsExpectedArray(): void
|
||||||
|
{
|
||||||
|
$engine = new class () implements PersonIdentifierEngineInterface {
|
||||||
|
public static function getName(): string
|
||||||
|
{
|
||||||
|
return 'dummy';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canonicalizeValue(array $value, PersonIdentifierDefinition $definition): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, PersonIdentifierDefinition $personIdentifierDefinition): void {}
|
||||||
|
|
||||||
|
public function renderAsString(?\Chill\PersonBundle\Entity\Identifier\PersonIdentifier $identifier, PersonIdentifierDefinition $definition): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$definition = new PersonIdentifierDefinition(label: ['en' => 'SSN'], engine: 'string');
|
||||||
|
$definition->setActive(false);
|
||||||
|
$worker = new PersonIdentifierWorker($engine, $definition);
|
||||||
|
|
||||||
|
$normalizer = new PersonIdentifierWorkerNormalizer();
|
||||||
|
$normalized = $normalizer->normalize($worker);
|
||||||
|
|
||||||
|
self::assertSame([
|
||||||
|
'type' => 'person_identifier_worker',
|
||||||
|
'id' => null,
|
||||||
|
'engine' => 'string',
|
||||||
|
'label' => ['en' => 'SSN'],
|
||||||
|
'isActive' => false,
|
||||||
|
], $normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNormalizeThrowsOnInvalidObject(): void
|
||||||
|
{
|
||||||
|
$normalizer = new PersonIdentifierWorkerNormalizer();
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$normalizer->normalize(new \stdClass());
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@@ -108,3 +108,6 @@ services:
|
|||||||
|
|
||||||
Chill\PersonBundle\PersonIdentifier\Rendering\:
|
Chill\PersonBundle\PersonIdentifier\Rendering\:
|
||||||
resource: '../PersonIdentifier/Rendering'
|
resource: '../PersonIdentifier/Rendering'
|
||||||
|
|
||||||
|
Chill\PersonBundle\PersonIdentifier\Normalizer\:
|
||||||
|
resource: '../PersonIdentifier/Normalizer'
|
||||||
|
Reference in New Issue
Block a user