Merge branch 'rector/rules-up-to-php82' into rector/rules-symfony

This commit is contained in:
2023-07-19 23:22:57 +02:00
577 changed files with 18339 additions and 2168 deletions

View File

@@ -0,0 +1,54 @@
<?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 AccompanyingPeriod\Lifecycle;
use Chill\MainBundle\Entity\CronJobExecution;
use Chill\PersonBundle\AccompanyingPeriod\Lifecycle\AccompanyingPeriodStepChangeCronjob;
use Chill\PersonBundle\AccompanyingPeriod\Lifecycle\AccompanyingPeriodStepChangeRequestor;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
/**
* @internal
* @coversNothing
*/
class AccompanyingPeriodStepChangeCronjobTest extends TestCase
{
use ProphecyTrait;
/**
* @dataProvider provideRunTimes
*/
public function testCanRun(string $datetime, \DateTimeImmutable $lastExecutionStart, bool $canRun): void
{
$requestor = $this->prophesize(AccompanyingPeriodStepChangeRequestor::class);
$clock = new MockClock($datetime);
$cronJob = new AccompanyingPeriodStepChangeCronjob($clock, $requestor->reveal());
$cronJobExecution = (new CronJobExecution($cronJob->getKey()))->setLastStart($lastExecutionStart);
$this->assertEquals($canRun, $cronJob->canRun($cronJobExecution));
}
public function provideRunTimes(): iterable
{
// can run, during the night
yield ['2023-01-15T01:00:00+02:00', new \DateTimeImmutable('2023-01-14T00:00:00+02:00'), true];
// can not run, not during the night
yield ['2023-01-15T10:00:00+02:00', new \DateTimeImmutable('2023-01-14T00:00:00+02:00'), false];
// can not run: not enough elapsed time
yield ['2023-01-15T01:00:00+02:00', new \DateTimeImmutable('2023-01-15T00:30:00+02:00'), false];
}
}

View File

