[generic doc] listing generic doc for person

This commit is contained in:
2023-05-30 20:48:35 +02:00
parent eb107f5a15
commit 40af1e64ac
15 changed files with 842 additions and 42 deletions

View File

@@ -14,9 +14,12 @@ namespace Chill\DocStoreBundle\Tests\GenericDoc;
use Chill\DocStoreBundle\GenericDoc\FetchQuery;
use Chill\DocStoreBundle\GenericDoc\FetchQueryInterface;
use Chill\DocStoreBundle\GenericDoc\GenericDocDTO;
use Chill\DocStoreBundle\GenericDoc\GenericDocForPersonProviderInterface;
use Chill\DocStoreBundle\GenericDoc\Manager;
use Chill\DocStoreBundle\GenericDoc\GenericDocForAccompanyingPeriodProviderInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
@@ -53,7 +56,8 @@ class ManagerTest extends KernelTestCase
}
$manager = new Manager(
[new SimpleGenericDocProvider()],
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
@@ -62,6 +66,27 @@ class ManagerTest extends KernelTestCase
self::assertIsInt($nb);
}
public function testCountByPerson(): void
{
$person = $this->em->createQuery('SELECT a FROM '.Person::class.' a')
->setMaxResults(1)
->getSingleResult();
if (null === $person) {
throw new \UnexpectedValueException("person found");
}
$manager = new Manager(
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
$nb = $manager->countDocForPerson($person);
self::assertIsInt($nb);
}
public function testFindDocByAccompanyingPeriod(): void
{
$period = $this->em->createQuery('SELECT a FROM '.AccompanyingPeriod::class.' a')
@@ -73,7 +98,8 @@ class ManagerTest extends KernelTestCase
}
$manager = new Manager(
[new SimpleGenericDocProvider()],
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
@@ -81,14 +107,77 @@ class ManagerTest extends KernelTestCase
self::assertInstanceOf(GenericDocDTO::class, $doc);
}
}
public function testFindDocByPerson(): void
{
$person = $this->em->createQuery('SELECT a FROM '.Person::class.' a')
->setMaxResults(1)
->getSingleResult();
if (null === $person) {
throw new \UnexpectedValueException("person not found");
}
$manager = new Manager(
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
foreach ($manager->findDocForPerson($person) as $doc) {
self::assertInstanceOf(GenericDocDTO::class, $doc);
}
}
public function testPlacesForPerson(): void
{
$person = $this->em->createQuery('SELECT a FROM '.Person::class.' a')
->setMaxResults(1)
->getSingleResult();
if (null === $person) {
throw new \UnexpectedValueException("person not found");
}
$manager = new Manager(
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
$places = $manager->placesForPerson($person);
self::assertEquals(['dummy_person_doc'], $places);
}
public function testPlacesForAccompanyingPeriod(): void
{
$period = $this->em->createQuery('SELECT a FROM '.AccompanyingPeriod::class.' a')
->setMaxResults(1)
->getSingleResult();
if (null === $period) {
throw new \UnexpectedValueException("period not found");
}
$manager = new Manager(
[new SimpleGenericDocAccompanyingPeriodProvider()],
[new SimpleGenericDocPersonProvider()],
$this->connection,
);
$places = $manager->placesForAccompanyingPeriod($period);
self::assertEquals(['accompanying_course_document_dummy'], $places);
}
}
final readonly class SimpleGenericDocProvider implements GenericDocForAccompanyingPeriodProviderInterface
final readonly class SimpleGenericDocAccompanyingPeriodProvider implements GenericDocForAccompanyingPeriodProviderInterface
{
public function buildFetchQueryForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod, ?\DateTimeImmutable $startDate = null, ?\DateTimeImmutable $endDate = null, ?string $content = null, ?string $origin = null): FetchQueryInterface
{
$query = new FetchQuery(
'accompanying_course_document',
'accompanying_course_document_dummy',
sprintf('jsonb_build_object(\'id\', %s)', 'id'),
'd',
'(VALUES (1, \'2023-05-01\'::date), (2, \'2023-05-01\'::date)) AS sq (id, d)',
@@ -104,3 +193,25 @@ final readonly class SimpleGenericDocProvider implements GenericDocForAccompanyi
return true;
}
}
final readonly class SimpleGenericDocPersonProvider implements GenericDocForPersonProviderInterface
{
public function buildFetchQueryForPerson(Person $person, ?DateTimeImmutable $startDate = null, ?DateTimeImmutable $endDate = null, ?string $content = null, ?string $origin = null): FetchQueryInterface
{
$query = new FetchQuery(
'dummy_person_doc',
sprintf('jsonb_build_object(\'id\', %s)', 'id'),
'd',
'(VALUES (1, \'2023-05-01\'::date), (2, \'2023-05-01\'::date)) AS sq (id, d)',
);
$query->addWhereClause('d > ?', [new \DateTimeImmutable('2023-01-01')], [Types::DATE_IMMUTABLE]);
return $query;
}
public function isAllowedForPerson(Person $person): bool
{
return true;
}
}