mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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()],
|
||||
|
@@ -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)];
|
||||
}
|
||||
}
|
||||
|
@@ -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];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user