@@ -138,12 +138,14 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
$this->assertCount(1, $period->getLocationHistories());
$this->assertSame($address, $period->getLocationHistories()->first()->getAddressLocation());
$this->assertNull($period->getLocationHistories()->first()->getPersonLocation());
$period->setPersonLocation($person);
$period->setAddressLocation(null);
$this->assertCount(2, $period->getLocationHistories());
$this->assertSame($person, $period->getLocationHistories()->last()->getPersonLocation());
$this->assertNull($period->getLocationHistories()->last()->getAddressLocation());
$period->setAddressLocation($address);
$period->setPersonLocation(null);
@@ -172,6 +174,27 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
} while ($iterator->valid());
}
public function testHistoryLocationNotHavingBothAtStart()
{
$period = new AccompanyingPeriod();
$person = new Person();
$address = new Address();
$period->setAddressLocation($address);
$period->setPersonLocation($person);
$period->setStep(AccompanyingPeriod::STEP_CONFIRMED);
$this->assertCount(1, $period->getLocationHistories());
self::assertNull($period->getAddressLocation());
self::assertNull($period->getLocationHistories()->first()->getAddressLocation());
self::assertSame($person, $period->getLocationHistories()->first()->getPersonLocation());
self::assertSame($person, $period->getPersonLocation());
self::assertEquals('person', $period->getLocationStatus());
}
public function testIsClosed()
{
$period = new AccompanyingPeriod(new DateTime());

View File

@@ -13,7 +13,7 @@ namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\StepFilter;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\StepFilterOnDate;
/**
* @internal
@@ -21,7 +21,7 @@ use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\StepFilter;
*/
final class StepFilterTest extends AbstractFilterTest
{
private StepFilter $filter;
private StepFilterOnDate $filter;
protected function setUp(): void
{
@@ -40,6 +40,8 @@ final class StepFilterTest extends AbstractFilterTest
return [
['accepted_steps' => AccompanyingPeriod::STEP_DRAFT],
['accepted_steps' => AccompanyingPeriod::STEP_CONFIRMED],
['accepted_steps' => AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_LONG],
['accepted_steps' => AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT],
['accepted_steps' => AccompanyingPeriod::STEP_CLOSED],
];
}

View File

@@ -78,17 +78,13 @@ final class SocialWorkTypeFilterTest extends AbstractFilterTest
$goals = array_unique($goals);
$results = array_unique($results);
$data = [
return [
[
'actionType' => implode(',', $actions),
'goal' => implode(',', $goals),
'result' => implode(',', $results),
],
];
/// TODO ne fonctionne pas
var_dump($data);
return $data;
}
public function getQueryBuilders(): array

View File

@@ -0,0 +1,515 @@
<?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 Repository;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepository;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Registry;
/**
* @internal
* @coversNothing
*/
class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
{
use ProphecyTrait;
private AccompanyingPeriodRepository $accompanyingPeriodRepository;
private CenterResolverManagerInterface $centerResolverManager;
private CenterRepositoryInterface $centerRepository;
private EntityManagerInterface $entityManager;
private ScopeRepositoryInterface $scopeRepository;
private Registry $registry;
private static array $periodsIdsToDelete = [];
protected function setUp(): void
{
self::bootKernel();
$this->accompanyingPeriodRepository = self::$container->get(AccompanyingPeriodRepository::class);
$this->centerRepository = self::$container->get(CenterRepositoryInterface::class);
$this->centerResolverManager = self::$container->get(CenterResolverManagerInterface::class);
$this->entityManager = self::$container->get(EntityManagerInterface::class);
$this->scopeRepository = self::$container->get(ScopeRepositoryInterface::class);
$this->registry = self::$container->get(Registry::class);
}
public static function tearDownAfterClass(): void
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$repository = self::$container->get(AccompanyingPeriodRepository::class);
foreach (self::$periodsIdsToDelete as $id) {
if (null === $period = $repository->find($id)) {
throw new \RuntimeException("period not found while trying to delete it");
}
foreach ($period->getParticipations() as $participation) {
$em->remove($participation);
}
$em->remove($period);
}
//$em->flush();
}
/**
* @dataProvider provideDataFindByUserAndPostalCodesOpenedAccompanyingPeriod
* @param list<array{center: Center, scopeOnRole: list<Scope>, scopeCanSeeConfidential: list<Scope>}> $centerScopes
* @param list<AccompanyingPeriod> $expectedContains
* @param list<AccompanyingPeriod> $expectedNotContains
*/
public function testFindByUserAndPostalCodesOpenedAccompanyingPeriod(User $user, User $searched, array $centerScopes, array $expectedContains, array $expectedNotContains, string $message): void
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$authorizationHelper = $this->prophesize(AuthorizationHelperForCurrentUserInterface::class);
$centers = [];
foreach ($centerScopes as ['center' => $center, 'scopeOnRole' => $scopes, 'scopeCanSeeConfidential' => $scopesCanSeeConfidential]) {
$centers[spl_object_hash($center)] = $center;
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE, $center)
->willReturn($scopes);
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE_CONFIDENTIAL_ALL, $center)
->willReturn($scopesCanSeeConfidential);
}
$authorizationHelper->getReachableCenters(AccompanyingPeriodVoter::SEE)->willReturn(array_values($centers));
$repository = new AccompanyingPeriodACLAwareRepository(
$this->accompanyingPeriodRepository,
$security->reveal(),
$authorizationHelper->reveal(),
$this->centerResolverManager
);
$actual = array_map(
fn (AccompanyingPeriod $period) => $period->getId(),
$repository->findByUserAndPostalCodesOpenedAccompanyingPeriod($searched, [], ['id' => 'DESC'], 20, 0)
);
foreach ($expectedContains as $expected) {
self::assertContains($expected->getId(), $actual, $message);
}
foreach ($expectedNotContains as $expected) {
self::assertNotContains($expected->getId(), $actual, $message);
}
}
public function provideDataFindByUserAndPostalCodesOpenedAccompanyingPeriod(): iterable
{
$this->setUp();
if (null === $user = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u")->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
if (null === $anotherUser = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u WHERE u.id != :uid")->setParameter('uid', $user->getId())
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
/** @var Person $person */
[$person, $anotherPerson, $person2, $person3] = $this->entityManager
->createQuery("SELECT p FROM " . Person::class . " p JOIN p.centerCurrent current_center")
->setMaxResults(4)
->getResult();
if (null === $person || null === $anotherPerson || null === $person2 || null === $person3) {
throw new \RuntimeException("no person found");
}
$scopes = $this->scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException("not enough scopes for this test");
}
$scopesCanSee = [ $scopes[0] ];
$scopesGroup2 = [ $scopes[1] ];
$centers = $this->centerRepository->findActive();
$aCenterNotAssociatedToPerson = array_values(array_filter($centers, fn (Center $c) => $c !== $person->getCenter()))[0];
if (2 > count($centers)) {
throw new \RuntimeException("not enough centers for this test");
}
$period = $this->buildPeriod($person, $scopesCanSee, $user, true);
$period->setUser($user);
yield [
$anotherUser,
$user,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => [],
],
],
[$period],
[],
"period should be visible with expected scopes",
];
yield [
$anotherUser,
$user,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesGroup2,
'scopeCanSeeConfidential' => [],
],
],
[],
[$period],
"period should not be visible without expected scopes",
];
yield [
$anotherUser,
$user,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesGroup2,
'scopeCanSeeConfidential' => [],
],
[
'center' => $aCenterNotAssociatedToPerson,
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => [],
],
],
[],
[$period],
"period should not be visible for user having right in another scope (with multiple centers)"
];
$period = $this->buildPeriod($person, $scopesCanSee, $user, true);
$period->setUser($user);
$period->setConfidential(true);
yield [
$anotherUser,
$user,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => [],
],
],
[],
[$period],
"period confidential should not be visible",
];
yield [
$anotherUser,
$user,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => $scopesCanSee,
],
],
[$period],
[],
"period confidential be visible if user has required scopes",
];
$this->entityManager->flush();
}
/**
* @dataProvider provideDataFindByUndispatched
* @param list<array{center: Center, scopeOnRole: list<Scope>, scopeCanSeeConfidential: list<Scope>}> $centerScopes
* @param list<AccompanyingPeriod> $expectedContains
* @param list<AccompanyingPeriod> $expectedNotContains
*/
public function testFindByUndispatched(User $user, array $centerScopes, array $expectedContains, array $expectedNotContains, string $message): void
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$authorizationHelper = $this->prophesize(AuthorizationHelperForCurrentUserInterface::class);
$centers = [];
foreach ($centerScopes as ['center' => $center, 'scopeOnRole' => $scopes, 'scopeCanSeeConfidential' => $scopesCanSeeConfidential]) {
$centers[spl_object_hash($center)] = $center;
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE, $center)
->willReturn($scopes);
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE_CONFIDENTIAL_ALL, $center)
->willReturn($scopesCanSeeConfidential);
}
$authorizationHelper->getReachableCenters(AccompanyingPeriodVoter::SEE)->willReturn(array_values($centers));
$repository = new AccompanyingPeriodACLAwareRepository(
$this->accompanyingPeriodRepository,
$security->reveal(),
$authorizationHelper->reveal(),
$this->centerResolverManager
);
$actual = array_map(
fn (AccompanyingPeriod $period) => $period->getId(),
$repository->findByUnDispatched([], [], [], ['id' => 'DESC'], 20, 0)
);
foreach ($expectedContains as $expected) {
self::assertContains($expected->getId(), $actual, $message);
}
foreach ($expectedNotContains as $expected) {
self::assertNotContains($expected->getId(), $actual, $message);
}
}
public function provideDataFindByUndispatched(): iterable
{
$this->setUp();
if (null === $user = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u")->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
if (null === $anotherUser = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u WHERE u.id != :uid")->setParameter('uid', $user->getId())
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
/** @var Person $person */
[$person, $anotherPerson, $person2, $person3] = $this->entityManager
->createQuery("SELECT p FROM " . Person::class . " p ")
->setMaxResults(4)
->getResult();
if (null === $person || null === $anotherPerson || null === $person2 || null === $person3) {
throw new \RuntimeException("no person found");
}
$scopes = $this->scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException("not enough scopes for this test");
}
$scopesCanSee = [ $scopes[0] ];
$scopesGroup2 = [ $scopes[1] ];
$centers = $this->centerRepository->findActive();
if (2 > count($centers)) {
throw new \RuntimeException("not enough centers for this test");
}
$period = $this->buildPeriod($person, $scopesCanSee, $user, true);
// expected scope: can see the period
yield [
$anotherUser,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => [],
],
],
[$period],
[],
"period should be visible with expected scopes",
];
// no scope visible
yield [
$anotherUser,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesGroup2,
'scopeCanSeeConfidential' => [],
],
],
[],
[$period],
"period should not be visible without expected scopes",
];
// another center
yield [
$anotherUser,
[
[
'center' => $person->getCenter(),
'scopeOnRole' => $scopesGroup2,
'scopeCanSeeConfidential' => [],
],
[
'center' => array_values(array_filter($centers, fn (Center $c) => $c !== $person->getCenter()))[0],
'scopeOnRole' => $scopesCanSee,
'scopeCanSeeConfidential' => [],
],
],
[],
[$period],
"period should not be visible for user having right in another scope (with multiple centers)"
];
$this->entityManager->flush();
}
/**
* For testing this method, we mock the authorization helper to return different Scope that a user
* can see, or that a user can see confidential periods.
*
* @param array<Scope> $scopeUserCanSee
* @param array<Scope> $scopeUserCanSeeConfidential
* @param array<AccompanyingPeriod> $expectedPeriod
* @dataProvider provideDataForFindByPerson
*/
public function testFindByPersonTestUser(User $user, Person $person, array $scopeUserCanSee, array $scopeUserCanSeeConfidential, array $expectedPeriod, string $message): void
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$authorizationHelper = $this->prophesize(AuthorizationHelperForCurrentUserInterface::class);
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE, Argument::any())
->willReturn($scopeUserCanSee);
$authorizationHelper->getReachableScopes(AccompanyingPeriodVoter::SEE_CONFIDENTIAL_ALL, Argument::any())
->willReturn($scopeUserCanSeeConfidential);
$repository = new AccompanyingPeriodACLAwareRepository(
$this->accompanyingPeriodRepository,
$security->reveal(),
$authorizationHelper->reveal(),
$this->centerResolverManager
);
$actuals = $repository->findByPerson($person, AccompanyingPeriodVoter::SEE);
$expectedIds = array_map(fn (AccompanyingPeriod $period) => $period->getId(), $expectedPeriod);
self::assertCount(count($expectedPeriod), $actuals, $message);
foreach ($actuals as $actual) {
self::assertContains($actual->getId(), $expectedIds);
}
}
public function provideDataForFindByPerson(): iterable
{
$this->setUp();
if (null === $user = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u")->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
if (null === $anotherUser = $this->entityManager->createQuery("SELECT u FROM " . User::class . " u WHERE u.id != :uid")->setParameter('uid', $user->getId())
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("no user found");
}
[$person, $anotherPerson, $person2, $person3] = $this->entityManager
->createQuery("SELECT p FROM " . Person::class . " p WHERE SIZE(p.accompanyingPeriodParticipations) = 0")
->setMaxResults(4)
->getResult();
if (null === $person || null === $anotherPerson || null === $person2 || null === $person3) {
throw new \RuntimeException("no person found");
}
$scopes = $this->scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException("not enough scopes for this test");
}
$scopesCanSee = [ $scopes[0] ];
$scopesGroup2 = [ $scopes[1] ];
// case: a period is in draft state
$period = $this->buildPeriod($person, $scopesCanSee, $user, false);
yield [$user, $person, $scopesCanSee, [], [$period], "a user can see his period during draft state"];
// another user is not allowed to see this period, because it is in DRAFT state
yield [$anotherUser, $person, $scopesCanSee, [], [], "another user is not allowed to see the period of someone else in draft state"];
// the period is confirmed
$period = $this->buildPeriod($anotherPerson, $scopesCanSee, $user, true);
// the other user can now see it
yield [$user, $anotherPerson, $scopesCanSee, [], [$period], "a user see his period when confirmed"];
yield [$anotherUser, $anotherPerson, $scopesCanSee, [], [$period], "another user with required scopes is allowed to see the period when not draft"];
yield [$anotherUser, $anotherPerson, $scopesGroup2, [], [], "another user without the required scopes is not allowed to see the period when not draft"];
// this period will be confidential
$period = $this->buildPeriod($person2, $scopesCanSee, $user, true);
$period->setConfidential(true)->setUser($user, true);
yield [$user, $person2, $scopesCanSee, [], [$period], "a user see his period when confirmed and confidential with required scopes"];
yield [$user, $person2, $scopesGroup2, [], [$period], "a user see his period when confirmed and confidential without required scopes"];
yield [$anotherUser, $person2, $scopesCanSee, [], [], "a user don't see a confidential period, even if he has required scopes"];
yield [$anotherUser, $person2, $scopesCanSee, $scopesCanSee, [$period], "a user see the period when confirmed and confidential if he has required scope to see the period"];
// period draft with creator = null
$period = $this->buildPeriod($person3, $scopesCanSee, null, false);
yield [$user, $person3, $scopesCanSee, [], [$period], "a user see a period when draft if no creator on the period"];
$this->entityManager->flush();
}
/**
* @param array<Scope> $scopes
*/
private function buildPeriod(Person $person, array $scopes, User|null $creator, bool $confirm): AccompanyingPeriod
{
$period = new AccompanyingPeriod();
$period->addPerson($person);
if (null !== $creator) {
$period->setCreatedBy($creator);
}
foreach ($scopes as $scope) {
$period->addScope($scope);
}
$this->entityManager->persist($period);
self::$periodsIdsToDelete[] = $period->getId();
if ($confirm) {
$workflow = $this->registry->get($period, 'accompanying_period_lifecycle');
$workflow->apply($period, 'confirm');
}
return $period;
}
}

