Controller action to move members of an household

This commit is contained in:
2021-05-31 20:42:07 +02:00
parent ace3b1969e
commit 041b1dfc51
12 changed files with 356 additions and 48 deletions

View File

@@ -0,0 +1,98 @@
<?php
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\Position;
use Doctrine\ORM\EntityManagerInterface;
class HouseholdMemberControllerTest extends WebTestCase
{
use PrepareClientTrait;
/**
* @dataProvider provideValidData
*/
public function testMoveMember($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',
'id' => $householdId
]
],
true)
);
$this->assertEquals(Response::HTTP_OK,
$client->getResponse()->getStatusCode()
);
}
public function provideValidData(): \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);
$household = new Household();
$em->persist($household);
$em->flush();
$positions = $em->createQuery("SELECT pos.id FROM ".Position::class." pos ".
"WHERE pos.shareHouseHold = TRUE")
->getResult()
;
yield [
\array_pop($personIds)['id'],
$household->getId(),
$positions[\random_int(0, count($positions) - 1)]['id'],
new \DateTimeImmutable('today')
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Bundle\ChillPersonBundle\Tests\Entity\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Household\Position;
use PHPUnit\Framework\TestCase;
class HouseholdMemberTest extends TestCase
{
public function testPositionSharehousehold()
{
$position = (new Position())
->setShareHousehold(true)
;
$membership = (new HouseholdMember())
->setPosition($position)
;
$this->assertTrue($membership->getShareHousehold());
}
public function testPositionDoNotSharehousehold()
{
$position = (new Position())
->setShareHousehold(false)
;
$membership = (new HouseholdMember())
->setPosition($position)
;
$this->assertFalse($membership->getShareHousehold());
}
}

View File

@@ -37,10 +37,10 @@ class MembersEditorTest extends TestCase
$person,
$position);
$this->assertInstanceOf(Collection::class, $person->getHouseholdParticipations());
$this->assertEquals(1, $person->getHouseholdParticipations()->count());
$persistables = $editor->getPersistable();
$this->assertEquals(\count($persistables), 1);
$membership1 = $person->getHouseholdParticipations()->first();
$membership1 = $persistables[0];
$this->assertSame($household1, $membership1->getHousehold());
$this->assertNull($membership1->getEndDate());
@@ -52,9 +52,10 @@ class MembersEditorTest extends TestCase
$person,
$position);
$this->assertEquals(2, $person->getHouseholdParticipations()->count());
$persistables = $editor->getPersistable();
$this->assertEquals(1, count($persistables));
$membership2 = $person->getHouseholdParticipations()->last();
$membership2 = $persistables[0];
$this->assertSame($household2, $membership2->getHousehold());
$this->assertNull($membership2->getEndDate());
$this->assertNotNull($membership1->getEndDate(),
@@ -77,8 +78,8 @@ class MembersEditorTest extends TestCase
$person,
$position);
$this->assertInstanceOf(Collection::class, $person->getHouseholdParticipations());
$this->assertEquals(1, $person->getHouseholdParticipations()->count());
$persistables = $editor->getPersistable();
$this->assertEquals(1, count($persistables));
$membership1 = $person->getHouseholdParticipations()->first();
$this->assertSame($household1, $membership1->getHousehold());
@@ -92,7 +93,8 @@ class MembersEditorTest extends TestCase
$person,
$position);
$this->assertEquals(2, $person->getHouseholdParticipations()->count());
$persistables = $editor->getPersistable();
$this->assertEquals(1, count($persistables));
$membership2 = $person->getHouseholdParticipations()->last();
$this->assertNull($membership2->getEndDate());