Fix ClosingMotiveRender: rendering of label 'canceled' at the end of child, and remove trailing spaces

This commit is contained in:
Julien Fastré 2023-10-17 10:29:03 +02:00
parent 48e0a0af7d
commit 75e15c1389
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 95 additions and 12 deletions

View File

@ -11,10 +11,9 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Templating\Entity; namespace Chill\PersonBundle\Templating\Entity;
use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender;
use Chill\MainBundle\Templating\Entity\BoxUtilsChillEntityRenderTrait; use Chill\MainBundle\Templating\Entity\BoxUtilsChillEntityRenderTrait;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface; use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -29,7 +28,7 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface
private const SEPARATOR = ' > '; private const SEPARATOR = ' > ';
public function __construct( public function __construct(
private TranslatableStringHelper $translatableStringHelper, private TranslatableStringHelperInterface $translatableStringHelper,
private TranslatorInterface $translator private TranslatorInterface $translator
) {} ) {}
@ -45,7 +44,7 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface
{ {
return $this->renderStringRecursive( return $this->renderStringRecursive(
$entity, $entity,
'', //$this->translatableStringHelper->localize($entity->getName()), '',
$options $options
); );
} }
@ -55,27 +54,30 @@ final readonly class ClosingMotiveRender implements ChillEntityRenderInterface
return $entity instanceof ClosingMotive; 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()); $str = $this->translatableStringHelper->localize($motive->getName());
$isCanceled = $motive->getIsCanceledAccompanyingPeriod() ? $this->translator->trans('( Canceled period )') : '';
if ($motive->hasParent()) { if ($motive->hasParent()) {
if ('' !== $existing) { if ('' !== $existing) {
$newExisting = $newExisting . self::SEPARATOR . $existing; $str = $str . self::SEPARATOR . $existing;
} }
return $this->renderStringRecursive( $str = $this->renderStringRecursive(
$motive->getParent(), $motive->getParent(),
$newExisting . ' ' . $isCanceled, $str,
$options $options
); );
} }
if ('' !== $existing) { 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;
} }
} }

View File

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Templating\Entity;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\PersonBundle\Templating\Entity\ClosingMotiveRender;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
* @coversNothing
*/
class ClosingMotiveRenderTest extends KernelTestCase
{
private static ClosingMotiveRender $closingMotiveRender;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$closingMotiveRender = self::$container->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é)'];
}
}