mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
Add test for AccompanyingCourseController
This commit is contained in:
parent
a2160bef7d
commit
f7c508939c
@ -46,11 +46,13 @@ class AccompanyingPeriodRepository extends ServiceEntityRepository
|
|||||||
public function findByPerson(Person $person, $orderBy = null, $limit = null, $offset = null): array
|
public function findByPerson(Person $person, $orderBy = null, $limit = null, $offset = null): array
|
||||||
{
|
{
|
||||||
$qb = $this->createQueryBuilder('ap');
|
$qb = $this->createQueryBuilder('ap');
|
||||||
$qb->join('qb.participations', 'participation')
|
$qb->join('ap.participations', 'participation')
|
||||||
->where($qb->expr()->eq('participation.person', ':person'))
|
->where($qb->expr()->eq('participation.person', ':person'))
|
||||||
->orderBy($orderBy)
|
|
||||||
->setMaxResults($limit)
|
// ->orderBy($orderBy)
|
||||||
->setFirstResult($offset)
|
// ->setMaxResults($limit)
|
||||||
|
//->setFirstResult($offset ?? 0)
|
||||||
|
->setParameter('person', $person)
|
||||||
;
|
;
|
||||||
|
|
||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
|
@ -25,11 +25,13 @@ namespace Chill\PersonBundle\Tests\Controller;
|
|||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||||
|
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use Chill\MainBundle\Entity\Center;
|
use Chill\MainBundle\Entity\Center;
|
||||||
use Doctrine\Common\Collections\Criteria;
|
use Doctrine\Common\Collections\Criteria;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test api for AccompanyingCourseControllerTest
|
* Test api for AccompanyingCourseControllerTest
|
||||||
@ -44,8 +46,6 @@ class AccompanyingCourseControllerTest extends WebTestCase
|
|||||||
public static function setUpBeforeClass()
|
public static function setUpBeforeClass()
|
||||||
{
|
{
|
||||||
static::bootKernel();
|
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_USER' => 'center a_social',
|
||||||
'PHP_AUTH_PW' => 'password',
|
'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
|
* @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());
|
$data = \json_decode($response->getContent());
|
||||||
$this->assertEquals($data->id, $period->getId(),
|
$this->assertEquals($data->id, $period->getId(),
|
||||||
"test that the response's data contains the id of the period"
|
"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()
|
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)
|
$center = $em->getRepository(Center::class)
|
||||||
->findOneBy(array('name' => 'Center A'));
|
->findOneBy(array('name' => 'Center A'));
|
||||||
|
|
||||||
$personIds = $em->createQuery("SELECT p.id FROM ".
|
$personIds = $em->createQuery("SELECT p.id FROM ".
|
||||||
Person::class.
|
Person::class." p ".
|
||||||
" WHERE p.center_id = :centerId ")
|
" WHERE p.center = :center")
|
||||||
->setParameter('centerId', $center->getId())
|
->setParameter('center', $center)
|
||||||
->setMaxResults(100)
|
->setMaxResults($maxResults)
|
||||||
->getScalarResult();
|
->getScalarResult();
|
||||||
|
|
||||||
|
// create a random order
|
||||||
shuffle($personIds);
|
shuffle($personIds);
|
||||||
|
|
||||||
$nbGenerated = 0;
|
$nbGenerated = 0;
|
||||||
$maxGenerated = 15;
|
|
||||||
|
|
||||||
while ($nbGenerated < $maxGenerated) {
|
while ($nbGenerated < $maxGenerated) {
|
||||||
$id = \array_pop($personIds);
|
$id = \array_pop($personIds)["id"];
|
||||||
|
|
||||||
$person = $em->getRepository(Person::class)
|
$person = $em->getRepository(Person::class)
|
||||||
->find($id);
|
->find($id);
|
||||||
$periods = $em->getRepository(AccompanyingPeriod::class)
|
$periods = $em->getRepository(AccompanyingPeriod::class)
|
||||||
->findByPerson($person);
|
->findByPerson($person);
|
||||||
|
|
||||||
yield $periods[\array_rand($periods)];
|
yield [\array_pop($personIds)["id"], $periods[\array_rand($periods)] ];
|
||||||
|
|
||||||
|
$nbGenerated++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,3 +18,8 @@ services:
|
|||||||
tags: [ doctrine.repository_service ]
|
tags: [ doctrine.repository_service ]
|
||||||
arguments:
|
arguments:
|
||||||
- '@Doctrine\Persistence\ManagerRegistry'
|
- '@Doctrine\Persistence\ManagerRegistry'
|
||||||
|
|
||||||
|
Chill\PersonBundle\Repository\AccompanyingPeriodParticipationRepository:
|
||||||
|
arguments:
|
||||||
|
- '@Doctrine\Persistence\ManagerRegistry'
|
||||||
|
tags: [ doctrine.repository_service ]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user