bootstrap api and apply on accompanying period

This commit is contained in:
2021-05-06 00:14:36 +02:00
parent f02e33fda7
commit 07e0692783
16 changed files with 235 additions and 257 deletions

View File

@@ -36,7 +36,7 @@ use Symfony\Component\HttpFoundation\Request;
/**
* Test api for AccompanyingCourseControllerTest
*/
class AccompanyingCourseControllerTest extends WebTestCase
class AccompanyingCourseApiControllerTest extends WebTestCase
{
protected static EntityManagerInterface $em;
@@ -65,7 +65,7 @@ class AccompanyingCourseControllerTest extends WebTestCase
*/
public function testAccompanyingCourseShow(int $personId, AccompanyingPeriod $period)
{
$this->client->request(Request::METHOD_GET, sprintf('/fr/person/api/1.0/accompanying-course/%d/show.json', $period->getId()));
$c = $this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId()));
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)");
@@ -77,6 +77,14 @@ class AccompanyingCourseControllerTest extends WebTestCase
$this->assertGreaterThan(0, $data->participations);
}
public function testShow404()
{
$this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', 99999));
$response = $this->client->getResponse();
$this->assertEquals(404, $response->getStatusCode(), "Test that the response of rest api has a status code 'not found' (404)");
}
/**
*
* @dataProvider dataGenerateRandomAccompanyingCourse
@@ -85,26 +93,55 @@ class AccompanyingCourseControllerTest extends WebTestCase
{
$this->client->request(
Request::METHOD_POST,
sprintf('/fr/person/api/1.0/accompanying-course/%d/participation.json', $period->getId()),
sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $period->getId()),
[], // parameters
[], // files
[], // server parameters
\json_encode([ 'id' => $personId ])
);
$response = $this->client->getResponse();
$data = \json_decode($response->getContent(), true);
$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()));
$this->assertArrayHasKey('id', $data);
$this->assertArrayHasKey('startDate', $data);
$this->assertNotNull($data['startDate']);
// check by deownloading the accompanying cours
$this->client->request(Request::METHOD_GET, sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId()));
$response = $this->client->getResponse();
$data = \json_decode($response->getContent());
// check that the person id is contained
$participationsPersonsIds = \array_map(
function($participation) { return $participation->person->id; },
$data->participations);
$this->assertContains($personId, $participationsPersonsIds);
// check removing the participation
$this->client->request(
Request::METHOD_DELETE,
sprintf('/api/1.0/person/accompanying-course/%d/participation.json', $period->getId()),
[], // parameters
[], // files
[], // server parameters
\json_encode([ 'id' => $personId ])
);
$response = $this->client->getResponse();
$data = \json_decode($response->getContent(), true);
$this->assertEquals(200, $response->getStatusCode(), "Test that the response of rest api has a status code ok (200)");
$this->assertArrayHasKey('id', $data);
$this->assertArrayHasKey('startDate', $data);
$this->assertNotNull($data['startDate']);
$this->assertArrayHasKey('endDate', $data);
$this->assertNotNull($data['endDate']);
// set to variable for tear down
$this->personId = $personId;
$this->period = $period;
}
@@ -112,6 +149,7 @@ class AccompanyingCourseControllerTest extends WebTestCase
protected function tearDown()
{
// remove participation created during test 'testAccompanyingCourseAddParticipation'
// and if the test could not remove it
$testAddParticipationName = 'testAccompanyingCourseAddParticipation';
@@ -126,8 +164,10 @@ class AccompanyingCourseControllerTest extends WebTestCase
->findOneBy(['person' => $this->personId, 'accompanyingPeriod' => $this->period])
;
$em->remove($participation);
$em->flush();
if (NULL !== $participation) {
$em->remove($participation);
$em->flush();
}
}
public function dataGenerateRandomAccompanyingCourse()
@@ -139,9 +179,9 @@ class AccompanyingCourseControllerTest extends WebTestCase
// * 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;
// Ensure to keep always $maxGenerated to the double of $maxResults. x8 is a good compromize :)
$maxGenerated = 3;
$maxResults = $maxGenerated * 8;
static::bootKernel();
$em = static::$container->get(EntityManagerInterface::class);

View File

@@ -27,7 +27,6 @@ use Chill\PersonBundle\Entity\Person;
class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
{
public function testClosingIsAfterOpeningConsistency()
{
$datetime1 = new \DateTime('now');
@@ -77,22 +76,24 @@ class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
$this->assertFalse($period->isOpen());
}
public function testCanBeReOpened()
public function testPersonPeriod()
{
$person = new Person(\DateTime::createFromFormat('Y-m-d', '2010-01-01'));
$person->close($person->getAccompanyingPeriods()[0]
->setClosingDate(\DateTime::createFromFormat('Y-m-d', '2010-12-31')));
$firstAccompanygingPeriod = $person->getAccompanyingPeriodsOrdered()[0];
$this->assertTrue($firstAccompanygingPeriod->canBeReOpened());
$lastAccompanyingPeriod = (new AccompanyingPeriod(\DateTime::createFromFormat('Y-m-d', '2011-01-01')))
->setClosingDate(\DateTime::createFromFormat('Y-m-d', '2011-12-31'))
;
$person->addAccompanyingPeriod($lastAccompanyingPeriod);
$this->assertFalse($firstAccompanygingPeriod->canBeReOpened());
}
$person = new Person();
$period = new AccompanyingPeriod(new \DateTime());
$period->addPerson($person);
$this->assertEquals(1, $period->getParticipations()->count());
$this->assertTrue($period->containsPerson($person));
$participation = $period->getOpenParticipationContainsPerson($person);
$participations = $period->getParticipationsContainsPerson($person);
$this->assertNotNull($participation);
$this->assertEquals(1, $participations->count());
$period->removePerson($person);
$participation = $period->getOpenParticipationContainsPerson($person);
$this->assertNull($participation);
}
}