From f02d97ddb093c523e9a5be52bd08c177d82a9cc2 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 27 Jul 2023 10:18:11 +0200 Subject: [PATCH] FEATURE [centerHistory] attempt to complete test for centerHistory --- ...AccompanyingPeriodParticipationHandler.php | 1 - .../PersonMoveCenterHistoryHandler.php | 59 +++++++++++-------- .../Tests/Action/Remove/PersonMoveTest.php | 52 +++++++++++++++- 3 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index 5acd67126..e164dcaf8 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -15,7 +15,6 @@ class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlH public function getSqls(string $className, string $field, Person $from, Person $to): array { - var_dump(__METHOD__); $insertSql = sprintf(<<getId(), $from->getId(), $to->getId()); + $sqlStatements = []; - $updateSql = sprintf(<<em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]); +// $centerHistoriesA = $from->getCenterHistory()->toArray(); +// var_dump($centerHistoriesA); + $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); + if (!count($datesArrayA) === 0) { + $oldestDateA = min($datesArrayA); + } + $centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]); +// $centerHistoriesB = $to->getCenterHistory()->toArray(); +// var_dump($centerHistoriesB); + $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); + + if (!count($datesArrayB) === 0) { + $oldestDateB = min($datesArrayB); + + $indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB); + $oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter]; + } + + $sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); + + $sqlStatements = [$sqlDelete]; + + if ($oldestDateA <= $oldestDateB) { + $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + + $sqlStatements = [$sqlInsert, $sqlDelete]; + } + + return $sqlStatements; - SQL, $to->getId()); } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 05fc98aa6..fa6c5faa9 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -3,6 +3,7 @@ namespace Action\Remove; use Chill\ActivityBundle\Entity\Activity; +use Chill\MainBundle\Entity\Center; use Chill\PersonBundle\Actions\Remove\PersonMove; use Chill\PersonBundle\Actions\Remove\PersonMoveManager; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -62,7 +63,6 @@ class PersonMoveTest extends KernelTestCase $conn = $this->em->getConnection(); // $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { -// var_dump($sql); $conn->executeStatement($sql); } // }); @@ -74,6 +74,56 @@ class PersonMoveTest extends KernelTestCase self::assertNotNull($personB?->getId(), $message); } + public function testMovePersonCenterHistory(): void + { + $personA = new Person(); + $personB = new Person(); + $centerA = ($this->em->getRepository(Center::class))->find(1); + $centerB = ($this->em->getRepository(Center::class))->find(2); + + $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30')); + $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + + $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15')); + $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($personCenterHistoryAFirst); + $this->em->persist($personCenterHistoryASecond); + $this->em->persist($personCenterHistoryBFirst); + $this->em->persist($personCenterHistoryBSecond); + + $this->em->flush(); + $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()); + $personB = $this->em->find(Person::class, $personB->getId()); + $remainingCenterHistory = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $personB]); + + $message = 'Move persons with overlapping center histories'; + + self::assertNull($personA?->getId(), $message); + self::assertNotNull($personB?->getId(), $message); + self::assertEquals(new \DateTimeImmutable('2023-01-01'), $remainingCenterHistory->getStartDate()); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryAFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryASecond]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBSecond]; + } + public function dataProviderMovePerson(): iterable { $this->setUp();