, * * 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)]; } } }