address: add unit test for address render template

This commit is contained in:
nobohan
2022-01-06 15:08:59 +01:00
parent 4c4a003977
commit 6bae7606dd
2 changed files with 175 additions and 40 deletions

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Templating\Entity;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Templating\EngineInterface;
use function array_merge;
@@ -30,10 +31,12 @@ class AddressRender implements ChillEntityRenderInterface
];
private EngineInterface $templating;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(EngineInterface $templating)
public function __construct(EngineInterface $templating, TranslatableStringHelper $translatableStringHelper)
{
$this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
@@ -47,9 +50,10 @@ class AddressRender implements ChillEntityRenderInterface
return $this->templating
->render('@ChillMain/Entity/address.html.twig', [
'address' => $addr,
'streetLine' => $this->renderStreetLine($addr),
'streetLine' => $this->renderStreetLine($addr), // TODO inutile?
'render' => $options['render'] ?? 'bloc',
'options' => $options,
'lines' => $this->renderLines($addr),
]);
}
@@ -62,25 +66,23 @@ class AddressRender implements ChillEntityRenderInterface
public function renderLines($addr): array
{
$lines = [];
$lines[0] = $this->renderBuildingLine($addr);
$lines[1] = $this->renderDeliveryLine($addr);
$lines[2] = $this->renderStreetLine($addr);
$lines[3] = $this->renderCityLine($addr);
$lines[4] = $this->renderCountryLine($addr);
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
if (null !== $addr->getPostCode()) {
if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
$lines[0] = $this->renderIntraBuildingLine($addr);
$lines[1] = $this->renderBuildingLine($addr);
$lines[2] = $this->renderStreetLine($addr);
$lines[3] = $this->renderDeliveryLine($addr);
$lines[4] = $this->renderCityLine($addr);
$lines[5] = $this->renderCountryLine($addr);
$lines[] = $this->renderIntraBuildingLine($addr);
$lines[] = $this->renderBuildingLine($addr);
$lines[] = $this->renderStreetLine($addr);
$lines[] = $this->renderDeliveryLine($addr);
$lines[] = $this->renderCityLine($addr);
$lines[] = $this->renderCountryLine($addr); // TODO only if != available country
} else {
$lines[] = $this->renderBuildingLine($addr);
$lines[] = $this->renderDeliveryLine($addr);
$lines[] = $this->renderStreetLine($addr);
$lines[] = $this->renderCityLine($addr);
$lines[] = $this->renderCountryLine($addr);
}
}
return $lines;
return array_values(array_filter($lines, fn ($l) => null !== $l));
}
/**
@@ -99,14 +101,23 @@ class AddressRender implements ChillEntityRenderInterface
private function renderBuildingLine($addr): ?string
{
$res = $addr->getBuildingName() . ' - ' . $this->renderIntraBuildingLine($addr);
if (null === $addr->getBuildingName()) {
$res = $this->renderIntraBuildingLine($addr);
if (!empty($addr->getBuildingName())) {
$building = $addr->getBuildingName();
} else {
$building = '';
}
if (null === $this->renderIntraBuildingLine($addr)) {
return null;
}
if (!empty($this->renderIntraBuildingLine($addr))) {
$intraBuilding = $this->renderIntraBuildingLine($addr);
} else {
$intraBuilding = '';
}
$res = trim($building . ' - ' . $intraBuilding, ' - ');
if ('' === $res) {
$res = null;
}
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
@@ -140,7 +151,9 @@ class AddressRender implements ChillEntityRenderInterface
private function renderCountryLine($addr): ?string
{
return $addr->getPostCode()->getCountry()->getName();
return $this->translatableStringHelper->localize(
$addr->getPostCode()->getCountry()->getName()
);
}
private function renderDeliveryLine($addr): ?string
@@ -153,22 +166,26 @@ class AddressRender implements ChillEntityRenderInterface
$arr = [];
if ($addr->getFlat()) {
$arr[] = $addr->getFlat();
$arr[] = 'appart ' . $addr->getFlat();
}
if ($addr->getFloor()) {
$arr[] = $addr->getFloor();
}
if ($addr->getSteps()) {
$arr[] = $addr->getSteps();
$arr[] = 'ét ' . $addr->getFloor();
}
if ($addr->getCorridor()) {
$arr[] = $addr->getCorridor();
$arr[] = 'coul ' . $addr->getCorridor();
}
return implode(' - ', $arr);
if ($addr->getSteps()) {
$arr[] = 'esc ' . $addr->getSteps();
}
$res = implode(' - ', $arr);
if ('' === $res) {
$res = null;
}
return $res;
}
private function renderStreetLine($addr): string