From a2160bef7dbd495562451dc4c6391360312cc618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 27 Apr 2021 19:33:34 +0200 Subject: [PATCH] WIP: first implementation for test --- .../AccompanyingPeriodRepository.php | 17 +++ .../AccompanyingCourseControllerTest.php | 120 ++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php diff --git a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php index 4bc9f92db..d8c8acb5d 100644 --- a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php @@ -23,6 +23,7 @@ namespace Chill\PersonBundle\Repository; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\Person; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -39,4 +40,20 @@ class AccompanyingPeriodRepository extends ServiceEntityRepository parent::__construct($registry, AccompanyingPeriod::class); } + /** + * @return array|AccompanyingPeriod[] + */ + public function findByPerson(Person $person, $orderBy = null, $limit = null, $offset = null): array + { + $qb = $this->createQueryBuilder('ap'); + $qb->join('qb.participations', 'participation') + ->where($qb->expr()->eq('participation.person', ':person')) + ->orderBy($orderBy) + ->setMaxResults($limit) + ->setFirstResult($offset) + ; + + return $qb->getQuery()->getResult(); + } + } diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php new file mode 100644 index 000000000..d3d7defe3 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php @@ -0,0 +1,120 @@ +, + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\PersonBundle\Tests\Controller; + + +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\Person; +use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\Center; +use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\EntityManagerInterface; + +/** + * Test api for AccompanyingCourseControllerTest + */ +class AccompanyingCourseControllerTest extends WebTestCase +{ + protected static EntityManagerInterface $em; + + /** + * Setup before the first test of this class (see phpunit doc) + */ + public static function setUpBeforeClass() + { + static::bootKernel(); + static::$em = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager'); + } + + /** + * Setup before each test method (see phpunit doc) + */ + public function setUp() + { + $this->client = static::createClient(array(), array( + 'PHP_AUTH_USER' => 'center a_social', + 'PHP_AUTH_PW' => 'password', + )); + + $center = static::$em->getRepository(Center::class) + ->findOneBy(array('name' => 'Center A')); + + $this->person = (new Person(new \DateTime('2015-01-05'))) + ->setFirstName('Roland') + ->setLastName('Gallorime') + ->setCenter($center) + ->setGender(Person::MALE_GENDER); + + static::$em->persist($this->person); + static::$em->flush(); + } + + /** + * + * @dataProvider dataGenerateRandomAccompanyingCourse + */ + public function testShowAccompanyingPeriod(AccompanyingPeriod $period) + { + $response = $this->client->request(sprintf('/fr/person/api/1.0/accompanying-period/%d/show.json', $period->getId())); + + $this->assert(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)"); + + $data = \json_decode($response->getContent()); + $this->assertEquals($data->id, $period->getId(), + "test that the response's data contains the id of the period" + ); + } + + public function dataGenerateRandomAccompanyingCourse() + { + $em = static::$em; + $center = $em->getRepository(Center::class) + ->findOneBy(array('name' => 'Center A')); + + $personIds = $em->createQuery("SELECT p.id FROM ". + Person::class. + " WHERE p.center_id = :centerId ") + ->setParameter('centerId', $center->getId()) + ->setMaxResults(100) + ->getScalarResult(); + + shuffle($personIds); + + $nbGenerated = 0; + $maxGenerated = 15; + + while ($nbGenerated < $maxGenerated) { + $id = \array_pop($personIds); + + $person = $em->getRepository(Person::class) + ->find($id); + $periods = $em->getRepository(AccompanyingPeriod::class) + ->findByPerson($person); + + yield $periods[\array_rand($periods)]; + } + } + +}