Add test for AccompanyingCourseController

This commit is contained in:
Julien Fastré 2021-04-27 22:40:00 +02:00
parent a2160bef7d
commit f7c508939c
3 changed files with 94 additions and 32 deletions

View File

@ -46,11 +46,13 @@ class AccompanyingPeriodRepository extends ServiceEntityRepository
public function findByPerson(Person $person, $orderBy = null, $limit = null, $offset = null): array
{
$qb = $this->createQueryBuilder('ap');
$qb->join('qb.participations', 'participation')
$qb->join('ap.participations', 'participation')
->where($qb->expr()->eq('participation.person', ':person'))
->orderBy($orderBy)
->setMaxResults($limit)
->setFirstResult($offset)
// ->orderBy($orderBy)
// ->setMaxResults($limit)
//->setFirstResult($offset ?? 0)
->setParameter('person', $person)
;
return $qb->getQuery()->getResult();

View File

@ -25,11 +25,13 @@ 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
@ -44,8 +46,6 @@ class AccompanyingCourseControllerTest extends WebTestCase
public static function setUpBeforeClass()
{
static::bootKernel();
static::$em = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager');
}
/**
@ -57,64 +57,119 @@ class AccompanyingCourseControllerTest extends WebTestCase
'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)
public function testAccompanyingCourseShow(int $personId, AccompanyingPeriod $period)
{
$response = $this->client->request(sprintf('/fr/person/api/1.0/accompanying-period/%d/show.json', $period->getId()));
$this->client->request(Request::METHOD_GET, sprintf('/fr/person/api/1.0/accompanying-course/%d/show.json', $period->getId()));
$response = $this->client->getResponse();
$this->assert(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)");
$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()
{
$em = static::$em;
// 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.
" WHERE p.center_id = :centerId ")
->setParameter('centerId', $center->getId())
->setMaxResults(100)
Person::class." p ".
" WHERE p.center = :center")
->setParameter('center', $center)
->setMaxResults($maxResults)
->getScalarResult();
// create a random order
shuffle($personIds);
$nbGenerated = 0;
$maxGenerated = 15;
while ($nbGenerated < $maxGenerated) {
$id = \array_pop($personIds);
$id = \array_pop($personIds)["id"];
$person = $em->getRepository(Person::class)
->find($id);
$periods = $em->getRepository(AccompanyingPeriod::class)
->findByPerson($person);
yield $periods[\array_rand($periods)];
yield [\array_pop($personIds)["id"], $periods[\array_rand($periods)] ];
$nbGenerated++;
}
}
}

View File

@ -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 ]