diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php index 611f1763e..bf87bf7ee 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php @@ -156,7 +156,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues /** * @var Collection - * @ORM\OneToMany(targetEntity=AccompanyingPeriodWorkReferrerHistory::class, cascade={"persist", "remove"}, mappedBy="accompanyingPeriodWork") + * @ORM\OneToMany(targetEntity=AccompanyingPeriodWorkReferrerHistory::class, cascade={"persist", "remove"}, mappedBy="accompanyingPeriodWork", orphanRemoval=true) * @ORM\JoinTable(name="chill_person_accompanying_period_work_referrer") */ private Collection $referrersHistory; @@ -357,9 +357,17 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues */ public function getReferrers(): ReadableCollection { - return $this->referrersHistory->map(fn (AccompanyingPeriodWorkReferrerHistory $h) => $h->getUser()); + return $this->referrersHistory + ->filter(fn (AccompanyingPeriodWorkReferrerHistory $h) => null === $h->getEndDate()) + ->map(fn (AccompanyingPeriodWorkReferrerHistory $h) => $h->getUser()); } + public function getReferrersHistory(): Collection + { + return $this->referrersHistory; + } + + /** * @return Collection */ @@ -442,6 +450,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues if ($history->isDateRangeEmpty()) { $history->removeAccompanyingPeriodWork(); + $this->referrersHistory->removeElement($history); } } } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkReferrerHistory.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkReferrerHistory.php index 48b75d3e4..057b5a409 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkReferrerHistory.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkReferrerHistory.php @@ -38,29 +38,24 @@ class AccompanyingPeriodWorkReferrerHistory implements TrackCreationInterface, T * @var \DateTimeImmutable|null * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) */ - private ?\DateTimeImmutable $endDate; - - /** - * @var AccompanyingPeriodWork|null - * @ORM\ManyToOne(targetEntity=AccompanyingPeriodWork::class, inversedBy="referrersHistory") - */ - private ?AccompanyingPeriodWork $accompanyingPeriodWork; + private ?\DateTimeImmutable $endDate = null; public function __construct( - AccompanyingPeriodWork $accompanyingPeriodWork, + /** + * @ORM\ManyToOne(targetEntity=AccompanyingPeriodWork::class, inversedBy="referrersHistory") + */ + private ?AccompanyingPeriodWork $accompanyingPeriodWork, /** * @var User * @ORM\ManyToOne(targetEntity=User::class) */ - private readonly User $user, + private User $user, /** * @var \DateTimeImmutable * @ORM\Column(type="date_immutable", nullable=false) */ - private readonly \DateTimeImmutable $startDate, - ) { - $this->accompanyingPeriodWork = $accompanyingPeriodWork; - } + private \DateTimeImmutable $startDate + ) {} public function getId(): ?int { diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriod/AccompanyingPeriodWorkTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriod/AccompanyingPeriodWorkTest.php new file mode 100644 index 000000000..6771b0305 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriod/AccompanyingPeriodWorkTest.php @@ -0,0 +1,96 @@ +getReferrers()); + + $work->addReferrer($userA); + + self::assertCount(1, $work->getReferrers()); + self::assertContains($userA, $work->getReferrers()); + + $work->addReferrer($userB); + + self::assertCount(2, $work->getReferrers()); + self::assertContains($userA, $work->getReferrers()); + self::assertContains($userB, $work->getReferrers()); + + $work->addReferrer($userC); + $work->removeReferrer($userB); + + self::assertCount(2, $work->getReferrers()); + self::assertContains($userA, $work->getReferrers()); + self::assertNotContains($userB, $work->getReferrers()); + self::assertContains($userC, $work->getReferrers()); + + $work->removeReferrer($userA); + self::assertNotContains($userA, $work->getReferrers()); + self::assertNotContains($userB, $work->getReferrers()); + self::assertContains($userC, $work->getReferrers()); + } + + public function testReferrerHistoryOnDifferentDays(): void + { + + $work = new AccompanyingPeriodWork(); + $userA = new User(); + $userB = new User(); + $userC = new User(); + + $work->addReferrer($userA); + + $historyA = $work->getReferrersHistory()->first(); + $reflection = new \ReflectionClass($historyA); + $startDateReflection = $reflection->getProperty('startDate'); + $startDateReflection->setAccessible(true); + $startDateReflection->setValue($historyA, new \DateTimeImmutable('1 year ago')); + + $work->addReferrer($userB); + $work->addReferrer($userC); + + $work->removeReferrer($userB); + $work->removeReferrer($userA); + + self::assertCount(1, $work->getReferrers()); + self::assertNotContains($userA, $work->getReferrers()); + self::assertNotContains($userB, $work->getReferrers()); + self::assertContains($userC, $work->getReferrers()); + + self::assertCount(2, $work->getReferrersHistory()); + + $historyA = $work->getReferrersHistory() + ->filter(fn (AccompanyingPeriodWorkReferrerHistory $h) => $userA === $h->getUser()) + ->first(); + + self::assertNotFalse($historyA); + self::assertSame($userA, $historyA->getUser()); + self::assertEquals((new \DateTimeImmutable())->format('Y-m-d'), $historyA->getEndDate()->format('Y-m-d')); + } +}