mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
address: add unit test for address render template
This commit is contained in:
parent
4c4a003977
commit
6bae7606dd
@ -12,6 +12,7 @@ declare(strict_types=1);
|
|||||||
namespace Chill\MainBundle\Templating\Entity;
|
namespace Chill\MainBundle\Templating\Entity;
|
||||||
|
|
||||||
use Chill\MainBundle\Entity\Address;
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||||
use Symfony\Component\Templating\EngineInterface;
|
use Symfony\Component\Templating\EngineInterface;
|
||||||
|
|
||||||
use function array_merge;
|
use function array_merge;
|
||||||
@ -30,10 +31,12 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
];
|
];
|
||||||
|
|
||||||
private EngineInterface $templating;
|
private EngineInterface $templating;
|
||||||
|
private TranslatableStringHelper $translatableStringHelper;
|
||||||
|
|
||||||
public function __construct(EngineInterface $templating)
|
public function __construct(EngineInterface $templating, TranslatableStringHelper $translatableStringHelper)
|
||||||
{
|
{
|
||||||
$this->templating = $templating;
|
$this->templating = $templating;
|
||||||
|
$this->translatableStringHelper = $translatableStringHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,9 +50,10 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
return $this->templating
|
return $this->templating
|
||||||
->render('@ChillMain/Entity/address.html.twig', [
|
->render('@ChillMain/Entity/address.html.twig', [
|
||||||
'address' => $addr,
|
'address' => $addr,
|
||||||
'streetLine' => $this->renderStreetLine($addr),
|
'streetLine' => $this->renderStreetLine($addr), // TODO inutile?
|
||||||
'render' => $options['render'] ?? 'bloc',
|
'render' => $options['render'] ?? 'bloc',
|
||||||
'options' => $options,
|
'options' => $options,
|
||||||
|
'lines' => $this->renderLines($addr),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,25 +66,23 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
public function renderLines($addr): array
|
public function renderLines($addr): array
|
||||||
{
|
{
|
||||||
$lines = [];
|
$lines = [];
|
||||||
|
if (null !== $addr->getPostCode()) {
|
||||||
$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 ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
|
if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
|
||||||
$lines[0] = $this->renderIntraBuildingLine($addr);
|
$lines[] = $this->renderIntraBuildingLine($addr);
|
||||||
$lines[1] = $this->renderBuildingLine($addr);
|
$lines[] = $this->renderBuildingLine($addr);
|
||||||
$lines[2] = $this->renderStreetLine($addr);
|
$lines[] = $this->renderStreetLine($addr);
|
||||||
$lines[3] = $this->renderDeliveryLine($addr);
|
$lines[] = $this->renderDeliveryLine($addr);
|
||||||
$lines[4] = $this->renderCityLine($addr);
|
$lines[] = $this->renderCityLine($addr);
|
||||||
$lines[5] = $this->renderCountryLine($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 array_values(array_filter($lines, fn ($l) => null !== $l));
|
||||||
return $lines;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -99,14 +101,23 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
|
|
||||||
private function renderBuildingLine($addr): ?string
|
private function renderBuildingLine($addr): ?string
|
||||||
{
|
{
|
||||||
$res = $addr->getBuildingName() . ' - ' . $this->renderIntraBuildingLine($addr);
|
|
||||||
|
|
||||||
if (null === $addr->getBuildingName()) {
|
if (!empty($addr->getBuildingName())) {
|
||||||
$res = $this->renderIntraBuildingLine($addr);
|
$building = $addr->getBuildingName();
|
||||||
|
} else {
|
||||||
|
$building = '';
|
||||||
|
}
|
||||||
|
|
||||||
if (null === $this->renderIntraBuildingLine($addr)) {
|
if (!empty($this->renderIntraBuildingLine($addr))) {
|
||||||
return null;
|
$intraBuilding = $this->renderIntraBuildingLine($addr);
|
||||||
}
|
} else {
|
||||||
|
$intraBuilding = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = trim($building . ' - ' . $intraBuilding, ' - ');
|
||||||
|
|
||||||
|
if ('' === $res) {
|
||||||
|
$res = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
|
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
|
||||||
@ -140,7 +151,9 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
|
|
||||||
private function renderCountryLine($addr): ?string
|
private function renderCountryLine($addr): ?string
|
||||||
{
|
{
|
||||||
return $addr->getPostCode()->getCountry()->getName();
|
return $this->translatableStringHelper->localize(
|
||||||
|
$addr->getPostCode()->getCountry()->getName()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function renderDeliveryLine($addr): ?string
|
private function renderDeliveryLine($addr): ?string
|
||||||
@ -153,22 +166,26 @@ class AddressRender implements ChillEntityRenderInterface
|
|||||||
$arr = [];
|
$arr = [];
|
||||||
|
|
||||||
if ($addr->getFlat()) {
|
if ($addr->getFlat()) {
|
||||||
$arr[] = $addr->getFlat();
|
$arr[] = 'appart ' . $addr->getFlat();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($addr->getFloor()) {
|
if ($addr->getFloor()) {
|
||||||
$arr[] = $addr->getFloor();
|
$arr[] = 'ét ' . $addr->getFloor();
|
||||||
}
|
|
||||||
|
|
||||||
if ($addr->getSteps()) {
|
|
||||||
$arr[] = $addr->getSteps();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($addr->getCorridor()) {
|
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
|
private function renderStreetLine($addr): string
|
||||||
|
@ -18,6 +18,7 @@ use Chill\MainBundle\Templating\Entity\AddressRender;
|
|||||||
use Iterator;
|
use Iterator;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
use Symfony\Component\Templating\EngineInterface;
|
use Symfony\Component\Templating\EngineInterface;
|
||||||
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -30,11 +31,11 @@ final class AddressRenderTest extends KernelTestCase
|
|||||||
self::bootKernel();
|
self::bootKernel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addressDataProvider(): Iterator
|
public function simpleAddressDataProviderBE(): Iterator
|
||||||
{
|
{
|
||||||
$addr = new Address();
|
$addr = new Address();
|
||||||
$country = (new Country())
|
$country = (new Country())
|
||||||
->setName(['fr' => 'Pays'])
|
->setName(['fr' => 'Belgium'])
|
||||||
->setCountryCode('BE');
|
->setCountryCode('BE');
|
||||||
$postCode = new PostalCode();
|
$postCode = new PostalCode();
|
||||||
$postCode->setName('Locality')
|
$postCode->setName('Locality')
|
||||||
@ -45,20 +46,137 @@ final class AddressRenderTest extends KernelTestCase
|
|||||||
->setStreetNumber('5')
|
->setStreetNumber('5')
|
||||||
->setPostcode($postCode);
|
->setPostcode($postCode);
|
||||||
|
|
||||||
yield [$addr, 'Rue ABC, 5 - 012345 Locality'];
|
yield [$addr, 'Rue ABC, 5 - 012345 Locality - Belgium'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function simpleAddressDataProviderFR(): Iterator
|
||||||
|
{
|
||||||
|
$addr = new Address();
|
||||||
|
$country = (new Country())
|
||||||
|
->setName(['fr' => 'France'])
|
||||||
|
->setCountryCode('FR');
|
||||||
|
$postCode = new PostalCode();
|
||||||
|
$postCode->setName('Locality')
|
||||||
|
->setCode('012345')
|
||||||
|
->setCountry($country);
|
||||||
|
|
||||||
|
$addr->setStreet('Rue ABC')
|
||||||
|
->setStreetNumber('5')
|
||||||
|
->setPostcode($postCode);
|
||||||
|
|
||||||
|
yield [$addr, '5, Rue ABC - 012345 Locality - France'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider addressDataProvider
|
* @dataProvider simpleAddressDataProviderBE
|
||||||
*/
|
*/
|
||||||
public function testRenderString(Address $addr, string $expectedString): void
|
public function testRenderStringSimpleAddressBE(Address $addr, string $expectedString): void
|
||||||
{
|
{
|
||||||
$engine = self::$container->get(EngineInterface::class);
|
$engine = self::$container->get(EngineInterface::class);
|
||||||
$renderer = new AddressRender($engine);
|
$translatableStringHelper = self::$container->get(TranslatableStringHelper::class);
|
||||||
|
$renderer = new AddressRender($engine, $translatableStringHelper);
|
||||||
|
|
||||||
$this->assertEquals($expectedString, $renderer->renderString($addr, []));
|
$this->assertEquals($expectedString, $renderer->renderString($addr, []));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
$this->assertIsString($renderer->renderBox($addr, []));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider simpleAddressDataProviderFR
|
||||||
|
*/
|
||||||
|
public function testRenderStringSimpleAddressFR(Address $addr, string $expectedString): void
|
||||||
|
{
|
||||||
|
$engine = self::$container->get(EngineInterface::class);
|
||||||
|
$translatableStringHelper = self::$container->get(TranslatableStringHelper::class);
|
||||||
|
$renderer = new AddressRender($engine, $translatableStringHelper);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedString, $renderer->renderString($addr, []));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function complexAddressDataProviderBE(): Iterator
|
||||||
|
{
|
||||||
|
$addr = new Address();
|
||||||
|
$country = (new Country())
|
||||||
|
->setName(['fr' => 'Belgium'])
|
||||||
|
->setCountryCode('BE');
|
||||||
|
$postCode = new PostalCode();
|
||||||
|
$postCode->setName('Locality')
|
||||||
|
->setCode('012345')
|
||||||
|
->setCountry($country);
|
||||||
|
|
||||||
|
$addr->setStreet('Rue ABC')
|
||||||
|
->setStreetNumber('5')
|
||||||
|
->setPostcode($postCode);
|
||||||
|
|
||||||
|
$addr->setBuildingName('Résidence "Les Bleuets"');
|
||||||
|
$addr->setFlat('1');
|
||||||
|
$addr->setFloor('2');
|
||||||
|
$addr->setCorridor('3');
|
||||||
|
$addr->setSteps('4');
|
||||||
|
|
||||||
|
|
||||||
|
yield [$addr, 'Résidence "Les Bleuets" - appart 1 - ét 2 - coul 3 - esc 4 - Rue ABC, 5 - 012345 Locality - Belgium'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function complexAddressDataProviderFR(): Iterator
|
||||||
|
{
|
||||||
|
$addr = new Address();
|
||||||
|
$country = (new Country())
|
||||||
|
->setName(['fr' => 'France'])
|
||||||
|
->setCountryCode('FR');
|
||||||
|
$postCode = new PostalCode();
|
||||||
|
$postCode->setName('Locality')
|
||||||
|
->setCode('012345')
|
||||||
|
->setCountry($country);
|
||||||
|
|
||||||
|
$addr->setStreet('Rue ABC')
|
||||||
|
->setStreetNumber('5')
|
||||||
|
->setPostcode($postCode);
|
||||||
|
|
||||||
|
$addr->setBuildingName('Résidence "Les Bleuets"');
|
||||||
|
$addr->setFlat('1');
|
||||||
|
$addr->setFloor('2');
|
||||||
|
$addr->setCorridor('3');
|
||||||
|
$addr->setSteps('4');
|
||||||
|
|
||||||
|
|
||||||
|
yield [$addr, 'appart 1 - ét 2 - coul 3 - esc 4 - Résidence "Les Bleuets" - 5, Rue ABC - 012345 Locality - France'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider complexAddressDataProviderBE
|
||||||
|
*/
|
||||||
|
public function testRenderComplexAddressBE(Address $addr, string $expectedString): void
|
||||||
|
{
|
||||||
|
$engine = self::$container->get(EngineInterface::class);
|
||||||
|
$translatableStringHelper = self::$container->get(TranslatableStringHelper::class);
|
||||||
|
$renderer = new AddressRender($engine, $translatableStringHelper);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedString, $renderer->renderString($addr, []));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider complexAddressDataProviderFR
|
||||||
|
*/
|
||||||
|
public function testRenderComplexAddressFR(Address $addr, string $expectedString): void
|
||||||
|
{
|
||||||
|
$engine = self::$container->get(EngineInterface::class);
|
||||||
|
$translatableStringHelper = self::$container->get(TranslatableStringHelper::class);
|
||||||
|
$renderer = new AddressRender($engine, $translatableStringHelper);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedString, $renderer->renderString($addr, []));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user