View File

@@ -18,6 +18,7 @@ use Chill\PersonBundle\Entity\Person;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
@@ -39,7 +40,7 @@ final class HouseholdNormalizerTest extends KernelTestCase
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
public function testNormalizationRecursive()
public function testNormalizationRecursive(): void
{
$person = new Person();
$person->setFirstName('ok')->setLastName('ok');
@@ -67,4 +68,53 @@ final class HouseholdNormalizerTest extends KernelTestCase
$this->assertArrayHasKey('type', $normalized);
$this->assertEquals('household', $normalized['type']);
}
/**
* When a household have old members (members which are not "current"),
* the indexes of the household must be reset to numerical and contiguous
* indexes. This ensure that it will be mapped as a list, not as an associative
* array.
*/
public function testHouseholdDocGenNormalizationWithOldMembers(): void
{
$previousPerson = new Person();
$previousPerson->setFirstName('ok')->setLastName('ok');
$this->entityManager->persist($previousPerson);
$member = new HouseholdMember();
$household = new Household();
$position = (new Position())
->setShareHousehold(true)
->setAllowHolder(true);
$member->setPerson($previousPerson)
->setStartDate(new DateTimeImmutable('1 year ago'))
->setEndDate(new DateTimeImmutable('1 month ago'))
->setPosition($position);
$household->addMember($member);
$currentPerson1 = new Person();
$currentPerson1->setFirstName('p1')->setLastName('p1');
$this->entityManager->persist($currentPerson1);
$member = new HouseholdMember();
$member->setPerson($currentPerson1)
->setStartDate(new DateTimeImmutable('1 year ago'))
->setPosition($position);
$household->addMember($member);
$normalized = $this->normalizer->normalize(
$household,
'docgen',
[
AbstractNormalizer::GROUPS => ['docgen:read'],
'docgen:expects' => Household::class,
'docgen:person:with-household' => false,
'docgen:person:with-relations' => false,
'docgen:person:with-budget' => false,
]
);
self::assertIsArray($normalized);
self::assertArrayHasKey(0, $normalized['currentMembers']);
}
}

