mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add test for AccompanyingCourseController
This commit is contained in:
parent
66426f5102
commit
19fdf2a503
@ -38,5 +38,4 @@ class AccompanyingPeriodRepository extends ServiceEntityRepository
|
|||||||
{
|
{
|
||||||
parent::__construct($registry, AccompanyingPeriod::class);
|
parent::__construct($registry, AccompanyingPeriod::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,174 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||||
|
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
class AccompanyingCourseControllerTest extends WebTestCase
|
||||||
|
{
|
||||||
|
protected static EntityManagerInterface $em;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup before the first test of this class (see phpunit doc)
|
||||||
|
*/
|
||||||
|
public static function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup before each test method (see phpunit doc)
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->client = static::createClient(array(), array(
|
||||||
|
'PHP_AUTH_USER' => 'center a_social',
|
||||||
|
'PHP_AUTH_PW' => 'password',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @dataProvider dataGenerateRandomAccompanyingCourse
|
||||||
|
*/
|
||||||
|
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()));
|
||||||
|
$response = $this->client->getResponse();
|
||||||
|
|
||||||
|
$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()
|
||||||
|
{
|
||||||
|
// 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." p ".
|
||||||
|
" WHERE p.center = :center")
|
||||||
|
->setParameter('center', $center)
|
||||||
|
->setMaxResults($maxResults)
|
||||||
|
->getScalarResult();
|
||||||
|
|
||||||
|
// create a random order
|
||||||
|
shuffle($personIds);
|
||||||
|
|
||||||
|
$nbGenerated = 0;
|
||||||
|
while ($nbGenerated < $maxGenerated) {
|
||||||
|
$id = \array_pop($personIds)["id"];
|
||||||
|
|
||||||
|
$person = $em->getRepository(Person::class)
|
||||||
|
->find($id);
|
||||||
|
$periods = $person->getAccompanyingPeriods();
|
||||||
|
|
||||||
|
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