fix tests (wip)

This commit is contained in:
Julien Fastré 2021-09-22 12:23:15 +02:00
parent e7928c222d
commit c382008b4d

View File

@ -23,6 +23,7 @@
namespace Chill\PersonBundle\Tests\Controller; namespace Chill\PersonBundle\Tests\Controller;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
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\AccompanyingPeriodParticipation;
@ -47,6 +48,8 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
protected ?AccompanyingPeriod $period = NULL; protected ?AccompanyingPeriod $period = NULL;
protected ?int $periodId = NULL;
/** /**
* Setup before the first test of this class (see phpunit doc) * Setup before the first test of this class (see phpunit doc)
*/ */
@ -70,15 +73,15 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
* *
* @dataProvider dataGenerateRandomAccompanyingCourse * @dataProvider dataGenerateRandomAccompanyingCourse
*/ */
public function testAccompanyingCourseShow(int $personId, AccompanyingPeriod $period) public function testAccompanyingCourseShow(int $personId, int $periodId)
{ {
$c = $this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId())); $c = $this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $periodId));
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(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, $periodId,
"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); $this->assertGreaterThan(0, $data->participations);
@ -326,14 +329,16 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
* *
* @dataProvider dataGenerateRandomAccompanyingCourse * @dataProvider dataGenerateRandomAccompanyingCourse
*/ */
public function testAccompanyingPeriodPatch(int $personId, AccompanyingPeriod $period) public function testAccompanyingPeriodPatch(int $personId, int $periodId)
{ {
$period = self::$container->get(AccompanyingPeriodRepository::class)
->find($periodId);
$initialValueEmergency = $period->isEmergency(); $initialValueEmergency = $period->isEmergency();
$em = self::$container->get(EntityManagerInterface::class); $em = self::$container->get(EntityManagerInterface::class);
$this->client->request( $this->client->request(
Request::METHOD_PATCH, Request::METHOD_PATCH,
sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId()), sprintf('/api/1.0/person/accompanying-course/%d.json', $periodId),
[], // parameters [], // parameters
[], // files [], // files
[], // server parameters [], // server parameters
@ -343,11 +348,10 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$period = $em->getRepository(AccompanyingPeriod::class) $period = $em->getRepository(AccompanyingPeriod::class)
->find($period->getId()); ->find($periodId);
$em->refresh($period);
$this->assertEquals(!$initialValueEmergency, $period->isEmergency()); $this->assertEquals(!$initialValueEmergency, $period->isEmergency());
// restore the initial valud // restore the initial value
$period->setEmergency($initialValueEmergency); $period->setEmergency($initialValueEmergency);
$em->flush(); $em->flush();
} }
@ -361,12 +365,32 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$em = static::$container->get(EntityManagerInterface::class); $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'));
$qb = $em->createQueryBuilder();
$personIds = $em->createQuery("SELECT p.id FROM ". $personIds = $qb
Person::class." p ". ->select('p.id')
" WHERE p.center = :center") ->distinct(true)
->from(Person::class, 'p')
->join('p.accompanyingPeriodParticipations', 'participation')
->join('participation.accompanyingPeriod', 'ap')
->where(
$qb->expr()->eq(
'p.center',
':center'
)
)
->andWhere(
$qb->expr()->gt(
'SIZE(p.accompanyingPeriodParticipations)',
0)
)
->andWhere(
$qb->expr()->eq('ap.step', ':step')
)
->setParameter('center', $center) ->setParameter('center', $center)
->setParameter('step', AccompanyingPeriod::STEP_CONFIRMED)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getQuery()
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
@ -400,11 +424,11 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
* *
* @dataProvider dataGenerateRandomAccompanyingCourse * @dataProvider dataGenerateRandomAccompanyingCourse
*/ */
public function testAccompanyingCourseAddParticipation(int $personId, AccompanyingPeriod $period) public function testAccompanyingCourseAddParticipation(int $personId, int $periodId)
{ {
$this->client->request( $this->client->request(
Request::METHOD_POST, Request::METHOD_POST,
sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $period->getId()), sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $periodId),
[], // parameters [], // parameters
[], // files [], // files
[], // server parameters [], // server parameters
@ -420,7 +444,8 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
// check by deownloading the accompanying cours // check by deownloading the accompanying cours
$this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId())); $this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $periodId));
$this->assertEquals(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)");
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$data = \json_decode($response->getContent()); $data = \json_decode($response->getContent());
@ -435,7 +460,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
// check removing the participation // check removing the participation
$this->client->request( $this->client->request(
Request::METHOD_DELETE, Request::METHOD_DELETE,
sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $period->getId()), sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $periodId),
[], // parameters [], // parameters
[], // files [], // files
[], // server parameters [], // server parameters
@ -450,42 +475,6 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$this->assertNotNull($data['startDate']); $this->assertNotNull($data['startDate']);
$this->assertArrayHasKey('endDate', $data); $this->assertArrayHasKey('endDate', $data);
$this->assertNotNull($data['endDate']); $this->assertNotNull($data['endDate']);
// set to variable for tear down
$this->personId = $personId;
$this->period = $period;
}
protected function tearDown()
{
$em = static::$container->get(EntityManagerInterface::class);
// remove participation if set
if ($this->personId && $this->period) {
$participation = $em
->getRepository(AccompanyingPeriodParticipation::class)
->findOneBy(['person' => $this->personId, 'accompanyingPeriod' => $this->period])
;
if (NULL !== $participation) {
$em->remove($participation);
$em->flush();
}
$this->personId = NULL;
$this->period = NULL;
} elseif ($this->period) {
$period = $em
->getRepository(AccompanyingPeriod::class)
->find($this->period->getId()) ;
if ($period !== NULL) {
$em->remove($period);
$em->flush();
}
$this->period = null;
}
} }
public function dataGenerateRandomAccompanyingCourseWithSocialIssue() public function dataGenerateRandomAccompanyingCourseWithSocialIssue()
@ -505,12 +494,32 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$em = static::$container->get(EntityManagerInterface::class); $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'));
$qb = $em->createQueryBuilder();
$personIds = $em->createQuery("SELECT p.id FROM ". $personIds = $qb
Person::class." p ". ->select('p.id')
" WHERE p.center = :center") ->distinct(true)
->from(Person::class, 'p')
->join('p.accompanyingPeriodParticipations', 'participation')
->join('participation.accompanyingPeriod', 'ap')
->where(
$qb->expr()->eq(
'p.center',
':center'
)
)
->andWhere(
$qb->expr()->gt(
'SIZE(p.accompanyingPeriodParticipations)',
0)
)
->andWhere(
$qb->expr()->eq('ap.step', ':step')
)
->setParameter('center', $center) ->setParameter('center', $center)
->setParameter('step', AccompanyingPeriod::STEP_CONFIRMED)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getQuery()
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
@ -556,7 +565,10 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$qb = $em->createQueryBuilder(); $qb = $em->createQueryBuilder();
$personIds = $qb $personIds = $qb
->select('p.id') ->select('p.id')
->distinct(true)
->from(Person::class, 'p') ->from(Person::class, 'p')
->join('p.accompanyingPeriodParticipations', 'participation')
->join('participation.accompanyingPeriod', 'ap')
->where( ->where(
$qb->expr()->eq( $qb->expr()->eq(
'p.center', 'p.center',
@ -568,7 +580,11 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
'SIZE(p.accompanyingPeriodParticipations)', 'SIZE(p.accompanyingPeriodParticipations)',
0) 0)
) )
->andWhere(
$qb->expr()->eq('ap.step', ':step')
)
->setParameter('center', $center) ->setParameter('center', $center)
->setParameter('step', AccompanyingPeriod::STEP_CONFIRMED)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getQuery() ->getQuery()
->getScalarResult(); ->getScalarResult();
@ -584,7 +600,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
->find($id); ->find($id);
$periods = $person->getAccompanyingPeriods(); $periods = $person->getAccompanyingPeriods();
yield [\array_pop($personIds)["id"], $periods[\array_rand($periods)] ]; yield [\array_pop($personIds)["id"], $periods[\array_rand($periods)]->getId() ];
$nbGenerated++; $nbGenerated++;
} }