View File

@@ -0,0 +1,291 @@
<?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 Service\DocGenerator;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Service\DocGenerator\AccompanyingPeriodContext;
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @internal
* @coversNothing
*/
class AccompanyingPeriodContextTest extends KernelTestCase
{
private BaseContextData $baseContextData;
private DocumentCategoryRepository $documentCategoryRepository;
private EntityManagerInterface $em;
private NormalizerInterface $normalizer;
private PersonRenderInterface $personRender;
private PersonRepository $personRepository;
private TranslatableStringHelperInterface $translatableStringHelper;
private TranslatorInterface $translator;
private ThirdPartyRender $thirdPartyRender;
private ThirdPartyRepository $thirdPartyRepository;
protected function setUp(): void
{
self::bootKernel();
$this->baseContextData = self::$container->get(BaseContextData::class);
$this->documentCategoryRepository = self::$container->get(DocumentCategoryRepository::class);
$this->em = self::$container->get(EntityManagerInterface::class);
$this->normalizer = self::$container->get(NormalizerInterface::class);
$this->personRender = self::$container->get(PersonRenderInterface::class);
$this->personRepository = self::$container->get(PersonRepository::class);
$this->translatableStringHelper = self::$container->get(TranslatableStringHelperInterface::class);
$this->translator = self::$container->get(TranslatorInterface::class);
$this->thirdPartyRender = self::$container->get(ThirdPartyRender::class);
$this->thirdPartyRepository = self::$container->get(ThirdPartyRepository::class);
}
private function buildContext(): AccompanyingPeriodContext
{
return new AccompanyingPeriodContext(
$this->documentCategoryRepository,
$this->normalizer,
$this->translatableStringHelper,
$this->em,
$this->personRender,
$this->personRepository,
$this->translator,
$this->baseContextData,
$this->thirdPartyRender,
$this->thirdPartyRepository,
);
}
/**
* This test run the methods executed when a document is generated:
*
* - normalized data from the form in a way that they are stored in message queue;
* - denormalize the data from the message queue,
* - and get the data, as they will be transmitted to the GeneratorDriver
*
* @param array $options the options, as they are stored in the DocGeneratorTemplate (the admin form data)
* @param AccompanyingPeriod $entity The entity from which the data will be extracted
* @param array $data The data, from the public form
* @param array $expectedNormalized, how the normalized data are expected (allow to check that this data will be compliant with the storage in messenger queue)
* @param callable $assertionsOnData some test that will be executed on the normalized data
* @dataProvider provideNormalizedData
*/
public function testContextGenerationDataNormalizeDenormalizeGetData(
array $options,
AccompanyingPeriod $entity,
array $data,
array $expectedNormalized,
callable $assertionsOnData
): void {
$context = $this->buildContext();
$template = new DocGeneratorTemplate();
$template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class)
->setDescription("description")->setActive(true)
->setOptions($options);
$normalized = $context->contextGenerationDataNormalize($template, $entity, $data);
self::assertEquals($expectedNormalized, $normalized);
$denormalized = $context->contextGenerationDataDenormalize($template, $entity, $normalized);
$data = $context->getData($template, $entity, $denormalized);
call_user_func($assertionsOnData, $data);
}
public function provideNormalizedData(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$thirdParty = $em->createQuery("SELECT t FROM " . ThirdParty::class . " t")
->setMaxResults(1)
->getSingleResult();
if (null === $thirdParty) {
throw new \RuntimeException("No thirdparty in database");
}
$period = $em->createQuery("SELECT a FROM " . AccompanyingPeriod::class . " a WHERE a.step = 'CONFIRMED'")
->setMaxResults(1)
->getSingleResult();
if (null === $period) {
throw new \RuntimeException("No confirmed period in database");
}
$person = $em->createQuery("SELECT p FROM " . Person::class . " p")
->setMaxResults(1)
->getSingleResult();
if (null === $person) {
throw new \RuntimeException("No confirmed period in database");
}
yield [
// test with only thirdParty
[
'mainPerson' => false,
'mainPersonLabel' => 'person',
'person1' => false,
'person1Label' => 'person2',
'person2' => false,
'person2Label' => 'person2',
'thirdParty' => true,
'thirdPartyLabel' => '3party'
],
$period,
[
'thirdParty' => $thirdParty
],
[
'thirdParty' => $thirdParty->getId(),
'mainPerson' => null,
'person1' => null,
'person2' => null,
],
function (array $data) use ($thirdParty, $period) {
self::assertArrayHasKey('thirdParty', $data);
self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']);
self::assertArrayHasKey('course', $data);
self::assertEquals($period->getId(), $data['course']['id']);
},
];
yield [
// test with only mainPerson
[
'mainPerson' => true,
'mainPersonLabel' => 'person',
'person1' => false,
'person1Label' => 'person2',
'person2' => false,
'person2Label' => 'person2',
'thirdParty' => false,
'thirdPartyLabel' => '3party'
],
$period,
[
'mainPerson' => $person,
],
[
'thirdParty' => null,
'mainPerson' => $person->getId(),
'person1' => null,
'person2' => null,
],
function (array $data) use ($person, $period) {
self::assertArrayHasKey('mainPerson', $data);
self::assertEquals($person->getId(), $data['mainPerson']['id']);
self::assertArrayHasKey('course', $data);
self::assertEquals($period->getId(), $data['course']['id']);
},
];
yield [
// test with every options activated
[
'mainPerson' => true,
'mainPersonLabel' => 'person',
'person1' => true,
'person1Label' => 'person2',
'person2' => true,
'person2Label' => 'person2',
'thirdParty' => true,
'thirdPartyLabel' => '3party'
],
$period,
[
'mainPerson' => $person,
'person1' => $person,
'person2' => $person,
'thirdParty' => $thirdParty,
],
[
'thirdParty' => $thirdParty->getId(),
'mainPerson' => $person->getId(),
'person1' => $person->getId(),
'person2' => $person->getId(),
],
function (array $data) use ($person, $thirdParty, $period) {
self::assertArrayHasKey('mainPerson', $data);
self::assertEquals($person->getId(), $data['mainPerson']['id']);
self::assertArrayHasKey('person1', $data);
self::assertEquals($person->getId(), $data['person1']['id']);
self::assertArrayHasKey('person2', $data);
self::assertEquals($person->getId(), $data['person2']['id']);
self::assertArrayHasKey('thirdParty', $data);
self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']);
self::assertArrayHasKey('course', $data);
self::assertEquals($period->getId(), $data['course']['id']);
},
];
yield [
// test with any option activated
[
'mainPerson' => false,
'mainPersonLabel' => 'person',
'person1' => false,
'person1Label' => 'person2',
'person2' => false,
'person2Label' => 'person2',
'thirdParty' => false,
'thirdPartyLabel' => '3party'
],
$period,
[
'mainPerson' => null,
'person1' => null,
'person2' => null,
'thirdParty' => null,
],
[
'thirdParty' => null,
'mainPerson' => null,
'person1' => null,
'person2' => null,
],
function (array $data) use ($period) {
self::assertArrayHasKey('course', $data);
self::assertEquals($period->getId(), $data['course']['id']);
},
];
}
}

