Change non-static class-level variables and methods to static in tests's data providers

The update modifies several test classes within the "chill-project" to change non-static class-level variables and methods to static ones. This change has been made to improve readability, performance, and to eliminate unnecessary instantiation of class objects in test scenarios. Also, flush and clear actions on the entity manager are moved to individual data providers.
This commit is contained in:
Julien Fastré 2024-02-19 15:38:28 +01:00
parent d0f23eb6b1
commit 0081146a78
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
11 changed files with 81 additions and 92 deletions

View File

@ -123,11 +123,12 @@ class PersonDocumentACLAwareRepositoryTest extends KernelTestCase
self::assertIsInt($nb, 'test that the query could be executed');
}
public function provideDateForFetchQueryForAccompanyingPeriod(): iterable
public static function provideDateForFetchQueryForAccompanyingPeriod(): iterable
{
$this->setUp();
self::bootKernel();
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
if (null === $period = $this->entityManager->createQuery(
if (null === $period = $entityManager->createQuery(
'SELECT p FROM '.AccompanyingPeriod::class.' p WHERE SIZE(p.participations) > 0'
)
->setMaxResults(1)->getSingleResult()) {

View File

@ -28,7 +28,7 @@ trait PrepareCenterTrait
/**
* prepare a mocked center, with and id and name given.
*/
protected function prepareCenter(int $id, string $name): Center
protected static function prepareCenter(int $id, string $name): Center
{
$center = new Center();
$center->setName($name);

View File

@ -28,7 +28,7 @@ trait PrepareScopeTrait
*
* The name will be present in both lang `fr` and `en`.
*/
protected function prepareScope(int $id, string $name): Scope
protected static function prepareScope(int $id, string $name): Scope
{
$scope = new Scope();
// set the name

View File

@ -50,7 +50,7 @@ trait PrepareUserTrait
*
* @throws \LogicException if the trait is not set up
*/
protected function prepareUser(array $permissions)
protected static function prepareUser(array $permissions)
{
$user = new User();

View File

@ -28,21 +28,23 @@ final class NotificationApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
private array $toDelete = [];
private static array $toDelete = [];
protected function tearDown(): void
{
$em = self::getContainer()->get(EntityManagerInterface::class);
foreach ($this->toDelete as [$className, $id]) {
foreach (self::$toDelete as [$className, $id]) {
$object = $em->find($className, $id);
$em->remove($object);
}
$em->flush();
self::$toDelete = [];
}
public function generateDataMarkAsRead()
public static function generateDataMarkAsRead()
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
@ -62,7 +64,9 @@ final class NotificationApiControllerTest extends WebTestCase
$em->refresh($notification);
$em->flush();
$this->toDelete[] = [Notification::class, $notification->getId()];
self::$toDelete[] = [Notification::class, $notification->getId()];
self::ensureKernelShutdown();
yield [$notification->getId()];
}

View File

@ -27,23 +27,7 @@ final class UserControllerTest extends WebTestCase
{
use PrepareClientTrait;
private array $toDelete = [];
protected function tearDown(): void
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
foreach ($this->toDelete as [$class, $id]) {
$obj = $em->getRepository($class)->find($id);
$em->remove($obj);
}
$em->flush();
self::ensureKernelShutdown();
}
public function dataGenerateUserId()
public static function dataGenerateUserId()
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
@ -58,11 +42,9 @@ final class UserControllerTest extends WebTestCase
$em->persist($user);
$em->flush();
$this->toDelete[] = [User::class, $user->getId()];
self::ensureKernelShutdown();
yield [$user->getId(), $user->getUsername()];
self::ensureKernelShutdown();
}
public function testList()

View File

@ -45,17 +45,17 @@ final class AuthorizationHelperTest extends KernelTestCase
self::bootKernel();
}
public function dataProvider_getReachableCenters()
public static function dataProvider_getReachableCenters()
{
self::bootKernel();
$centerA = $this->prepareCenter(1, 'center A');
$centerB = $this->prepareCenter(2, 'center B');
$scopeA = $this->prepareScope(1, 'scope default');
$scopeB = $this->prepareScope(2, 'scope B');
$scopeC = $this->prepareScope(3, 'scope C');
$centerA = self::prepareCenter(1, 'center A');
$centerB = self::prepareCenter(2, 'center B');
$scopeA = self::prepareScope(1, 'scope default');
$scopeB = self::prepareScope(2, 'scope B');
$scopeC = self::prepareScope(3, 'scope C');
$userA = $this->prepareUser([
$userA = self::prepareUser([
[
'center' => $centerA,
'permissionsGroup' => [
@ -72,7 +72,7 @@ final class AuthorizationHelperTest extends KernelTestCase
],
]);
$ah = $this->getAuthorizationHelper();
$ah = self::getAuthorizationHelper();
return [
// without scopes
@ -143,15 +143,15 @@ final class AuthorizationHelperTest extends KernelTestCase
];
}
public function dataProvider_getReachableScopes()
public static function dataProvider_getReachableScopes()
{
$centerA = $this->prepareCenter(1, 'center A');
$centerB = $this->prepareCenter(2, 'center B');
$scopeA = $this->prepareScope(1, 'scope default');
$scopeB = $this->prepareScope(2, 'scope B');
$scopeC = $this->prepareScope(3, 'scope C');
$centerA = self::prepareCenter(1, 'center A');
$centerB = self::prepareCenter(2, 'center B');
$scopeA = self::prepareScope(1, 'scope default');
$scopeB = self::prepareScope(2, 'scope B');
$scopeC = self::prepareScope(3, 'scope C');
$userA = $this->prepareUser([
$userA = self::prepareUser([
[
'center' => $centerA,
'permissionsGroup' => [
@ -579,7 +579,7 @@ final class AuthorizationHelperTest extends KernelTestCase
/**
* @return AuthorizationHelper
*/
private function getAuthorizationHelper()
private static function getAuthorizationHelper()
{
return self::getContainer()
->get(AuthorizationHelper::class);

View File

@ -174,15 +174,16 @@ class PersonMoveTest extends KernelTestCase
self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBSecond];
}
public function dataProviderMovePerson(): iterable
public static function dataProviderMovePerson(): iterable
{
$this->setUp();
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$personA = new Person();
$personB = new Person();
$this->em->persist($personA);
$this->em->persist($personB);
$em->persist($personA);
$em->persist($personB);
self::$entitiesToDelete[] = [Person::class, $personA];
self::$entitiesToDelete[] = [Person::class, $personB];
@ -197,9 +198,9 @@ class PersonMoveTest extends KernelTestCase
$activity->addPerson($personA);
$activity->addPerson($personB);
$this->em->persist($personA);
$this->em->persist($personB);
$this->em->persist($activity);
$em->persist($personA);
$em->persist($personB);
$em->persist($activity);
self::$entitiesToDelete[] = [Person::class, $personA];
self::$entitiesToDelete[] = [Person::class, $personB];
@ -219,11 +220,11 @@ class PersonMoveTest extends KernelTestCase
->setStartDate(new \DateTimeImmutable('2023-01-01'))
);
$this->em->persist($personA);
$this->em->persist($personB);
$this->em->persist($household);
$this->em->persist($memberA);
$this->em->persist($memberB);
$em->persist($personA);
$em->persist($personB);
$em->persist($household);
$em->persist($memberA);
$em->persist($memberB);
self::$entitiesToDelete[] = [HouseholdMember::class, $memberA];
self::$entitiesToDelete[] = [HouseholdMember::class, $memberB];
@ -240,9 +241,9 @@ class PersonMoveTest extends KernelTestCase
$parcours->addPerson($personA);
$parcours->addPerson($personB);
$this->em->persist($personA);
$this->em->persist($personB);
$this->em->persist($parcours);
$em->persist($personA);
$em->persist($personB);
$em->persist($parcours);
self::$entitiesToDelete[] = [Person::class, $personA];
self::$entitiesToDelete[] = [Person::class, $personB];
@ -262,11 +263,11 @@ class PersonMoveTest extends KernelTestCase
$relationship->setReverse(false);
$relationship->setCreatedBy($user);
$this->em->persist($personA);
$this->em->persist($personB);
$this->em->persist($relation);
$this->em->persist($user);
$this->em->persist($relationship);
$em->persist($personA);
$em->persist($personB);
$em->persist($relation);
$em->persist($user);
$em->persist($relationship);
self::$entitiesToDelete[] = [Person::class, $personA];
self::$entitiesToDelete[] = [Person::class, $personB];
@ -276,7 +277,7 @@ class PersonMoveTest extends KernelTestCase
yield [$personA, $personB, 'move 2 people with a relationship'];
$this->em->flush();
$this->em->clear();
$em->flush();
$em->clear();
}
}

View File

@ -31,7 +31,7 @@ final class HouseholdApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
private array $toDelete = [];
private static array $toDelete = [];
protected function tearDown(): void
{
@ -39,17 +39,17 @@ final class HouseholdApiControllerTest extends WebTestCase
$em = self::getContainer()->get(EntityManagerInterface::class);
foreach ($this->toDelete as [$class, $id]) {
foreach (self::$toDelete as [$class, $id]) {
$obj = $em->getRepository($class)->find($id);
$em->remove($obj);
}
$em->flush();
self::$toDelete = [];
self::ensureKernelShutdown();
}
public function generateHouseholdAssociatedWithAddressReference()
public static function generateHouseholdAssociatedWithAddressReference()
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
@ -80,7 +80,7 @@ final class HouseholdApiControllerTest extends WebTestCase
$em->flush();
$this->toDelete += [
self::$toDelete += [
[HouseholdMember::class, $m->getId()],
[User::class, $p->getId()],
[Household::class, $h->getId()],

View File

@ -31,9 +31,9 @@ final class RelationshipApiControllerTest extends WebTestCase
/**
* A cache for all relations.
*
* @var array|Relation[]|null
* @var array<int, Relation>|null
*/
private ?array $relations = null;
private static ?array $relations = null;
protected function tearDown(): void
{
@ -75,7 +75,7 @@ final class RelationshipApiControllerTest extends WebTestCase
];
}
public function relationProvider(): array
public static function relationProvider(): array
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
@ -94,7 +94,7 @@ final class RelationshipApiControllerTest extends WebTestCase
self::ensureKernelShutdown();
return [
[$personIdWithoutRelations[0]['id'], $personIdWithoutRelations[1]['id'], $this->getRandomRelation($em)->getId(), true],
[$personIdWithoutRelations[0]['id'], $personIdWithoutRelations[1]['id'], self::getRandomRelation($em)->getId(), true],
];
}
@ -137,13 +137,13 @@ final class RelationshipApiControllerTest extends WebTestCase
$this->assertEquals(200, $response->getStatusCode());
}
private function getRandomRelation(EntityManagerInterface $em): Relation
private static function getRandomRelation(EntityManagerInterface $em): Relation
{
if (null === $this->relations) {
$this->relations = $em->getRepository(Relation::class)
if (null === self::$relations) {
self::$relations = $em->getRepository(Relation::class)
->findAll();
}
return $this->relations[\array_rand($this->relations)];
return self::$relations[\array_rand(self::$relations)];
}
}

View File

@ -28,43 +28,44 @@ final class SocialWorkEvaluationApiControllerTest extends WebTestCase
private EntityManagerInterface $em;
private ?Evaluation $evaluationToReset = null;
private static ?Evaluation $evaluationToReset = null;
protected function tearDown(): void
{
if (null === $this->evaluationToReset) {
if (null === self::$evaluationToReset) {
return;
}
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$evaluation = $em->find(Evaluation::class, $this->evaluationToReset->getId());
$evaluation = $em->find(Evaluation::class, self::$evaluationToReset->getId());
$evaluation->setActive(true);
$em->flush();
}
public function dataGenerateSocialActionWithEvaluations(): iterable
public static function dataGenerateSocialActionWithEvaluations(): iterable
{
self::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
$em = self::getContainer()->get(EntityManagerInterface::class);
/** @var SocialAction $socialAction */
$socialAction = $this->em->createQuery(
$socialAction = $em->createQuery(
'SELECT s FROM '.SocialAction::class.' s WHERE SIZE(s.evaluations) >= 2'
)
->setMaxResults(1)
->getSingleResult();
// set the first evaluation as inactive and save
$this->evaluationToReset = $socialAction->getEvaluations()->first();
$this->evaluationToReset->setActive(false);
self::$evaluationToReset = $socialAction->getEvaluations()->first();
self::$evaluationToReset->setActive(false);
$this->em->flush();
$em->flush();
yield [$socialAction, $this->evaluationToReset];
self::ensureKernelShutdown();
yield [$socialAction, self::$evaluationToReset];
}
/**