mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
Merge remote-tracking branch 'origin/master' into fix-person-tests
This commit is contained in:
@@ -27,11 +27,14 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
|
||||
|
||||
/**
|
||||
* Test api for AccompanyingCourseControllerTest
|
||||
@@ -40,6 +43,10 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
{
|
||||
protected static EntityManagerInterface $em;
|
||||
|
||||
protected ?int $personId = NULL;
|
||||
|
||||
protected ?AccompanyingPeriod $period = NULL;
|
||||
|
||||
/**
|
||||
* Setup before the first test of this class (see phpunit doc)
|
||||
*/
|
||||
@@ -85,6 +92,310 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
$this->assertEquals(404, $response->getStatusCode(), "Test that the response of rest api has a status code 'not found' (404)");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider dataGenerateRandomAccompanyingCourseWithSocialIssue
|
||||
*/
|
||||
public function testAccompanyingCourseAddRemoveSocialIssue(AccompanyingPeriod $period, SocialIssue $si)
|
||||
{
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/socialissue.json', $period->getId()),
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
\json_encode([ 'type' => 'social_issue', 'id' => $si->getId() ])
|
||||
);
|
||||
|
||||
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
||||
|
||||
$data = \json_decode($this->client->getResponse()->getContent(), true);
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertArrayHasKey('type', $data);
|
||||
$this->assertEquals('social_issue', $data['type']);
|
||||
|
||||
|
||||
$this->client->request(
|
||||
Request::METHOD_DELETE,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/socialissue.json', $period->getId()),
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
\json_encode([ 'type' => 'social_issue', 'id' => $si->getId() ])
|
||||
);
|
||||
|
||||
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateRandomRequestorValidData
|
||||
*/
|
||||
public function testCommentWithValidData(AccompanyingPeriod $period, $personId, $thirdPartyId)
|
||||
{
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/comment.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'accompanying_period_comment', 'content' => "this is a text"])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
|
||||
$this->client->request(
|
||||
Request::METHOD_DELETE,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/comment.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'accompanying_period_comment', 'id' => $data['id']])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateRandomRequestorValidData
|
||||
*/
|
||||
public function testResourceWithValidData(AccompanyingPeriod $period, $personId, $thirdPartyId)
|
||||
{
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
// post a person
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/resource.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'accompanying_period_resource', 'resource' => [ 'type' => 'person', 'id' => $personId ]])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertEquals($personId, $data['resource']['id']);
|
||||
|
||||
// check into database
|
||||
$resource = $em->getRepository(Resource::class)
|
||||
->find($data['id']);
|
||||
$this->assertInstanceOf(Resource::class, $resource);
|
||||
$this->assertInstanceOf(Person::class, $resource->getResource());
|
||||
$this->assertEquals($personId, $resource->getResource()->getId());
|
||||
|
||||
// remove the resource
|
||||
$this->client->request(
|
||||
Request::METHOD_DELETE,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()),
|
||||
[],
|
||||
[],
|
||||
[], //server
|
||||
\json_encode([ 'type' => 'accompanying_period_resource', 'id' => $resource->getId()])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
// post a third party
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/resource.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'accompanying_period_resource', 'resource' => [ 'type' => 'thirdparty', 'id' => $thirdPartyId ]])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertEquals($thirdPartyId, $data['resource']['id']);
|
||||
|
||||
// check into database
|
||||
$resource = $em->getRepository(Resource::class)
|
||||
->find($data['id']);
|
||||
$this->assertInstanceOf(Resource::class, $resource);
|
||||
$this->assertInstanceOf(ThirdParty::class, $resource->getResource());
|
||||
$this->assertEquals($thirdPartyId, $resource->getResource()->getId());
|
||||
|
||||
// remove the resource
|
||||
$this->client->request(
|
||||
Request::METHOD_DELETE,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()),
|
||||
[],
|
||||
[],
|
||||
[], //server
|
||||
\json_encode([ 'type' => 'accompanying_period_resource', 'id' => $resource->getId()])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateRandomRequestorValidData
|
||||
*/
|
||||
public function testRequestorWithValidData(AccompanyingPeriod $period, $personId, $thirdPartyId)
|
||||
{
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
// post a person
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'person', 'id' => $personId ])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertEquals($personId, $data['id']);
|
||||
|
||||
// check into database
|
||||
$period = $em->getRepository(AccompanyingPeriod::class)
|
||||
->find($period->getId());
|
||||
$this->assertInstanceOf(Person::class, $period->getRequestor());
|
||||
$this->assertEquals($personId, $period->getRequestor()->getId());
|
||||
|
||||
// post a third party
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'thirdparty', 'id' => $thirdPartyId ])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertEquals($thirdPartyId, $data['id']);
|
||||
|
||||
// check into database
|
||||
$period = $em->getRepository(AccompanyingPeriod::class)
|
||||
->find($period->getId());
|
||||
$this->assertInstanceOf(ThirdParty::class, $period->getRequestor());
|
||||
$this->assertEquals($thirdPartyId, $period->getRequestor()->getId());
|
||||
|
||||
// remove the requestor
|
||||
$this->client->request(
|
||||
Request::METHOD_DELETE,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/requestor.json', $period->getId())
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
||||
// check into database
|
||||
$period = $em->getRepository(AccompanyingPeriod::class)
|
||||
->find($period->getId());
|
||||
$em->refresh($period);
|
||||
$this->assertNull($period->getRequestor());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateNewAccompanyingCourse
|
||||
*/
|
||||
public function testConfirm(AccompanyingPeriod $period)
|
||||
{
|
||||
$this->client->request(
|
||||
Request::METHOD_POST,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d/confirm.json', $period->getId())
|
||||
);
|
||||
|
||||
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
||||
|
||||
// add period to remove it in tear down
|
||||
$this->period = $period;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider dataGenerateRandomAccompanyingCourse
|
||||
*/
|
||||
public function testAccompanyingPeriodPatch(int $personId, AccompanyingPeriod $period)
|
||||
{
|
||||
$initialValueEmergency = $period->isEmergency();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$this->client->request(
|
||||
Request::METHOD_PATCH,
|
||||
sprintf('/api/1.0/person/accompanying-course/%d.json', $period->getId()),
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'type' => 'accompanying_period', 'emergency' => !$initialValueEmergency ])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$period = $em->getRepository(AccompanyingPeriod::class)
|
||||
->find($period->getId());
|
||||
$em->refresh($period);
|
||||
$this->assertEquals(!$initialValueEmergency, $period->isEmergency());
|
||||
|
||||
// restore the initial valud
|
||||
$period->setEmergency($initialValueEmergency);
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
public function dataGenerateRandomRequestorValidData(): \Iterator
|
||||
{
|
||||
$dataLength = 2;
|
||||
$maxResults = 100;
|
||||
|
||||
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);
|
||||
|
||||
$thirdPartyIds = $em->createQuery("SELECT t.id FROM ".
|
||||
ThirdParty::class." t ")
|
||||
->setMaxResults($maxResults)
|
||||
->getScalarResult();
|
||||
|
||||
// create a random order
|
||||
shuffle($thirdPartyIds);
|
||||
|
||||
$i = 0;
|
||||
while ($i <= $dataLength) {
|
||||
$person = $em->getRepository(Person::class)
|
||||
->find(\array_pop($personIds)['id']);
|
||||
|
||||
if (count($person->getAccompanyingPeriods()) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$period = $person->getAccompanyingPeriods()[0];
|
||||
|
||||
yield [$period, \array_pop($personIds)['id'], \array_pop($thirdPartyIds)['id'] ];
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider dataGenerateRandomAccompanyingCourse
|
||||
@@ -97,7 +408,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'id' => $personId ])
|
||||
\json_encode([ 'type' => 'person', 'id' => $personId ])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
@@ -128,7 +439,7 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server parameters
|
||||
\json_encode([ 'id' => $personId ])
|
||||
\json_encode([ 'type' => 'person', 'id' => $personId ])
|
||||
);
|
||||
$response = $this->client->getResponse();
|
||||
$data = \json_decode($response->getContent(), true);
|
||||
@@ -148,25 +459,79 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
// remove participation created during test 'testAccompanyingCourseAddParticipation'
|
||||
// and if the test could not remove it
|
||||
|
||||
$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])
|
||||
;
|
||||
// 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();
|
||||
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()
|
||||
{
|
||||
// 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. x8 is a good compromize :)
|
||||
$maxGenerated = 3;
|
||||
$maxResults = $maxGenerated * 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);
|
||||
|
||||
$socialIssues = $em->createQuery("SELECT s FROM ".
|
||||
SocialIssue::class." s ")
|
||||
->setMaxResults(10)
|
||||
->getResult();
|
||||
|
||||
$nbGenerated = 0;
|
||||
while ($nbGenerated < $maxGenerated) {
|
||||
$id = \array_pop($personIds)["id"];
|
||||
|
||||
$person = $em->getRepository(Person::class)
|
||||
->find($id);
|
||||
$periods = $person->getAccompanyingPeriods();
|
||||
|
||||
yield [$periods[\array_rand($periods)], $socialIssues[\array_rand($socialIssues)] ];
|
||||
|
||||
$nbGenerated++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,4 +576,39 @@ class AccompanyingCourseApiControllerTest extends WebTestCase
|
||||
$nbGenerated++;
|
||||
}
|
||||
}
|
||||
|
||||
public function dataGenerateNewAccompanyingCourse()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$period = new AccompanyingPeriod(new \DateTime('1 week ago'));
|
||||
$user = $em->getRepository(User::class)
|
||||
->findOneByUsernameCanonical('center a_social');
|
||||
$period->setCreatedBy($user);
|
||||
//$period->setCreatedAt(new \DateTime('yesterday'));
|
||||
|
||||
$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(100)
|
||||
->getScalarResult();
|
||||
|
||||
// create a random order
|
||||
shuffle($personIds);
|
||||
|
||||
for ($i = 0; $i<2; $i++) {
|
||||
$person = $em->getRepository(Person::class)->find(\array_pop($personIds));
|
||||
$period->addPerson($person);
|
||||
}
|
||||
|
||||
$em->persist($period);
|
||||
$em->flush();
|
||||
|
||||
yield [ $period ];
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class AccompanyingCourseControllerTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
self::bootKernel();
|
||||
$this->client = $this->getClientAuthenticated();
|
||||
}
|
||||
|
||||
public function testNewWithoutUsers()
|
||||
{
|
||||
$this->client->request('GET', '/fr/person/parcours/new');
|
||||
|
||||
$this->assertResponseRedirects();
|
||||
$location = $this->client->getResponse()->headers->get('Location');
|
||||
|
||||
$this->assertEquals(1, \preg_match("|^\/[^\/]+\/parcours/([\d]+)/show$|", $location));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateRandomUsers
|
||||
*/
|
||||
public function testWithNewUsers($personId0, $personId1)
|
||||
{
|
||||
$this->client->request('GET', '/fr/person/parcours/new', [
|
||||
'person_id' => [
|
||||
$personId0,
|
||||
$personId1
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertResponseRedirects();
|
||||
$location = $this->client->getResponse()->headers->get('Location');
|
||||
$matches = [];
|
||||
|
||||
$this->assertEquals(1, \preg_match("|^\/[^\/]+\/parcours/([\d]+)/show$|", $location, $matches));
|
||||
$id = $matches[1];
|
||||
|
||||
$period = self::$container->get(EntityManagerInterface::class)
|
||||
->getRepository(AccompanyingPeriod::class)
|
||||
->find($id);
|
||||
|
||||
$this->assertNotNull($period);
|
||||
|
||||
$this->assertEquals(2, count($period->getParticipations()));
|
||||
}
|
||||
|
||||
public function dataGenerateRandomUsers(): \Iterator
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$period = new AccompanyingPeriod(new \DateTime('1 week ago'));
|
||||
$user = $em->getRepository(User::class)
|
||||
->findOneByUsernameCanonical('center a_social');
|
||||
$period->setCreatedBy($user);
|
||||
//$period->setCreatedAt(new \DateTime('yesterday'));
|
||||
|
||||
$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(100)
|
||||
->getScalarResult();
|
||||
|
||||
yield [ \array_pop($personIds), \array_pop($personIds) ];
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class PersonApiControllerTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
/**
|
||||
* @dataProvider dataGetPersonFromCenterB
|
||||
*/
|
||||
public function testPersonGetUnauthorized($personId): void
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$client->request(Request::METHOD_GET, "/api/1.0/person/person/{$personId}.json");
|
||||
$response = $client->getResponse();
|
||||
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGetPersonFromCenterA
|
||||
*/
|
||||
public function testPersonGet($personId): void
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$client->request(Request::METHOD_GET, "/api/1.0/person/person/{$personId}.json");
|
||||
$response = $client->getResponse();
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertArrayHasKey('type', $data);
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertEquals('person', $data['type']);
|
||||
$this->assertEquals($personId, $data['id']);
|
||||
}
|
||||
|
||||
public function dataGetPersonFromCenterA(): \Iterator
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$personIds= $em->createQuery("SELECT p.id FROM ".Person::class." p ".
|
||||
"JOIN p.center c ".
|
||||
"WHERE c.name = :center")
|
||||
->setParameter('center', 'Center A')
|
||||
->setMaxResults(100)
|
||||
->getScalarResult()
|
||||
;
|
||||
|
||||
\shuffle($personIds);
|
||||
|
||||
yield \array_pop($personIds);
|
||||
yield \array_pop($personIds);
|
||||
}
|
||||
|
||||
public function dataGetPersonFromCenterB(): \Iterator
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$personIds= $em->createQuery("SELECT p.id FROM ".Person::class." p ".
|
||||
"JOIN p.center c ".
|
||||
"WHERE c.name = :center")
|
||||
->setParameter('center', 'Center B')
|
||||
->setMaxResults(100)
|
||||
->getScalarResult()
|
||||
;
|
||||
|
||||
\shuffle($personIds);
|
||||
|
||||
yield \array_pop($personIds);
|
||||
yield \array_pop($personIds);
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
|
||||
class SocialIssueApiControllerTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testList(): array
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
$client->request(Request::METHOD_GET, '/api/1.0/person/social-work/social-issue.json');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertGreaterThan(0, $data['count']);
|
||||
$this->assertGreaterThan(0, count($data['results']));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testList
|
||||
*/
|
||||
public function testItem(array $data): void
|
||||
{
|
||||
$socialIssues = $data['results'];
|
||||
shuffle($socialIssues);
|
||||
$socialIssue = \array_pop($socialIssues);
|
||||
|
||||
|
||||
$client = $this->getClientAuthenticated();
|
||||
$client->request(Request::METHOD_GET, sprintf('/api/1.0/person/social-work/social-issue/%d.json', $socialIssue['id']));
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertArrayHasKey('id', $data);
|
||||
$this->assertArrayHasKey('type', $data);
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Entity\AccompanyingPeriod;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
|
||||
class ResourceTest extends TestCase
|
||||
{
|
||||
public function testSetResource()
|
||||
{
|
||||
$person = new Person();
|
||||
$thirdParty = new ThirdParty();
|
||||
|
||||
$resource = new Resource();
|
||||
|
||||
$resource->setResource($person);
|
||||
|
||||
$this->assertSame($person, $resource->getResource());
|
||||
$this->assertNull($resource->getThirdParty());
|
||||
|
||||
$resource->setResource($thirdParty);
|
||||
|
||||
$this->assertSame($thirdParty, $resource->getResource());
|
||||
$this->assertNull($resource->getPerson());
|
||||
|
||||
// we repeat adding a person, to ensure that third party is
|
||||
// well reset
|
||||
$resource->setResource($person);
|
||||
$this->assertSame($person, $resource->getResource());
|
||||
$this->assertNull($resource->getThirdParty());
|
||||
|
||||
$resource->setResource(null);
|
||||
$this->assertNull($resource->getThirdParty());
|
||||
$this->assertNull($resource->getPerson());
|
||||
$this->assertNull($resource->getResource());
|
||||
}
|
||||
}
|
@@ -23,7 +23,10 @@
|
||||
namespace Chill\PersonBundle\Tests\Entity;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
|
||||
|
||||
class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@@ -83,10 +86,11 @@ class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
|
||||
$person3 = new Person();
|
||||
$period = new AccompanyingPeriod(new \DateTime());
|
||||
|
||||
$period->addPerson($person);
|
||||
$period->addPerson($person2);
|
||||
$period->addPerson($person3);
|
||||
$participation0 = $period->createParticipationFor($person);
|
||||
$period->createParticipationFor($person2);
|
||||
$period->createParticipationFor($person3);
|
||||
|
||||
$this->assertNotNull($participation0);
|
||||
$this->assertEquals(3, $period->getParticipations()->count());
|
||||
$this->assertTrue($period->containsPerson($person));
|
||||
$this->assertFalse($period->containsPerson(new Person()));
|
||||
@@ -95,13 +99,79 @@ class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
|
||||
$participations = $period->getParticipationsContainsPerson($person);
|
||||
$this->assertNotNull($participation);
|
||||
$this->assertSame($person, $participation->getPerson());
|
||||
$this->assertSame($participation, $participation0);
|
||||
$this->assertEquals(1, $participations->count());
|
||||
|
||||
$participationL = $period->removePerson($person);
|
||||
$participationL = $period->closeParticipationFor($person);
|
||||
$this->assertSame($participationL, $participation);
|
||||
$this->assertTrue($participation->getEndDate() instanceof \DateTimeInterface);
|
||||
|
||||
$participation = $period->getOpenParticipationContainsPerson($person);
|
||||
$this->assertNull($participation);
|
||||
|
||||
$person4 = new Person();
|
||||
$participations4 = $period->getParticipationsContainsPerson($person4);
|
||||
$this->assertEquals(0, $participations4->count());
|
||||
$participation4 = $period->getOpenParticipationContainsPerson($person4);
|
||||
$this->assertNull($participation4);
|
||||
|
||||
$period->addPerson($person4);
|
||||
$this->assertInstanceOf(AccompanyingPeriodParticipation::class, $period->getOpenParticipationContainsPerson($person4));
|
||||
$this->assertEquals(1, $period->getParticipationsContainsPerson($person4)->count());
|
||||
$period->removePerson($person4);
|
||||
$this->assertNull($period->getOpenParticipationContainsPerson($person4));
|
||||
$this->assertEquals(1, $period->getParticipationsContainsPerson($person4)->count());
|
||||
}
|
||||
|
||||
public function testRequestor()
|
||||
{
|
||||
$period = new AccompanyingPeriod(new \DateTime());
|
||||
$person = new Person();
|
||||
$thirdParty = new ThirdParty();
|
||||
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertNull($period->getRequestor());
|
||||
|
||||
$period->setRequestor($person);
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertSame($person, $period->getRequestorPerson());
|
||||
$this->assertSame($person, $period->getRequestor());
|
||||
|
||||
$period->setRequestor($thirdParty);
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertSame($thirdParty, $period->getRequestorThirdParty());
|
||||
$this->assertSame($thirdParty, $period->getRequestor());
|
||||
|
||||
$period->setRequestor(NULL);
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertNull($period->getRequestor());
|
||||
}
|
||||
|
||||
public function testInitialComment()
|
||||
{
|
||||
$period = new AccompanyingPeriod(new \DateTime());
|
||||
$comment = new Comment();
|
||||
$replacingComment = new Comment();
|
||||
|
||||
$period->setInitialComment(NULL);
|
||||
$this->assertNull($period->getInitialComment());
|
||||
|
||||
$period->setInitialComment($comment);
|
||||
$this->assertSame($period->getInitialComment(), $comment);
|
||||
$this->assertSame($period, $comment->getAccompanyingPeriod());
|
||||
$this->assertEquals(0, count($period->getComments()), "The initial comment should not appears in the list of comments");
|
||||
|
||||
$period->setInitialComment($replacingComment);
|
||||
$this->assertSame($period->getInitialComment(), $replacingComment);
|
||||
$this->assertSame($period, $replacingComment->getAccompanyingPeriod());
|
||||
$this->assertEquals(0, count($period->getComments()), "The initial comment should not appears in the list of comments");
|
||||
$this->assertNull($comment->getAccompanyingPeriod());
|
||||
|
||||
$period->setInitialComment(NULL);
|
||||
$this->assertNull($period->getInitialComment());
|
||||
$this->assertNull($replacingComment->getAccompanyingPeriod());
|
||||
$this->assertEquals(0, count($period->getComments()), "The initial comment should not appears in the list of comments");
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Workflows;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
|
||||
class AccompanyingPeriodLifecycle extends KernelTestCase
|
||||
{
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testConfirm()
|
||||
{
|
||||
$registry = self::$container->get(Registry::class);
|
||||
$period = new AccompanyingPeriod(new \DateTime('now'));
|
||||
$workflow = $registry->get($period);
|
||||
|
||||
$this->assertArrayHasKey('DRAFT', $workflow->getMarking($period)->getPlaces());
|
||||
$this->assertTrue($workflow->can($period, 'confirm'));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user