fix data provider which randomly generate invalid data in test

This commit is contained in:
Julien Fastré 2021-06-18 09:19:28 +00:00
parent 970635dfad
commit a7ecd08d78

View File

@ -2,8 +2,8 @@
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* *
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop> * <http://www.champs-libres.coop>, <info@champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -54,7 +54,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
{ {
static::bootKernel(); static::bootKernel();
} }
/** /**
* Setup before each test method (see phpunit doc) * Setup before each test method (see phpunit doc)
*/ */
@ -76,11 +76,11 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$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, $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); $this->assertGreaterThan(0, $data->participations);
} }
@ -106,7 +106,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
[], [],
\json_encode([ 'type' => 'social_issue', 'id' => $si->getId() ]) \json_encode([ 'type' => 'social_issue', 'id' => $si->getId() ])
); );
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$data = \json_decode($this->client->getResponse()->getContent(), true); $data = \json_decode($this->client->getResponse()->getContent(), true);
@ -204,7 +204,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
// post a third party // post a third party
$this->client->request( $this->client->request(
Request::METHOD_POST, Request::METHOD_POST,
sprintf('/api/1.0/person/accompanying-course/%d/resource.json', $period->getId()), sprintf('/api/1.0/person/accompanying-course/%d/resource.json', $period->getId()),
@ -269,7 +269,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$this->assertInstanceOf(Person::class, $period->getRequestor()); $this->assertInstanceOf(Person::class, $period->getRequestor());
$this->assertEquals($personId, $period->getRequestor()->getId()); $this->assertEquals($personId, $period->getRequestor()->getId());
// post a third party // post a third party
$this->client->request( $this->client->request(
Request::METHOD_POST, Request::METHOD_POST,
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()), sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()),
@ -332,7 +332,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$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', $period->getId()),
[], // parameters [], // parameters
[], // files [], // files
@ -368,7 +368,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
->setParameter('center', $center) ->setParameter('center', $center)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
shuffle($personIds); shuffle($personIds);
@ -376,7 +376,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
ThirdParty::class." t ") ThirdParty::class." t ")
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
shuffle($thirdPartyIds); shuffle($thirdPartyIds);
@ -421,7 +421,7 @@ 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', $period->getId()));
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$data = \json_decode($response->getContent()); $data = \json_decode($response->getContent());
@ -491,9 +491,9 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
public function dataGenerateRandomAccompanyingCourseWithSocialIssue() public function dataGenerateRandomAccompanyingCourseWithSocialIssue()
{ {
// note about max result for person query, and maxGenerated: // note about max result for person query, and maxGenerated:
// //
// in the final loop, an id is popped out of the personIds array twice: // 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 person, which will in turn provide his accompanying period;
// * one for getting the personId to populate to the data manager // * one for getting the personId to populate to the data manager
// //
@ -512,7 +512,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
->setParameter('center', $center) ->setParameter('center', $center)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
shuffle($personIds); shuffle($personIds);
@ -538,9 +538,9 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
public function dataGenerateRandomAccompanyingCourse() public function dataGenerateRandomAccompanyingCourse()
{ {
// note about max result for person query, and maxGenerated: // note about max result for person query, and maxGenerated:
// //
// in the final loop, an id is popped out of the personIds array twice: // 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 person, which will in turn provide his accompanying period;
// * one for getting the personId to populate to the data manager // * one for getting the personId to populate to the data manager
// //
@ -553,13 +553,26 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
$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 ". $qb = $em->createQueryBuilder();
Person::class." p ". $personIds = $qb
" WHERE p.center = :center") ->select('p.id')
->from(Person::class, 'p')
->where(
$qb->expr()->eq(
'p.center',
':center'
)
)
->andWhere(
$qb->expr()->gt(
'SIZE(p.accompanyingPeriodParticipations)',
0)
)
->setParameter('center', $center) ->setParameter('center', $center)
->setMaxResults($maxResults) ->setMaxResults($maxResults)
->getQuery()
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
shuffle($personIds); shuffle($personIds);
@ -597,7 +610,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
->setParameter('center', $center) ->setParameter('center', $center)
->setMaxResults(100) ->setMaxResults(100)
->getScalarResult(); ->getScalarResult();
// create a random order // create a random order
shuffle($personIds); shuffle($personIds);