View File

@@ -18,20 +18,30 @@ use Chill\DocStoreBundle\Entity\PersonDocument;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\PostalCode;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\ResidentialAddressRepository;
use Chill\PersonBundle\Service\DocGenerator\AccompanyingPeriodContext;
use Chill\PersonBundle\Service\DocGenerator\PersonContext;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Exception\Prediction\FailedPredictionException;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
@@ -46,10 +56,142 @@ use function count;
* @internal
* @coversNothing
*/
final class PersonContextTest extends TestCase
final class PersonContextTest extends KernelTestCase
{
use ProphecyTrait;
/**
* This test run the methods executed when a document is generated:
*
* - normalized data from the form in a way that they are stored in message queue;
* - denormalize the data from the message queue,
* - and get the data, as they will be transmitted to the GeneratorDriver
*
* @param array $options the options, as they are stored in the DocGeneratorTemplate (the admin form data)
* @param Person $entity The entity from which the data will be extracted
* @param array $data The data, from the public form
* @param array $expectedNormalized, how the normalized data are expected (allow to check that this data will be compliant with the storage in messenger queue)
* @param callable $assertionsOnData some test that will be executed on the normalized data
* @dataProvider provideNormalizedData
*/
public function testContextGenerationDataNormalizeDenormalizeGetData(
array $options,
Person $entity,
array $data,
array $expectedNormalized,
callable $assertionsOnData
): void {
// we boot kernel only for this test
self::bootKernel();
// we create a PersonContext with the minimal dependency injection needed (relying on
// prophecy for other dependencies)
$context = $this->buildPersonContext(
null,
self::$container->get(BaseContextData::class),
self::$container->get(CenterResolverManagerInterface::class),
self::$container->get(DocumentCategoryRepository::class),
self::$container->get(EntityManagerInterface::class),
self::$container->get(NormalizerInterface::class),
(new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => false]]])),
null,
self::$container->get(Security::class),
null,
null,
null,
self::$container->get(ThirdPartyRepository::class)
);
$template = new DocGeneratorTemplate();
$template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class)
->setDescription("description")->setActive(true)
->setOptions($options);
$normalized = $context->contextGenerationDataNormalize($template, $entity, $data);
self::assertEquals($expectedNormalized, $normalized);
$denormalized = $context->contextGenerationDataDenormalize($template, $entity, $normalized);
$data = $context->getData($template, $entity, $denormalized);
call_user_func($assertionsOnData, $data);
}
public function provideNormalizedData(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$thirdParty = $em->createQuery("SELECT t FROM " . ThirdParty::class . " t")
->setMaxResults(1)
->getSingleResult();
if (null === $thirdParty) {
throw new \RuntimeException("No thirdparty in database");
}
$person = $em->createQuery("SELECT p FROM " . Person::class . " p")
->setMaxResults(1)
->getSingleResult();
if (null === $person) {
throw new \RuntimeException("No confirmed period in database");
}
$category = self::$container->get(DocumentCategoryRepository::class)
->findAll()[0];
if (null === $category) {
throw new \RuntimeException("no document category in database");
}
yield [
[
'thirdParty' => true,
'thirdPartyLabel' => '3party',
'category' => $category,
],
$person,
[
'title' => 'test',
'thirdParty' => $thirdParty,
],
[
'thirdParty' => $thirdParty->getId(),
'title' => 'test',
'scope_id' => null,
],
function ($data) use ($person, $thirdParty) {
self::assertArrayHasKey('person', $data);
self::assertEquals($person->getId(), $data['person']['id']);
self::assertArrayHasKey('thirdParty', $data);
self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']);
}
];
yield [
[
'thirdParty' => false,
'thirdPartyLabel' => '3party',
'category' => $category,
],
$person,
[
'title' => 'test',
],
[
'title' => 'test',
'scope_id' => null,
'thirdParty' => null,
],
function ($data) use ($person, $thirdParty) {
self::assertArrayHasKey('person', $data);
self::assertEquals($person->getId(), $data['person']['id']);
}
];
}
/**
* Test that the build person context works in the case when 'form_show_scope' is false.
*/
@@ -208,9 +350,13 @@ final class PersonContextTest extends TestCase
?EntityManagerInterface $em = null,
?NormalizerInterface $normalizer = null,
?ParameterBagInterface $parameterBag = null,
?ScopeRepositoryInterface $scopeRepository = null,
?Security $security = null,
?TranslatorInterface $translator = null,
?TranslatableStringHelperInterface $translatableStringHelper = null
?TranslatableStringHelperInterface $translatableStringHelper = null,
?ThirdPartyRender $thirdPartyRender = null,
?ThirdPartyRepository $thirdPartyRepository = null,
?ResidentialAddressRepository $residentialAddressRepository = null
): PersonContext {
if (null === $authorizationHelper) {
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class)->reveal();
@@ -250,6 +396,11 @@ final class PersonContextTest extends TestCase
$parameterBag = new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => true]]]);
}
if (null === $scopeRepository) {
$scopeRepository = $this->prophesize(ScopeRepositoryInterface::class);
$scopeRepository = $scopeRepository->reveal();
}
if (null === $security) {
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
@@ -267,6 +418,28 @@ final class PersonContextTest extends TestCase
$translatableStringHelper = $translatableStringHelper->reveal();
}
if (null === $thirdPartyRender) {
$thirdPartyRender = $this->prophesize(ThirdPartyRender::class);
$thirdPartyRender = $thirdPartyRender->reveal();
}
if (null === $thirdPartyRepository) {
$thirdPartyRepository = $this->prophesize(ThirdPartyRepository::class);
$thirdPartyRepository = $thirdPartyRepository->reveal();
}
if (null === $residentialAddressRepository) {
$residentialAddressRepository = $this->prophesize(ResidentialAddressRepository::class);
$residentialAddressRepository->findCurrentResidentialAddressByPerson(Argument::type(Person::class), Argument::any())
->willReturn([
(new Person\ResidentialAddress())
->setAddress((new Address())
->setStreet('test street')
->setPostcode(new PostalCode()))
]);
$residentialAddressRepository = $residentialAddressRepository->reveal();
}
return new PersonContext(
$authorizationHelper,
$baseContextData,
@@ -275,9 +448,13 @@ final class PersonContextTest extends TestCase
$em,
$normalizer,
$parameterBag,
$scopeRepository,
$security,
$translator,
$translatableStringHelper
$translatableStringHelper,
$thirdPartyRender,
$thirdPartyRepository,
$residentialAddressRepository
);
}
}

