upgrade phpunit: make data provider static

This commit is contained in:
2024-02-14 12:28:14 +01:00
parent d5115b3718
commit f251e6f100
57 changed files with 142 additions and 121 deletions

View File

@@ -140,20 +140,23 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
}
}
public function provideDataFindByUserAndPostalCodesOpenedAccompanyingPeriod(): iterable
public static function provideDataFindByUserAndPostalCodesOpenedAccompanyingPeriod(): iterable
{
$this->setUp();
self::bootKernel();
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$scopeRepository = self::getContainer()->get(ScopeRepositoryInterface::class);
$centerRepository = self::getContainer()->get(CenterRepositoryInterface::class);
if (null === $user = $this->entityManager->createQuery('SELECT u FROM '.User::class.' u')->setMaxResults(1)->getSingleResult()) {
if (null === $user = $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())
if (null === $anotherUser = $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');
}
$persons = $this->entityManager
$persons = $entityManager
->createQuery('SELECT p FROM '.Person::class.' p JOIN p.centerCurrent current_center')
->setMaxResults(4)
->getResult();
@@ -165,7 +168,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
/** @var Person $person */
[$person, $anotherPerson, $person2, $person3] = $persons;
$scopes = $this->scopeRepository->findAll();
$scopes = $scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException('not enough scopes for this test');
@@ -173,14 +176,14 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
$scopesCanSee = [$scopes[0]];
$scopesGroup2 = [$scopes[1]];
$centers = $this->centerRepository->findActive();
$centers = $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 = self::buildPeriod($person, $scopesCanSee, $user, true);
$period->setUser($user);
yield [
@@ -233,7 +236,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
'period should not be visible for user having right in another scope (with multiple centers)',
];
$period = $this->buildPeriod($person, $scopesCanSee, $user, true);
$period = self::buildPeriod($person, $scopesCanSee, $user, true);
$period->setUser($user);
$period->setConfidential(true);
@@ -267,7 +270,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
'period confidential be visible if user has required scopes',
];
$this->entityManager->flush();
$entityManager->flush();
}
/**
@@ -314,21 +317,24 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
}
}
public function provideDataFindByUndispatched(): iterable
public static function provideDataFindByUndispatched(): iterable
{
$this->setUp();
self::bootKernel();
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$scopeRepository = self::getContainer()->get(ScopeRepositoryInterface::class);
$centerRepository = self::getContainer()->get(CenterRepositoryInterface::class);
if (null === $user = $this->entityManager->createQuery('SELECT u FROM '.User::class.' u')->setMaxResults(1)->getSingleResult()) {
if (null === $user = $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())
if (null === $anotherUser = $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 */
$persons = $this->entityManager
$persons = $entityManager
->createQuery('SELECT p FROM '.Person::class.' p ')
->setMaxResults(4)
->getResult();
@@ -339,7 +345,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
[$person, $anotherPerson, $person2, $person3] = $persons;
$scopes = $this->scopeRepository->findAll();
$scopes = $scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException('not enough scopes for this test');
@@ -347,13 +353,13 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
$scopesCanSee = [$scopes[0]];
$scopesGroup2 = [$scopes[1]];
$centers = $this->centerRepository->findActive();
$centers = $centerRepository->findActive();
if (2 > count($centers)) {
throw new \RuntimeException('not enough centers for this test');
}
$period = $this->buildPeriod($person, $scopesCanSee, $user, true);
$period = self::buildPeriod($person, $scopesCanSee, $user, true);
// expected scope: can see the period
yield [
@@ -405,7 +411,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
'period should not be visible for user having right in another scope (with multiple centers)',
];
$this->entityManager->flush();
$entityManager->flush();
}
/**
@@ -445,20 +451,22 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
}
}
public function provideDataForFindByPerson(): iterable
public static function provideDataForFindByPerson(): iterable
{
$this->setUp();
self::bootKernel();
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$scopeRepository = self::getContainer()->get(ScopeRepositoryInterface::class);
if (null === $user = $this->entityManager->createQuery('SELECT u FROM '.User::class.' u')->setMaxResults(1)->getSingleResult()) {
if (null === $user = $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())
if (null === $anotherUser = $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');
}
if (null === $centerA = $this->entityManager->createQuery('SELECT c FROM '.Center::class.' c WHERE c.name LIKE :cn')->setParameter('cn', 'Center A')
if (null === $centerA = $entityManager->createQuery('SELECT c FROM '.Center::class.' c WHERE c.name LIKE :cn')->setParameter('cn', 'Center A')
->setMaxResults(1)->getSingleResult()) {
throw new \RuntimeException('Center A not found');
}
@@ -473,13 +481,13 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
->setBirthdate((new \DateTime('today'))->sub(new \DateInterval('P'.random_int(18, 100).'Y')))
->setCenter($centerA)
;
$this->entityManager->persist($p);
$entityManager->persist($p);
self::$entitiesToDelete[] = [Person::class, $p->getId()];
}
[$person, $anotherPerson, $person2, $person3] = $persons;
$scopes = $this->scopeRepository->findAll();
$scopes = $scopeRepository->findAll();
if (3 > count($scopes)) {
throw new \RuntimeException('not enough scopes for this test');
@@ -488,7 +496,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
$scopesGroup2 = [$scopes[1]];
// case: a period is in draft state
$period = $this->buildPeriod($person, $scopesCanSee, $user, false);
$period = self::buildPeriod($person, $scopesCanSee, $user, false);
yield [$user, $person, $scopesCanSee, [], [$period], 'a user can see his period during draft state'];
@@ -496,7 +504,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
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);
$period = self::buildPeriod($anotherPerson, $scopesCanSee, $user, true);
// the other user can now see it
yield [$user, $anotherPerson, $scopesCanSee, [], [$period], 'a user see his period when confirmed'];
@@ -504,7 +512,7 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
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 = self::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'];
@@ -513,16 +521,19 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
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);
$period = self::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();
$entityManager->flush();
}
/**
* @param array<Scope> $scopes
*/
private function buildPeriod(Person $person, array $scopes, User|null $creator, bool $confirm): AccompanyingPeriod
private static function buildPeriod(Person $person, array $scopes, User|null $creator, bool $confirm): AccompanyingPeriod
{
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$registry = self::getContainer()->get(Registry::class);
$period = new AccompanyingPeriod();
$period->addPerson($person);
if (null !== $creator) {
@@ -533,11 +544,11 @@ class AccompanyingPeriodACLAwareRepositoryTest extends KernelTestCase
$period->addScope($scope);
}
$this->entityManager->persist($period);
$entityManager->persist($period);
self::$periodsIdsToDelete[] = $period->getId();
if ($confirm) {
$workflow = $this->registry->get($period, 'accompanying_period_lifecycle');
$workflow = $registry->get($period, 'accompanying_period_lifecycle');
$workflow->apply($period, 'confirm');
}