From c5250a10593d3d2e1c165413eaec07d1f77f6fe2 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 27 Apr 2021 23:07:59 +0200 Subject: [PATCH 1/3] Use injected EntityManager - fix property visibility. --- .../Controller/PersonController.php | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index a167dc589..445a17482 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -49,13 +49,11 @@ use Doctrine\ORM\EntityManagerInterface; class PersonController extends AbstractController { /** - * * @var SimilarPersonMatcher */ protected $similarPersonMatcher; /** - * * @var TranslatorInterface */ protected $translator; @@ -66,34 +64,31 @@ class PersonController extends AbstractController protected $eventDispatcher; /** - * * @var PersonRepository; */ protected $personRepository; /** - * * @var ConfigPersonAltNamesHelper */ protected $configPersonAltNameHelper; /** - * * @var EntityManagerInterface */ - protected $em; + private $em; /** * @var \Psr\Log\LoggerInterface */ private $logger; - + /** * @var ValidatorInterface */ private $validator; - public function __construct( + public function __construct( SimilarPersonMatcher $similarPersonMatcher, TranslatorInterface $translator, EventDispatcherInterface $eventDispatcher, @@ -117,8 +112,7 @@ class PersonController extends AbstractController { $cFGroup = null; - $em = $this->getDoctrine()->getManager(); - $cFDefaultGroup = $em->getRepository("ChillCustomFieldsBundle:CustomFieldsDefaultGroup") + $cFDefaultGroup = $this->em->getRepository("ChillCustomFieldsBundle:CustomFieldsDefaultGroup") ->findOneByEntity("Chill\PersonBundle\Entity\Person"); if($cFDefaultGroup) { @@ -207,8 +201,7 @@ class PersonController extends AbstractController ->trans('The person data has been updated') ); - $em = $this->getDoctrine()->getManager(); - $em->flush(); + $this->em->flush(); $url = $this->generateUrl('chill_person_view', array( 'person_id' => $person->getId() @@ -402,11 +395,9 @@ class PersonController extends AbstractController 'You are not allowed to create this person'); if ($errors->count() === 0) { - $em = $this->getDoctrine()->getManager(); + $this->em->persist($person); - $em->persist($person); - - $em->flush(); + $this->em->flush(); return $this->redirect($this->generateUrl('chill_person_general_edit', array('person_id' => $person->getId()))); From 5448238697256be9cd60773b53678c5adedc5b76 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 27 Apr 2021 23:11:53 +0200 Subject: [PATCH 2/3] Set final keyword - remove redundant information in phpdoc. --- .../ChillPersonBundle/Controller/PersonController.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index 445a17482..bfb32654e 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -41,12 +41,7 @@ use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Symfony\Component\Validator\Validator\ValidatorInterface; use Doctrine\ORM\EntityManagerInterface; -/** - * Class PersonController - * - * @package Chill\PersonBundle\Controller - */ -class PersonController extends AbstractController +final class PersonController extends AbstractController { /** * @var SimilarPersonMatcher 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 3/3] 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 ]