From 94f26df81e370b3e348162fff2af219207961243 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 7 Sep 2023 16:07:23 +0200 Subject: [PATCH] Make test personMove work for centers --- .../PersonMoveCenterHistoryHandler.php | 31 +++++---- .../Actions/Remove/PersonMove.php | 1 - .../ChillPersonBundle/Entity/Person.php | 1 + .../Tests/Action/Remove/PersonMoveTest.php | 65 +++++++++++-------- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 4d4203a0b..220adcfc1 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -5,10 +5,14 @@ namespace Chill\PersonBundle\Actions\Remove\Handler; use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository; +use Doctrine\ORM\EntityManagerInterface; class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface { - public function __construct(private PersonCenterHistoryRepository $centerHistoryRepository) + public function __construct( + private PersonCenterHistoryRepository $centerHistoryRepository, + private EntityManagerInterface $em + ) { } @@ -23,20 +27,23 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $oldestDateA = null; $oldestDateB = null; + $oldestCenterHistoryB = null; + $oldestCenterHistoryA = null; + $centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]); - $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); - if (!count($datesArrayA) === 0) { - $oldestDateA = min($datesArrayA); + foreach ($centerHistoriesA as $ch) { + if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) { + $oldestDateA = $ch->getStartDate(); + $oldestCenterHistoryA = $ch; + } } $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); - $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); - - if (!count($datesArrayB) === 0) { - $oldestDateB = min($datesArrayB); - - $indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB); - $oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter]; + foreach ($centerHistoriesB as $ch) { + if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) { + $oldestDateB = $ch->getStartDate(); + $oldestCenterHistoryB = $ch; + } } $sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); @@ -44,7 +51,7 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $sqlStatements = [$sqlDelete]; if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { - $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s' WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index ecf17d3b4..c3ecd8ec1 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -209,7 +209,6 @@ class PersonMove private function getDeleteEntities(): array { return [ - Person\PersonCenterHistory::class, AccompanyingPeriod\AccompanyingPeriodWork::class, Relationship::class ]; diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 81e603fbc..38e51a60a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -203,6 +203,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI /** * @ORM\OneToMany(targetEntity=PersonCenterHistory::class, mappedBy="person", cascade={"persist"}) + * @ORM\OrderBy({"startDate": "ASC", "id": "ASC"}) * * @var Collection|PersonCenterHistory[] */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 64b39a20b..14268160e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -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 */ @@ -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);