mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
Link between address and reference, and api endpoint to find household
by address reference * add a one-to-many link between address and address reference; * update AddAddress.vue to add information on picked address reference; * add an HouseholdACLAwareRepository, with a method to find household by current address reference * add an endpoint to retrieve household by address reference * + tests
This commit is contained in:
@@ -2,9 +2,14 @@
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Entity\AddressReference;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
@@ -15,6 +20,8 @@ class HouseholdApiControllerTest extends WebTestCase
|
||||
|
||||
use PrepareClientTrait;
|
||||
|
||||
private array $toDelete = [];
|
||||
|
||||
/**
|
||||
* @dataProvider generatePersonId
|
||||
*/
|
||||
@@ -45,6 +52,77 @@ class HouseholdApiControllerTest extends WebTestCase
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateHouseholdAssociatedWithAddressReference
|
||||
*/
|
||||
public function testFindHouseholdByAddressReference(int $addressReferenceId, int $expectedHouseholdId)
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$client->request(
|
||||
Request::METHOD_GET,
|
||||
"/api/1.0/person/household/by-address-reference/$addressReferenceId.json"
|
||||
);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$data = json_decode($client->getResponse()->getContent(), true);
|
||||
$this->assertArrayHasKey('count', $data);
|
||||
$this->assertArrayHasKey('results', $data);
|
||||
|
||||
$householdIds = \array_map(function($r) {
|
||||
return $r['id'];
|
||||
}, $data['results']);
|
||||
|
||||
$this->assertContains($expectedHouseholdId, $householdIds);
|
||||
}
|
||||
|
||||
public function generateHouseholdAssociatedWithAddressReference()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$centerA = $em->getRepository(Center::class)->findOneBy(['name' => 'Center A']);
|
||||
$nbReference = $em->createQueryBuilder()->select('count(ar)')->from(AddressReference::class, 'ar')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
$reference = $em->createQueryBuilder()->select('ar')->from(AddressReference::class, 'ar')
|
||||
->setFirstResult(\random_int(0, $nbReference))
|
||||
->setMaxResults(1)
|
||||
->getQuery()->getSingleResult();
|
||||
$p = new Person();
|
||||
$p->setFirstname('test')->setLastName('test lastname')
|
||||
->setGender(Person::BOTH_GENDER)
|
||||
->setCenter($centerA)
|
||||
;
|
||||
$em->persist($p);
|
||||
$h = new Household();
|
||||
$h->addMember($m = (new HouseholdMember())->setPerson($p));
|
||||
$h->addAddress(Address::createFromAddressReference($reference)->setValidFrom(new \DateTime('today')));
|
||||
$em->persist($m);
|
||||
$em->persist($h);
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->toDelete = $this->toDelete + [
|
||||
[HouseholdMember::class, $m->getId()],
|
||||
[User::class, $p->getId()],
|
||||
[Household::class, $h->getId()]
|
||||
];
|
||||
|
||||
yield [$reference->getId(), $h->getId()];
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
foreach ($this->toDelete as list($class, $id)) {
|
||||
$obj = $em->getRepository($class)->find($id);
|
||||
$em->remove($obj);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
public function generatePersonId()
|
||||
{
|
||||
self::bootKernel();
|
||||
@@ -64,7 +142,7 @@ class HouseholdApiControllerTest extends WebTestCase
|
||||
;
|
||||
|
||||
$person = $period->getParticipations()
|
||||
->first()->getPerson();
|
||||
->first()->getPerson();
|
||||
|
||||
yield [ $person->getId() ];
|
||||
}
|
||||
|
Reference in New Issue
Block a user