From 19fdf2a50351ea16874d2bb037d122e210f7fa57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 27 Apr 2021 22:40:00 +0200 Subject: [PATCH] Add test for AccompanyingCourseController --- .../AccompanyingPeriodRepository.php | 1 - .../AccompanyingCourseControllerTest.php | 174 ++++++++++++++++++ .../config/services/repository.yaml | 5 + 3 files changed, 179 insertions(+), 1 deletion(-) 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..849e4391a 100644 --- a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriodRepository.php @@ -38,5 +38,4 @@ class AccompanyingPeriodRepository extends ServiceEntityRepository { parent::__construct($registry, AccompanyingPeriod::class); } - } diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php new file mode 100644 index 000000000..c45cb93d9 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/AccompanyingCourseControllerTest.php @@ -0,0 +1,174 @@ +, + * + * 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\AccompanyingPeriodParticipation; +use Chill\PersonBundle\Entity\Person; +use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\Center; +use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\HttpFoundation\Request; + +/** + * 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(); + } + + /** + * 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', + )); + } + + /** + * + * @dataProvider dataGenerateRandomAccompanyingCourse + */ + public function testAccompanyingCourseShow(int $personId, AccompanyingPeriod $period) + { + $this->client->request(Request::METHOD_GET, sprintf('/fr/person/api/1.0/accompanying-course/%d/show.json', $period->getId())); + $response = $this->client->getResponse(); + + $this->assertEquals(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" + ); + $this->assertGreaterThan(0, $data->participations); + } + + /** + * + * @dataProvider dataGenerateRandomAccompanyingCourse + */ + public function testAccompanyingCourseAddParticipation(int $personId, AccompanyingPeriod $period) + { + $this->client->request( + Request::METHOD_POST, + sprintf('/fr/person/api/1.0/accompanying-course/%d/participation.json', $period->getId()), + [], // parameters + [], // files + [], // server parameters + \json_encode([ 'id' => $personId ]) + ); + $response = $this->client->getResponse(); + + $this->assertEquals(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)"); + $this->client->request(Request::METHOD_GET, sprintf('/fr/person/api/1.0/accompanying-course/%d/show.json', $period->getId())); + + $response = $this->client->getResponse(); + $data = \json_decode($response->getContent()); + + $participationsPersonsIds = \array_map( + function($participation) { return $participation->person->id; }, + $data->participations); + + $this->assertContains($personId, $participationsPersonsIds); + + $this->personId = $personId; + $this->period = $period; + } + + protected function tearDown() + { + // remove participation created during test 'testAccompanyingCourseAddParticipation' + + $testAddParticipationName = 'testAccompanyingCourseAddParticipation'; + + if ($testAddParticipationName !== \substr($this->getName(), 0, \strlen($testAddParticipationName))) { + return; + } + + $em = static::$container->get(EntityManagerInterface::class); + + $participation = $em + ->getRepository(AccompanyingPeriodParticipation::class) + ->findOneBy(['person' => $this->personId, 'accompanyingPeriod' => $this->period]) + ; + + $em->remove($participation); + $em->flush(); + } + + public function dataGenerateRandomAccompanyingCourse() + { + // note about max result for person query, and maxGenerated: + // + // in the final loop, an id is popped out of the personIds array twice: + // + // * one for getting the person, which will in turn provide his accompanying period; + // * one for getting the personId to populate to the data manager + // + // Ensure to keep always $maxGenerated to the double of $maxResults + $maxGenerated = 1; + $maxResults = 15 * 8; + + static::bootKernel(); + $em = static::$container->get(EntityManagerInterface::class); + $center = $em->getRepository(Center::class) + ->findOneBy(array('name' => 'Center A')); + + $personIds = $em->createQuery("SELECT p.id FROM ". + Person::class." p ". + " WHERE p.center = :center") + ->setParameter('center', $center) + ->setMaxResults($maxResults) + ->getScalarResult(); + + // create a random order + shuffle($personIds); + + $nbGenerated = 0; + while ($nbGenerated < $maxGenerated) { + $id = \array_pop($personIds)["id"]; + + $person = $em->getRepository(Person::class) + ->find($id); + $periods = $person->getAccompanyingPeriods(); + + yield [\array_pop($personIds)["id"], $periods[\array_rand($periods)] ]; + + $nbGenerated++; + } + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/repository.yaml b/src/Bundle/ChillPersonBundle/config/services/repository.yaml index 8cfaa8471..e899ba9e1 100644 --- a/src/Bundle/ChillPersonBundle/config/services/repository.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/repository.yaml @@ -18,3 +18,8 @@ services: tags: [ doctrine.repository_service ] arguments: - '@Doctrine\Persistence\ManagerRegistry' + + Chill\PersonBundle\Repository\AccompanyingPeriodParticipationRepository: + arguments: + - '@Doctrine\Persistence\ManagerRegistry' + tags: [ doctrine.repository_service ]