View File

@@ -0,0 +1,134 @@
<?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 Service\GenericDoc\Providers;
use Chill\CalendarBundle\Security\Voter\CalendarVoter;
use Chill\CalendarBundle\Service\GenericDoc\Providers\AccompanyingPeriodCalendarGenericDocProvider;
use Chill\DocStoreBundle\GenericDoc\FetchQueryToSqlBuilder;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
/**
* @internal
* @coversNothing
*/
class AccompanyingPeriodCalendarGenericDocProviderTest extends KernelTestCase
{
use ProphecyTrait;
private Security $security;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->security = self::$container->get(Security::class);
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
/**
* @dataProvider provideDataForAccompanyingPeriod
*/
public function testBuildFetchQueryForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod, ?\DateTimeImmutable $startDate, ?\DateTimeImmutable $endDate, ?string $content): void
{
$provider = new AccompanyingPeriodCalendarGenericDocProvider($this->security, $this->entityManager);
$query = $provider->buildFetchQueryForAccompanyingPeriod($accompanyingPeriod, $startDate, $endDate, $content);
['sql' => $sql, 'params' => $params, 'types' => $types] = (new FetchQueryToSqlBuilder())->toSql($query);
$nb = $this->entityManager->getConnection()->fetchOne("SELECT COUNT(*) FROM ({$sql}) AS sq", $params, $types);
self::assertIsInt($nb);
}
/**
* @dataProvider provideDataForPerson
*/
public function testBuildFetchQueryForPerson(Person $person, ?\DateTimeImmutable $startDate, ?\DateTimeImmutable $endDate, ?string $content): void
{
$security = $this->prophesize(Security::class);
$security->isGranted(CalendarVoter::SEE, Argument::any())->willReturn(true);
$provider = new AccompanyingPeriodCalendarGenericDocProvider($security->reveal(), $this->entityManager);
$query = $provider->buildFetchQueryForPerson($person, $startDate, $endDate, $content);
['sql' => $sql, 'params' => $params, 'types' => $types] = (new FetchQueryToSqlBuilder())->toSql($query);
$nb = $this->entityManager->getConnection()->fetchOne("SELECT COUNT(*) FROM ({$sql}) AS sq", $params, $types);
self::assertIsInt($nb);
self::assertStringNotContainsStringIgnoringCase('FALSE = TRUE', $sql);
self::assertStringNotContainsStringIgnoringCase('TRUE = FALSE', $sql);
}
/**
* @dataProvider provideDataForPerson
*/
public function testBuildFetchQueryForPersonWithoutAnyRight(Person $person, ?\DateTimeImmutable $startDate, ?\DateTimeImmutable $endDate, ?string $content): void
{
$security = $this->prophesize(Security::class);
$security->isGranted(CalendarVoter::SEE, Argument::any())->willReturn(false);
$provider = new AccompanyingPeriodCalendarGenericDocProvider($security->reveal(), $this->entityManager);
$query = $provider->buildFetchQueryForPerson($person, $startDate, $endDate, $content);
['sql' => $sql, 'params' => $params, 'types' => $types] = (new FetchQueryToSqlBuilder())->toSql($query);
$nb = $this->entityManager->getConnection()->fetchOne("SELECT COUNT(*) FROM ({$sql}) AS sq", $params, $types);
self::assertIsInt($nb);
self::assertStringContainsStringIgnoringCase('TRUE = FALSE', $sql);
}
public function provideDataForPerson(): iterable
{
$this->setUp();
if (null === $person = $this->entityManager->createQuery("SELECT p FROM " . Person::class . " p WHERE SIZE(p.accompanyingPeriodParticipations) > 0")
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("There is no person");
}
yield [$person, null, null, null];
yield [$person, new \DateTimeImmutable("1 year ago"), null, null];
yield [$person, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), null];
yield [$person, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), "text"];
yield [$person, null, null, "text"];
yield [$person, null, new \DateTimeImmutable("6 month ago"), null];
}
public function provideDataForAccompanyingPeriod(): iterable
{
$this->setUp();
if (null === $period = $this->entityManager->createQuery("SELECT p FROM " . AccompanyingPeriod::class . " p ")
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("There is no accompanying period");
}
yield [$period, null, null, null];
yield [$period, new \DateTimeImmutable("1 year ago"), null, null];
yield [$period, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), null];
yield [$period, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), "text"];
yield [$period, null, null, "text"];
yield [$period, null, new \DateTimeImmutable("6 month ago"), null];
}
}

