Make test personMove work for centers

This commit is contained in:
2023-09-07 16:07:23 +02:00
parent 256579af89
commit 94f26df81e
4 changed files with 58 additions and 40 deletions

View File

@@ -5,6 +5,7 @@ namespace Action\Remove;
use Chill\ActivityBundle\Entity\Activity;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\PersonBundle\Actions\Remove\PersonMove;
use Chill\PersonBundle\Actions\Remove\PersonMoveManager;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
@@ -28,6 +29,8 @@ class PersonMoveTest extends KernelTestCase
private EventDispatcherInterface $eventDispatcher;
private CenterRepositoryInterface $centerRepository;
/**
* @var list<array{0: class-string, 1: int}>
*/
@@ -39,6 +42,7 @@ class PersonMoveTest extends KernelTestCase
$this->em = self::$container->get(EntityManagerInterface::class);
$this->personMoveManager = self::$container->get(PersonMoveManager::class);
$this->eventDispatcher = self::$container->get(EventDispatcherInterface::class);
$this->centerRepository = self::$container->get(CenterRepositoryInterface::class);
}
public static function tearDownAfterClass(): void
@@ -60,21 +64,22 @@ class PersonMoveTest extends KernelTestCase
/**
* @dataProvider dataProviderMovePerson
*/
public function testMovePerson(Person $personA, Person $personB, string $message): void
public function testMovePersonSimple(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());
$personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id")
->setParameter('id', $personA->getId())
->getResult();
$personB = $this->em->find(Person::class, $personB->getId());
self::assertNull($personA?->getId(), $message);
self::assertCount(0, $personsByIdOfA);
self::assertNotNull($personB?->getId(), $message);
}
@@ -82,26 +87,23 @@ class PersonMoveTest extends KernelTestCase
{
$personA = new Person();
$personB = new Person();
$centerA = ($this->em->getRepository(Center::class))->find(1);
$centerB = ($this->em->getRepository(Center::class))->find(2);
[$centerA, $centerB] = $this->centerRepository->findAll();
$this->em->persist($personA);
$this->em->persist($personB);
$personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30'));
$personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30'));
$personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15'));
$personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30'));
// $personA->getCenterHistory()->add($personCenterHistoryAFirst);
// $personA->getCenterHistory()->add($personCenterHistoryASecond);
// $personB->getCenterHistory()->add($personCenterHistoryBFirst);
// $personB->getCenterHistory()->add($personCenterHistoryBSecond);
// $personCenterHistoryAFirst->setPerson($personA);
// $personCenterHistoryASecond->setPerson($personA);
// $personCenterHistoryBFirst->setPerson($personB);
// $personCenterHistoryBSecond->setPerson($personB);
$personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA)
->setStartDate(new \DateTimeImmutable('2023-01-01'))
->setEndDate(new \DateTimeImmutable('2023-06-30'));
$personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB)
->setStartDate(new \DateTimeImmutable('2023-06-30'))
->setEndDate(new \DateTimeImmutable('2023-09-30'));
$personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA)
->setStartDate(new \DateTimeImmutable('2023-03-01'))
->setEndDate(new \DateTimeImmutable('2023-07-15'));
$personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB)
->setStartDate(new \DateTimeImmutable('2023-07-15'))
->setEndDate(new \DateTimeImmutable('2023-09-30'));
$this->em->persist($personCenterHistoryAFirst);
$this->em->persist($personCenterHistoryASecond);
@@ -114,27 +116,36 @@ class PersonMoveTest extends KernelTestCase
$personB->addCenterHistory($personCenterHistoryBSecond);
$this->em->flush();
// $this->em->refresh($personA);
// $this->em->refresh($personB);
$this->em->clear();
$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());
$personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id")
->setParameter('id', $personA->getId())
->getResult();
/** @var Person $personB */
$personB = $this->em->find(Person::class, $personB->getId());
$message = 'Move persons with overlapping center histories';
self::assertNull($personA?->getId(), $message);
$this->em->refresh($personB);
self::assertCount(0, $personsByIdOfA);
self::assertNotNull($personB?->getId(), $message);
$centerHistoriesB = $personB->getCenterHistory();
$oldestDate = new \DateTimeImmutable('2023-01-01');
$this->em->refresh($centerHistoriesB->first());
self::assertCount(2, $centerHistoriesB);
self::assertEquals($oldestDate, $centerHistoriesB->first()->getStartDate());
self::$entitiesToDelete[] = [Person::class, $personA];
self::$entitiesToDelete[] = [Person::class, $personB];
self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryAFirst];
@@ -223,7 +234,7 @@ class PersonMoveTest extends KernelTestCase
$personB = new Person();
$relationship = new Relationship();
$relation = new Relation();
$user = new User();
$user = (new User())->setUsername(uniqid())->setEmail(uniqid() . '@foo.com');
$relationship->setRelation($relation);
$relationship->setToPerson($personA);