From 4f18b1d2b2a968086c24a3f0b3008ea340d929a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 23 Oct 2024 00:51:09 +0200 Subject: [PATCH] Add services and tests for associated entities management Implemented services to provide associated persons and third parties for accompanying periods and their works. Included comprehensive tests to ensure proper functionality and associations. --- .../ProvidePersonsAssociated.php | 34 ++++++++++++ .../ProvideThirdPartiesAssociated.php | 50 ++++++++++++++++++ .../ProvidePersonsAssociated.php | 32 ++++++++++++ .../ProvideThirdPartiesAssociated.php | 49 +++++++++++++++++ .../ProvideThirdPartiesAssociatedTest.php | 45 ++++++++++++++++ .../ProvideThirdPartiesAssociatedTest.php | 52 +++++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvidePersonsAssociated.php create mode 100644 src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvideThirdPartiesAssociated.php create mode 100644 src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvidePersonsAssociated.php create mode 100644 src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociated.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriod/ProvideThirdPartiesAssociatedTest.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociatedTest.php diff --git a/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvidePersonsAssociated.php b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvidePersonsAssociated.php new file mode 100644 index 000000000..af1ae6e0f --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvidePersonsAssociated.php @@ -0,0 +1,34 @@ + + */ + public function getPersonsAssociated(AccompanyingPeriod $period): array + { + return array_values( + $period->getCurrentParticipations() + ->map(fn (AccompanyingPeriodParticipation $participation) => $participation->getPerson()) + ->toArray() + ); + } +} diff --git a/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvideThirdPartiesAssociated.php b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvideThirdPartiesAssociated.php new file mode 100644 index 000000000..5aa24bcda --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriod/ProvideThirdPartiesAssociated.php @@ -0,0 +1,50 @@ + + */ + public function getThirdPartiesAssociated(AccompanyingPeriod $period): array + { + $thirdParties = []; + + foreach ( + $period + ->getResources()->filter(fn (Resource $resource) => null !== $resource->getThirdParty()) + ->map(fn (Resource $resource) => $resource->getThirdParty()) as $thirdParty) { + $thirdParties[] = $thirdParty; + } + + if (null !== $requestor = $period->getRequestorThirdParty()) { + $thirdParties[] = $requestor; + } + + return array_values( + // filter objects to remove duplicates + array_filter( + $thirdParties, + fn ($o, $k) => array_search($o, $thirdParties, true) === $k, + ARRAY_FILTER_USE_BOTH + ) + ); + } +} diff --git a/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvidePersonsAssociated.php b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvidePersonsAssociated.php new file mode 100644 index 000000000..5fd885948 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvidePersonsAssociated.php @@ -0,0 +1,32 @@ + + */ + public function getPersonsAssociated(AccompanyingPeriod\AccompanyingPeriodWork $work): array + { + return $this->providePersonsAssociated->getPersonsAssociated($work->getAccompanyingPeriod()); + } +} diff --git a/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociated.php b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociated.php new file mode 100644 index 000000000..2aa73935d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociated.php @@ -0,0 +1,49 @@ + + */ + public function getThirdPartiesAssociated(AccompanyingPeriodWork $accompanyingPeriodWork): array + { + $thirdParties = $this->thirdPartiesAssociated->getThirdPartiesAssociated($accompanyingPeriodWork->getAccompanyingPeriod()); + + if (null !== $tp = $accompanyingPeriodWork->getHandlingThierParty()) { + $thirdParties[] = $tp; + } + + foreach ($accompanyingPeriodWork->getThirdParties() as $thirdParty) { + $thirdParties[] = $thirdParty; + } + + return array_values( + // filter objects to remove duplicates + array_filter( + $thirdParties, + fn ($o, $k) => array_search($o, $thirdParties, true) === $k, + ARRAY_FILTER_USE_BOTH + ) + ); + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriod/ProvideThirdPartiesAssociatedTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriod/ProvideThirdPartiesAssociatedTest.php new file mode 100644 index 000000000..ccc2a902e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriod/ProvideThirdPartiesAssociatedTest.php @@ -0,0 +1,45 @@ +setRequestor($tp1 = new ThirdParty()); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($tp1)); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($tp2 = new ThirdParty())); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($p1 = new Person())); + + $provider = new ProvideThirdPartiesAssociated(); + + $thirdParties = $provider->getThirdPartiesAssociated($period); + + self::assertCount(2, $thirdParties); + self::assertContains($tp1, $thirdParties); + self::assertContains($tp2, $thirdParties); + self::assertNotContains($p1, $thirdParties); + self::assertNotContains(null, $thirdParties); + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociatedTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociatedTest.php new file mode 100644 index 000000000..5ce7b953e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Service/AccompanyingPeriodWork/ProvideThirdPartiesAssociatedTest.php @@ -0,0 +1,52 @@ +setRequestor($tp1 = new ThirdParty()); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($tp1)); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($tp2 = new ThirdParty())); + $period->addResource((new AccompanyingPeriod\Resource())->setResource($p1 = new Person())); + $period->addWork($work = new AccompanyingPeriod\AccompanyingPeriodWork()); + $work->addThirdParty($tp3 = new ThirdParty()); + $work->addThirdParty($tp1); + $work->setHandlingThierParty($tp4 = new ThirdParty()); + + $providerAccPeriod = new \Chill\PersonBundle\Service\AccompanyingPeriod\ProvideThirdPartiesAssociated(); + $provider = new ProvideThirdPartiesAssociated($providerAccPeriod); + + + $thirdParties = $provider->getThirdPartiesAssociated($work); + + self::assertContains($tp1, $thirdParties); + self::assertContains($tp2, $thirdParties); + self::assertContains($tp3, $thirdParties); + self::assertContains($tp4, $thirdParties); + self::assertNotContains($p1, $thirdParties); + self::assertCount(4, $thirdParties); + } +}