From f02873c6e07282801fad86674699d76cf47c636e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 8 Dec 2023 11:07:14 +0100 Subject: [PATCH] Refactor RelationshipApiControllerTest The codebase has been improved by refining and optimizing the test methods in the RelationshipApiControllerTest class. The check for relations was also added to exclude persons without any relationship, this may exclude duplicate relations. This change should avoid errors on this test. --- .../RelationshipApiControllerTest.php | 52 ++++++++----------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/RelationshipApiControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/RelationshipApiControllerTest.php index 7e191b91e..171dedacb 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/RelationshipApiControllerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/RelationshipApiControllerTest.php @@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Tests\Controller; use Chill\MainBundle\Test\PrepareClientTrait; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Relationships\Relation; +use Chill\PersonBundle\Entity\Relationships\Relationship; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Request; @@ -43,31 +44,34 @@ final class RelationshipApiControllerTest extends WebTestCase { self::bootKernel(); $em = self::$container->get(EntityManagerInterface::class); - $countPersons = $em->createQueryBuilder() - ->select('count(p)') - ->from(Person::class, 'p') - ->join('p.centerCurrent', 'center_current') - ->join('center_current.center', 'c') - ->where('c.name LIKE :name') - ->setParameter('name', 'Center A') - ->getQuery() - ->getSingleScalarResult(); - $person = $em->createQueryBuilder() - ->select('p') + $personIdHavingRelation = $em->createQueryBuilder() + ->select('p.id') ->from(Person::class, 'p') ->join('p.centerCurrent', 'center_current') ->join('center_current.center', 'c') ->where('c.name LIKE :name') + ->andWhere('EXISTS (SELECT 1 FROM ' . Relationship::class . ' r WHERE r.fromPerson = p OR r.toPerson = p)') ->setParameter('name', 'Center A') ->getQuery() ->setMaxResults(1) - ->setFirstResult(\random_int(0, $countPersons - 1)) - ->getSingleResult(); + ->getSingleScalarResult(); + + $personIdWithoutRelation = $em->createQueryBuilder() + ->select('p.id') + ->from(Person::class, 'p') + ->join('p.centerCurrent', 'center_current') + ->join('center_current.center', 'c') + ->where('c.name LIKE :name') + ->andWhere('NOT EXISTS (SELECT 1 FROM ' . Relationship::class . ' r WHERE r.fromPerson = p OR r.toPerson = p)') + ->setParameter('name', 'Center A') + ->getQuery() + ->setMaxResults(1) + ->getSingleScalarResult(); self::ensureKernelShutdown(); return [ - [$person->getId()], + [$personIdHavingRelation, $personIdWithoutRelation], ]; } @@ -75,39 +79,29 @@ final class RelationshipApiControllerTest extends WebTestCase { self::bootKernel(); $em = self::$container->get(EntityManagerInterface::class); - $countPersons = $em->createQueryBuilder() - ->select('count(DISTINCT p)') - ->from(Person::class, 'p') - ->join('p.centerCurrent', 'center_current') - ->join('center_current.center', 'c') - ->where('c.name LIKE :name') - ->setParameter('name', 'Center A') - ->getQuery() - ->getSingleScalarResult(); - - $persons = $em->createQueryBuilder() - ->select('p') + $personIdWithoutRelations = $em->createQueryBuilder() + ->select('p.id') ->from(Person::class, 'p') ->join('p.centerCurrent', 'center_current') ->join('center_current.center', 'c') ->where('c.name LIKE :name') + ->andWhere('NOT EXISTS (SELECT 1 FROM ' . Relationship::class . ' r WHERE r.fromPerson = p OR r.toPerson = p)') ->setParameter('name', 'Center A') ->getQuery() ->setMaxResults(2) - ->setFirstResult(\random_int(0, $countPersons - 1)) ->getResult(); self::ensureKernelShutdown(); return [ - [$persons[0]->getId(), $persons[1]->getId(), $this->getRandomRelation($em)->getId(), true], + [$personIdWithoutRelations[0]['id'], $personIdWithoutRelations[1]['id'], $this->getRandomRelation($em)->getId(), true], ]; } /** * @dataProvider personProvider */ - public function testGetRelationshipByPerson(mixed $personId) + public function testGetRelationshipByPerson(int $personId) { self::ensureKernelShutdown(); $client = $this->getClientAuthenticated();