Make test personMove work for centers

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

View File

@ -5,10 +5,14 @@ namespace Chill\PersonBundle\Actions\Remove\Handler;
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository; use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository;
use Doctrine\ORM\EntityManagerInterface;
class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface 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; $oldestDateA = null;
$oldestDateB = null; $oldestDateB = null;
$oldestCenterHistoryB = null;
$oldestCenterHistoryA = null;
$centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]); $centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]);
$datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); foreach ($centerHistoriesA as $ch) {
if (!count($datesArrayA) === 0) { if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) {
$oldestDateA = min($datesArrayA); $oldestDateA = $ch->getStartDate();
$oldestCenterHistoryA = $ch;
}
} }
$centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]);
$datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); foreach ($centerHistoriesB as $ch) {
if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) {
if (!count($datesArrayB) === 0) { $oldestDateB = $ch->getStartDate();
$oldestDateB = min($datesArrayB); $oldestCenterHistoryB = $ch;
}
$indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB);
$oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter];
} }
$sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); $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]; $sqlStatements = [$sqlDelete];
if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { 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]; $sqlStatements = [$sqlInsert, $sqlDelete];
} }

View File

@ -209,7 +209,6 @@ class PersonMove
private function getDeleteEntities(): array private function getDeleteEntities(): array
{ {
return [ return [
Person\PersonCenterHistory::class,
AccompanyingPeriod\AccompanyingPeriodWork::class, AccompanyingPeriod\AccompanyingPeriodWork::class,
Relationship::class Relationship::class
]; ];

View File

@ -203,6 +203,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/** /**
* @ORM\OneToMany(targetEntity=PersonCenterHistory::class, mappedBy="person", cascade={"persist"}) * @ORM\OneToMany(targetEntity=PersonCenterHistory::class, mappedBy="person", cascade={"persist"})
* @ORM\OrderBy({"startDate": "ASC", "id": "ASC"})
* *
* @var Collection|PersonCenterHistory[] * @var Collection|PersonCenterHistory[]
*/ */

View File

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