View File

@@ -0,0 +1,79 @@
<?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 Service\GenericDoc\Providers;
use Chill\DocStoreBundle\GenericDoc\FetchQueryToSqlBuilder;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Service\GenericDoc\Providers\AccompanyingPeriodWorkEvaluationGenericDocProvider;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
/**
* @internal
* @coversNothing
*/
class AccompanyingPeriodWorkEvaluationGenericDocProviderTest extends KernelTestCase
{
use ProphecyTrait;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
/**
* @dataProvider provideSearchArguments
*/
public function testBuildFetchQueryForAccompanyingPeriod(
?\DateTimeImmutable $startDate = null,
?\DateTimeImmutable $endDate = null,
?string $content = null
): void {
$period = $this->entityManager->createQuery("SELECT a FROM " . AccompanyingPeriod::class . ' a')
->setMaxResults(1)
->getSingleResult();
if (null === $period) {
throw new \RuntimeException('no accompanying period in databasee');
}
$security = $this->prophesize(Security::class);
$provider = new AccompanyingPeriodWorkEvaluationGenericDocProvider(
$security->reveal(),
$this->entityManager
);
$query = $provider->buildFetchQueryForAccompanyingPeriod($period, $startDate, $endDate, $content);
['sql' => $sql, 'params' => $params, 'types' => $types] = (new FetchQueryToSqlBuilder())->toSql($query);
$nb = $this->entityManager->getConnection()->executeQuery(
'SELECT COUNT(*) FROM ('.$sql.') AS sq',
$params,
$types
)->fetchOne();
self::assertIsInt($nb, "Test that there are no errors");
}
public function provideSearchArguments(): iterable
{
yield [null, null, null];
yield [new \DateTimeImmutable('1 month ago'), null, null];
yield [new \DateTimeImmutable('1 month ago'), new \DateTimeImmutable('now'), null];
yield [null, null, 'test'];
}
}

