mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-27 00:55:01 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,184 +1,114 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Bundle\ChillPersonBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Household\Position;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Iterator;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use function array_pop;
|
||||
use function json_decode;
|
||||
use function json_encode;
|
||||
use function random_int;
|
||||
use function shuffle;
|
||||
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class HouseholdMemberControllerTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*/
|
||||
public function testMoveMember($personId, $householdId, $positionId, \DateTimeInterface $date)
|
||||
public function provideValidDataEditMember(): Iterator
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
\json_encode(
|
||||
[
|
||||
'concerned' =>
|
||||
[
|
||||
[
|
||||
'person' =>
|
||||
[
|
||||
'type' => 'person',
|
||||
'id' => $personId
|
||||
],
|
||||
'start_date' =>
|
||||
[
|
||||
'datetime' => $date->format(\DateTimeInterface::RFC3339)
|
||||
],
|
||||
'position' =>
|
||||
[
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => "Introduced by automated test",
|
||||
],
|
||||
],
|
||||
'destination' =>
|
||||
[
|
||||
'type' => 'household',
|
||||
'id' => $householdId
|
||||
]
|
||||
],
|
||||
true)
|
||||
);
|
||||
$membershipIds = $em->createQuery('SELECT m.id FROM ' . HouseholdMember::class . ' m ' .
|
||||
'JOIN m.person p ' .
|
||||
'JOIN p.center c ' .
|
||||
'WHERE c.name = :center AND m.endDate IS NULL')
|
||||
->setParameter('center', 'Center A')
|
||||
->getScalarResult();
|
||||
|
||||
$this->assertEquals(Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
shuffle($membershipIds);
|
||||
|
||||
yield [array_pop($membershipIds)['id']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*/
|
||||
public function testMoveMemberToNewHousehold($personId, $householdId, $positionId, \DateTimeInterface $date)
|
||||
public function provideValidDataMove(): Iterator
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$yesterday = new DateTimeImmutable('yesterday');
|
||||
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
\json_encode(
|
||||
[
|
||||
'concerned' =>
|
||||
[
|
||||
[
|
||||
'person' =>
|
||||
[
|
||||
'type' => 'person',
|
||||
'id' => $personId
|
||||
],
|
||||
'start_date' =>
|
||||
[
|
||||
'datetime' => $date->format(\DateTimeInterface::RFC3339)
|
||||
],
|
||||
'position' =>
|
||||
[
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => "Introduced by automated test",
|
||||
],
|
||||
],
|
||||
'destination' =>
|
||||
[
|
||||
'type' => 'household',
|
||||
]
|
||||
],
|
||||
true)
|
||||
);
|
||||
$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();
|
||||
|
||||
$this->assertEquals(Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
shuffle($personIds);
|
||||
|
||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
||||
$household = new Household();
|
||||
$em->persist($household);
|
||||
$em->flush();
|
||||
|
||||
$this->assertIsArray($data);
|
||||
$this->assertArrayHasKey('members', $data);
|
||||
$this->assertIsArray($data['members']);
|
||||
$this->assertEquals(1, count($data['members']),
|
||||
"assert new household count one member");
|
||||
$this->assertArrayHasKey('person', $data['members'][0]);
|
||||
$this->assertArrayHasKey('id', $data['members'][0]['person']);
|
||||
$this->assertEquals($personId, $data['members'][0]['person']['id']);
|
||||
}
|
||||
$positions = $em->createQuery('SELECT pos.id FROM ' . Position::class . ' pos ' .
|
||||
'WHERE pos.shareHouseHold = TRUE')
|
||||
->getResult();
|
||||
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*/
|
||||
public function testLeaveWithoutHousehold($personId, $householdId, $positionId, \DateTimeInterface $date)
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
$i = 0;
|
||||
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
\json_encode(
|
||||
[
|
||||
'concerned' =>
|
||||
[
|
||||
[
|
||||
'person' =>
|
||||
[
|
||||
'type' => 'person',
|
||||
'id' => $personId
|
||||
],
|
||||
'start_date' =>
|
||||
[
|
||||
'datetime' => $date->format(\DateTimeInterface::RFC3339)
|
||||
],
|
||||
'position' =>
|
||||
[
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => "Introduced by automated test",
|
||||
],
|
||||
],
|
||||
'destination' => null
|
||||
],
|
||||
true)
|
||||
);
|
||||
do {
|
||||
$id = array_pop($personIds)['id'];
|
||||
$person = self::$container->get(EntityManagerInterface::class)
|
||||
->getRepository(Person::class)
|
||||
->find($id);
|
||||
|
||||
$this->assertEquals(Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
$participation = $person->getCurrentHouseholdParticipationShareHousehold();
|
||||
|
||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
||||
if (null == $participation
|
||||
|| (
|
||||
null === $participation->getEndDate()
|
||||
&& $participation->getStartDate() <= $yesterday
|
||||
)) {
|
||||
++$i;
|
||||
|
||||
$this->assertEquals(null, $data);
|
||||
yield [
|
||||
$id,
|
||||
$household->getId(),
|
||||
$positions[random_int(0, count($positions) - 1)]['id'],
|
||||
new DateTimeImmutable('tomorrow'),
|
||||
];
|
||||
}
|
||||
} while (1 >= $i);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideValidDataEditMember
|
||||
*
|
||||
* @param mixed $memberId
|
||||
*/
|
||||
public function testEditMember($memberId)
|
||||
{
|
||||
@@ -199,75 +129,170 @@ class HouseholdMemberControllerTest extends WebTestCase
|
||||
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||
}
|
||||
|
||||
public function provideValidDataMove(): \Iterator
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*
|
||||
* @param mixed $personId
|
||||
* @param mixed $householdId
|
||||
* @param mixed $positionId
|
||||
*/
|
||||
public function testLeaveWithoutHousehold($personId, $householdId, $positionId, DateTimeInterface $date)
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$yesterday = new \DateTimeImmutable('yesterday');
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$personIds = $em->createQuery("SELECT p.id FROM ".Person::class." p ".
|
||||
"JOIN p.center c ".
|
||||
"WHERE ".
|
||||
"c.name = :center "
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
json_encode(
|
||||
[
|
||||
'concerned' => [
|
||||
[
|
||||
'person' => [
|
||||
'type' => 'person',
|
||||
'id' => $personId,
|
||||
],
|
||||
'start_date' => [
|
||||
'datetime' => $date->format(DateTimeInterface::RFC3339),
|
||||
],
|
||||
'position' => [
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId,
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => 'Introduced by automated test',
|
||||
],
|
||||
],
|
||||
'destination' => null,
|
||||
],
|
||||
true
|
||||
)
|
||||
->setParameter('center', "Center A")
|
||||
->setMaxResults(100)
|
||||
->getScalarResult()
|
||||
;
|
||||
);
|
||||
|
||||
\shuffle($personIds);
|
||||
$this->assertEquals(
|
||||
Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
|
||||
$household = new Household();
|
||||
$em->persist($household);
|
||||
$em->flush();
|
||||
$data = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$positions = $em->createQuery("SELECT pos.id FROM ".Position::class." pos ".
|
||||
"WHERE pos.shareHouseHold = TRUE")
|
||||
->getResult()
|
||||
;
|
||||
|
||||
$i = 0;
|
||||
do {
|
||||
$id = \array_pop($personIds)['id'];
|
||||
$person = self::$container->get(EntityManagerInterface::class)
|
||||
->getRepository(Person::Class)
|
||||
->find($id);
|
||||
|
||||
$participation = $person->getCurrentHouseholdParticipationShareHousehold();
|
||||
|
||||
if (NULL == $participation ||
|
||||
(
|
||||
NULL === $participation->getEndDate()
|
||||
&& $participation->getStartDate() <= $yesterday
|
||||
)) {
|
||||
|
||||
$i++;
|
||||
yield [
|
||||
$id,
|
||||
$household->getId(),
|
||||
$positions[\random_int(0, count($positions) - 1)]['id'],
|
||||
new \DateTimeImmutable('tomorrow')
|
||||
];
|
||||
}
|
||||
|
||||
} while ($i <= 1);
|
||||
$this->assertEquals(null, $data);
|
||||
}
|
||||
|
||||
public function provideValidDataEditMember(): \Iterator
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*
|
||||
* @param mixed $personId
|
||||
* @param mixed $householdId
|
||||
* @param mixed $positionId
|
||||
*/
|
||||
public function testMoveMember($personId, $householdId, $positionId, DateTimeInterface $date)
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$membershipIds = $em->createQuery("SELECT m.id FROM ".HouseholdMember::class." m ".
|
||||
"JOIN m.person p ".
|
||||
"JOIN p.center c ".
|
||||
"WHERE c.name = :center AND m.endDate IS NULL")
|
||||
->setParameter('center', 'Center A')
|
||||
->getScalarResult()
|
||||
;
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
json_encode(
|
||||
[
|
||||
'concerned' => [
|
||||
[
|
||||
'person' => [
|
||||
'type' => 'person',
|
||||
'id' => $personId,
|
||||
],
|
||||
'start_date' => [
|
||||
'datetime' => $date->format(DateTimeInterface::RFC3339),
|
||||
],
|
||||
'position' => [
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId,
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => 'Introduced by automated test',
|
||||
],
|
||||
],
|
||||
'destination' => [
|
||||
'type' => 'household',
|
||||
'id' => $householdId,
|
||||
],
|
||||
],
|
||||
true
|
||||
)
|
||||
);
|
||||
|
||||
\shuffle($membershipIds);
|
||||
$this->assertEquals(
|
||||
Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
}
|
||||
|
||||
yield [ \array_pop($membershipIds)['id'] ];
|
||||
/**
|
||||
* @dataProvider provideValidDataMove
|
||||
*
|
||||
* @param mixed $personId
|
||||
* @param mixed $householdId
|
||||
* @param mixed $positionId
|
||||
*/
|
||||
public function testMoveMemberToNewHousehold($personId, $householdId, $positionId, DateTimeInterface $date)
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$client->request(
|
||||
Request::METHOD_POST,
|
||||
'/api/1.0/person/household/members/move.json',
|
||||
[], // parameters
|
||||
[], // files
|
||||
[], // server
|
||||
json_encode(
|
||||
[
|
||||
'concerned' => [
|
||||
[
|
||||
'person' => [
|
||||
'type' => 'person',
|
||||
'id' => $personId,
|
||||
],
|
||||
'start_date' => [
|
||||
'datetime' => $date->format(DateTimeInterface::RFC3339),
|
||||
],
|
||||
'position' => [
|
||||
'type' => 'household_position',
|
||||
'id' => $positionId,
|
||||
],
|
||||
'holder' => false,
|
||||
'comment' => 'Introduced by automated test',
|
||||
],
|
||||
],
|
||||
'destination' => [
|
||||
'type' => 'household',
|
||||
],
|
||||
],
|
||||
true
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
|
||||
$data = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertIsArray($data);
|
||||
$this->assertArrayHasKey('members', $data);
|
||||
$this->assertIsArray($data['members']);
|
||||
$this->assertEquals(
|
||||
1,
|
||||
count($data['members']),
|
||||
'assert new household count one member'
|
||||
);
|
||||
$this->assertArrayHasKey('person', $data['members'][0]);
|
||||
$this->assertArrayHasKey('id', $data['members'][0]['person']);
|
||||
$this->assertEquals($personId, $data['members'][0]['person']['id']);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user