From 75e15c138936ad15f632ef6b15489a2eefaac35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 17 Oct 2023 10:29:03 +0200 Subject: [PATCH] Fix ClosingMotiveRender: rendering of label 'canceled' at the end of child, and remove trailing spaces --- .../Templating/Entity/ClosingMotiveRender.php | 26 +++--- .../Entity/ClosingMotiveRenderTest.php | 81 +++++++++++++++++++ 2 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Templating/Entity/ClosingMotiveRenderTest.php diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/ClosingMotiveRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/ClosingMotiveRender.php index a70eeb6a7..5d44f6967 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/ClosingMotiveRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/ClosingMotiveRender.php @@ -11,10 +11,9 @@ declare(strict_types=1); namespace Chill\PersonBundle\Templating\Entity; -use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender; use Chill\MainBundle\Templating\Entity\BoxUtilsChillEntityRenderTrait; use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface; -use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Symfony\Contracts\Translation\TranslatorInterface; @@ -29,7 +28,7 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface private const SEPARATOR = ' > '; public function __construct( - private TranslatableStringHelper $translatableStringHelper, + private TranslatableStringHelperInterface $translatableStringHelper, private TranslatorInterface $translator ) {} @@ -45,7 +44,7 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface { return $this->renderStringRecursive( $entity, - '', //$this->translatableStringHelper->localize($entity->getName()), + '', $options ); } @@ -55,27 +54,30 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface return $entity instanceof ClosingMotive; } - private function renderStringRecursive(ClosingMotive $motive, string $existing, array $options) + private function renderStringRecursive(ClosingMotive $motive, string $existing, array $options): string { - $newExisting = $this->translatableStringHelper->localize($motive->getName()); - $isCanceled = $motive->getIsCanceledAccompanyingPeriod() ? $this->translator->trans('( Canceled period )') : ''; + $str = $this->translatableStringHelper->localize($motive->getName()); if ($motive->hasParent()) { if ('' !== $existing) { - $newExisting = $newExisting . self::SEPARATOR . $existing; + $str = $str . self::SEPARATOR . $existing; } - return $this->renderStringRecursive( + $str = $this->renderStringRecursive( $motive->getParent(), - $newExisting . ' ' . $isCanceled, + $str, $options ); } if ('' !== $existing) { - return $newExisting . self::SEPARATOR . $existing; + $str = $str . self::SEPARATOR . $existing; } - return $newExisting . ' ' . $isCanceled; + if ($motive->isLeaf() && $motive->isCanceledAccompanyingPeriod()) { + $str = $str . ' ' . $this->translator->trans('( Canceled period )'); + } + + return $str; } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Templating/Entity/ClosingMotiveRenderTest.php b/src/Bundle/ChillPersonBundle/Tests/Templating/Entity/ClosingMotiveRenderTest.php new file mode 100644 index 000000000..8667576bc --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Templating/Entity/ClosingMotiveRenderTest.php @@ -0,0 +1,81 @@ +get(ClosingMotiveRender::class); + } + + /** + * @dataProvider provideClosingMotiveWithRenderString + * @return void + */ + public function testRenderString(ClosingMotive $closingMotive, string $expected): void + { + $actual = self::$closingMotiveRender->renderString($closingMotive, []); + + self::assertEquals($expected, $actual); + } + + /** + * @dataProvider provideClosingMotiveWithRenderString + * @return void + */ + public function testRenderBox(ClosingMotive $closingMotive, string $expectedContent): void + { + $actual = strip_tags(self::$closingMotiveRender->renderBox($closingMotive, [])); + + self::assertStringContainsString($expectedContent, $actual); + + } + + public function provideClosingMotiveWithRenderString(): iterable + { + $closingMotive = (new ClosingMotive())->setName(['fr' => 'Left']); + + yield [$closingMotive, 'Left']; + + $closingMotive = (new ClosingMotive())->setName(['fr' => 'Left'])->setIsCanceledAccompanyingPeriod(true); + + yield [$closingMotive, 'Left (annulé)']; + + $closingMotiveParent = (new ClosingMotive())->setName(['fr' => 'Parent']); + $closingMotive = (new ClosingMotive())->setName(['fr' => 'Children']); + $closingMotiveParent->addChildren($closingMotive); + + yield [$closingMotive, 'Parent > Children']; + + $closingMotiveParent = (new ClosingMotive())->setName(['fr' => 'Parent'])->setIsCanceledAccompanyingPeriod(true); + $closingMotive = (new ClosingMotive())->setName(['fr' => 'Children']); + $closingMotiveParent->addChildren($closingMotive); + + yield [$closingMotive, 'Parent > Children (annulé)']; + + } + + + +}