View File

@@ -0,0 +1,70 @@
<?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 Service\GenericDoc\Providers;
use Chill\CalendarBundle\Service\GenericDoc\Providers\PersonCalendarGenericDocProvider;
use Chill\DocStoreBundle\GenericDoc\FetchQueryToSqlBuilder;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
/**
* @internal
* @coversNothing
*/
class PersonCalendarGenericDocProviderTest extends KernelTestCase
{
private Security $security;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->security = self::$container->get(Security::class);
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
/**
* @dataProvider provideDataForPerson
*/
public function testBuildFetchQueryForPerson(Person $person, ?\DateTimeImmutable $startDate, ?\DateTimeImmutable $endDate, ?string $content): void
{
$provider = new PersonCalendarGenericDocProvider($this->security, $this->entityManager);
$query = $provider->buildFetchQueryForPerson($person, $startDate, $endDate, $content);
['sql' => $sql, 'params' => $params, 'types' => $types] = (new FetchQueryToSqlBuilder())->toSql($query);
$nb = $this->entityManager->getConnection()->fetchOne("SELECT COUNT(*) FROM ({$sql}) AS sq", $params, $types);
self::assertIsInt($nb);
}
public function provideDataForPerson(): iterable
{
$this->setUp();
if (null === $person = $this->entityManager->createQuery("SELECT p FROM " . Person::class . " p ")
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException("There is no person");
}
yield [$person, null, null, null];
yield [$person, new \DateTimeImmutable("1 year ago"), null, null];
yield [$person, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), null];
yield [$person, new \DateTimeImmutable("1 year ago"), new \DateTimeImmutable("6 month ago"), "text"];
yield [$person, null, null, "text"];
yield [$person, null, new \DateTimeImmutable("6 month ago"), null];
}
}