mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
158 lines
5.2 KiB
PHP
158 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace Action\Remove;
|
|
|
|
use Chill\ActivityBundle\Entity\Activity;
|
|
use Chill\PersonBundle\Actions\Remove\PersonMove;
|
|
use Chill\PersonBundle\Actions\Remove\PersonMoveManager;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Doctrine\DBAL\Connection;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
class PersonMoveTest extends KernelTestCase
|
|
{
|
|
private EntityManagerInterface $em;
|
|
|
|
private PersonMoveManager $personMoveManager;
|
|
|
|
private EventDispatcherInterface $eventDispatcher;
|
|
|
|
/**
|
|
* @var list<array{0: class-string, 1: int}>
|
|
*/
|
|
private static $entitiesToDelete = [];
|
|
|
|
public function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
$this->em = self::$container->get(EntityManagerInterface::class);
|
|
$this->personMoveManager = self::$container->get(PersonMoveManager::class);
|
|
$this->eventDispatcher = self::$container->get(EventDispatcherInterface::class);
|
|
}
|
|
|
|
public static function tearDownAfterClass(): void
|
|
{
|
|
self::bootKernel();
|
|
$em = self::$container->get(EntityManagerInterface::class);
|
|
|
|
foreach (self::$entitiesToDelete as list($class, $id)) {
|
|
$entity = $em->find($class, $id);
|
|
|
|
if (null !== $entity) {
|
|
$em->remove($entity);
|
|
}
|
|
}
|
|
|
|
$em->flush();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataProviderMovePerson
|
|
*/
|
|
public function testMovePerson(Person $personA, Person $personB, string $message): void
|
|
{
|
|
$move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher);
|
|
$sqls = $move->getSQL($personA, $personB);
|
|
//$conn = $this->em->getConnection();
|
|
$this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) {
|
|
foreach ($sqls as $sql) {
|
|
$conn->executeStatement($sql);
|
|
}
|
|
});
|
|
|
|
$personA = $this->em->find(Person::class, $personA->getId());
|
|
$personB = $this->em->find(Person::class, $personB->getId());
|
|
|
|
self::assertNull($personA?->getId(), $message);
|
|
self::assertNotNull($personB?->getId(), $message);
|
|
}
|
|
|
|
public function dataProviderMovePerson(): iterable
|
|
{
|
|
$this->setUp();
|
|
|
|
$personA = new Person();
|
|
$personB = new Person();
|
|
|
|
$this->em->persist($personA);
|
|
$this->em->persist($personB);
|
|
|
|
self::$entitiesToDelete[] = [Person::class, $personA];
|
|
self::$entitiesToDelete[] = [Person::class, $personB];
|
|
|
|
yield [$personA, $personB, "move 2 people without any associated data"];
|
|
|
|
$personA = new Person();
|
|
$personB = new Person();
|
|
|
|
$activity = new Activity();
|
|
$activity->setDate(new \DateTime('today'));
|
|
$activity->addPerson($personA);
|
|
$activity->addPerson($personB);
|
|
|
|
$this->em->persist($personA);
|
|
$this->em->persist($personB);
|
|
$this->em->persist($activity);
|
|
|
|
self::$entitiesToDelete[] = [Person::class, $personA];
|
|
self::$entitiesToDelete[] = [Person::class, $personB];
|
|
self::$entitiesToDelete[] = [Activity::class, $activity];
|
|
|
|
yield [$personA, $personB, "move 2 people having an activity"];
|
|
|
|
$personA = new Person();
|
|
$personB = new Person();
|
|
$household = new Household();
|
|
$household->addMember(
|
|
$memberA = (new HouseholdMember())->setPerson($personA)->setShareHousehold(true)
|
|
->setStartDate(new \DateTimeImmutable('2023-01-01'))
|
|
);
|
|
$household->addMember(
|
|
$memberB = (new HouseholdMember())->setPerson($personB)->setShareHousehold(true)
|
|
->setStartDate(new \DateTimeImmutable('2023-01-01'))
|
|
);
|
|
|
|
|
|
|
|
$this->em->persist($personA);
|
|
$this->em->persist($personB);
|
|
$this->em->persist($household);
|
|
$this->em->persist($memberA);
|
|
$this->em->persist($memberB);
|
|
|
|
self::$entitiesToDelete[] = [Person::class, $personA];
|
|
self::$entitiesToDelete[] = [Person::class, $personB];
|
|
self::$entitiesToDelete[] = [HouseholdMember::class, $memberA];
|
|
self::$entitiesToDelete[] = [HouseholdMember::class, $memberB];
|
|
self::$entitiesToDelete[] = [Household::class, $household];
|
|
|
|
yield [$personA, $personB, "move 2 people having the same household at the same time"];
|
|
|
|
$personA = new Person();
|
|
$personB = new Person();
|
|
$parcours = new AccompanyingPeriod();
|
|
|
|
$parcours->addPerson($personA);
|
|
$parcours->addPerson($personB);
|
|
|
|
$this->em->persist($personA);
|
|
$this->em->persist($personB);
|
|
$this->em->persist($parcours);
|
|
|
|
self::$entitiesToDelete[] = [Person::class, $personA];
|
|
self::$entitiesToDelete[] = [Person::class, $personB];
|
|
self::$entitiesToDelete[] = [AccompanyingPeriod::class, $parcours];
|
|
|
|
yield [$personA, $personB, "move 2 people participating to the same parcours"];
|
|
|
|
$this->em->flush();
|
|
$this->em->clear();
|
|
}
|
|
}
|