Skip to content

Commit

Permalink
added: more formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Sep 25, 2023
1 parent b6c37f4 commit 89d8655
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MultiValuePresenterFormatter implements PropertyFormatterInterface, Requir
*/
public function format($value, array $context = [])
{
$format = $value['format'] ?? $context['format'] ?? null;
$valueSeparator = $context['value-separator'] ?? ', '; # best fallback option
if ($context['current-attribute-type'] === 'pim_catalog_multiselect' && empty($value)) {
return '';
Expand All @@ -24,16 +25,16 @@ public function format($value, array $context = [])
return $value;
}

if (is_array($value) && isset($value['format'])) {
return ValueFormatter::format($value['format'], $value);
if (is_array($value) && $format) {
return ValueFormatter::format($format, $value);
}

if (is_array($value) && !isset($value['format']) && isset($valueSeparator)) {
if (is_array($value) && !$format && isset($valueSeparator)) {
$value = implode($valueSeparator, $value);
}

if (is_string($value) && isset($context['format'])) {
return ValueFormatter::format($context['format'], ['value' => $value]);
if (is_string($value) && $format) {
return ValueFormatter::format($format, ['value' => $value]);
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ReplaceAttributeCodeFormatter implements PropertyFormatterInterface, Requi
],
];

public function __construct(Source $source, string $supportedType)
public function __construct(Source $source, string $supportedType = 'pim_catalog')
{
$this->source = $source;
$this->supportedTypes = $this->supportedTypes[$supportedType];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Misery\Component\AttributeFormatter;

use Induxx\Twig\Extension\TranslationBundleExtension;
use Misery\Component\AttributeFormatter\BooleanAttributeFormatter;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
class BooleanAttributeFormatterTest extends TestCase
{
use ProphecyTrait;

public function test_it_should_boolean_type_a_value(): void
{
$format = new BooleanAttributeFormatter();

$context = [
'label' => [
'Y' => 'YES',
'N' => 'NO',
],
'current-attribute-type' => 'pim_catalog_boolean',
];

$this->assertSame($format->format(true, $context), 'YES');
$this->assertSame($format->format(false, $context), 'NO');

$this->assertTrue($format->requires($context));
$this->assertTrue($format->supports('pim_catalog_boolean'));

$context = [
'label' => [
'Y' => 'Ja',
'N' => 'Nee',
],
'current-attribute-type' => 'pim_catalog_boolean',
];

$this->assertSame($format->format(true, $context), 'Ja');
$this->assertSame($format->format(false, $context), 'Nee');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Misery\Component\AttributeFormatter;

use Misery\Component\AttributeFormatter\AttributeValueFormatter;
use Misery\Component\AttributeFormatter\MetricAttributeFormatter;
use Misery\Component\AttributeFormatter\MultiValuePresenterFormatter;
use Misery\Component\AttributeFormatter\PropertyFormatterRegistry;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class MetricAttributeFormatterTest extends TestCase
{
use ProphecyTrait;

public function test_it_should_metric_type_a_value(): void
{
$registry = new PropertyFormatterRegistry();
$registry->add(new MetricAttributeFormatter());
$registry->add(new MultiValuePresenterFormatter());
$attributeValueFormatter = new AttributeValueFormatter($registry);
$attributeValueFormatter->setAttributeTypesAndCodes([
'weight' => 'pim_catalog_metric',
]);

$value = ['amount' => '1', 'unit' => 'GRAM'];

$context = [
'pim_catalog_metric' => [
'format' => '%amount% %unit%',
]
];

$this->assertSame(
$attributeValueFormatter->format('weight', $value, $context),
'1 GRAM'
);

$context = ['map' => ['GRAM' => 'gr']];

$this->assertSame(
$attributeValueFormatter->format('weight', $value, $context),
'1 gr'
);

$this->assertSame(
$attributeValueFormatter->format('weight', $value, $context+['metric-display-unit' => false]),
'1'
);

$this->assertSame(
$attributeValueFormatter->format('weight', $value, $context+['metric-separator' => '']),
'1gr'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Misery\Component\AttributeFormatter;

use Misery\Component\AttributeFormatter\AttributeValueFormatter;
use Misery\Component\AttributeFormatter\MetricAttributeFormatter;
use Misery\Component\AttributeFormatter\MultiValuePresenterFormatter;
use Misery\Component\AttributeFormatter\PriceCollectionFormatter;
use Misery\Component\AttributeFormatter\PropertyFormatterRegistry;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class MultiValuePresenterFormatterTest extends TestCase
{
use ProphecyTrait;

public function test_it_should_price_collection_type_a_value(): void
{
$registry = new PropertyFormatterRegistry();
$registry->add(new MultiValuePresenterFormatter());
$attributeValueFormatter = new AttributeValueFormatter($registry);
$attributeValueFormatter->setAttributeTypesAndCodes([
'list' => 'pim_catalog_simpleselect'
]);

$value = ['a', 'b', 'c'];

$context = ['value-separator' => ','];

$this->assertSame(
$attributeValueFormatter->format('list', $value, $context),
'a,b,c'
);

$context = []; # default seperator ', '

$this->assertSame(
$attributeValueFormatter->format('list', $value, $context),
'a, b, c'
);

$value = ['unit' => 'GRAM', 'amount' => 100, 'dosis' => 'per day'];


$context = [
'format' => '%amount% %unit% %dosis%',
];

$this->assertSame(
$attributeValueFormatter->format('list', $value, $context),
'100 GRAM per day'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Misery\Component\AttributeFormatter;

use Misery\Component\AttributeFormatter\AttributeValueFormatter;
use Misery\Component\AttributeFormatter\MetricAttributeFormatter;
use Misery\Component\AttributeFormatter\MultiValuePresenterFormatter;
use Misery\Component\AttributeFormatter\PriceCollectionFormatter;
use Misery\Component\AttributeFormatter\PropertyFormatterRegistry;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class PriceCollectionFormatterTest extends TestCase
{
use ProphecyTrait;

public function test_it_should_price_collection_type_a_value(): void
{
$registry = new PropertyFormatterRegistry();
$registry->add(new PriceCollectionFormatter());
$registry->add(new MultiValuePresenterFormatter());
$attributeValueFormatter = new AttributeValueFormatter($registry);

$attributeValueFormatter->setAttributeTypesAndCodes([
'price' => 'pim_catalog_price_collection'
]);

$value = [
['amount' => '1.0000', 'currency' => 'EUR'],
['amount' => '2.0000', 'currency' => 'US']
];

$context = ['currency' => 'EUR'];

$this->assertSame(
$attributeValueFormatter->format('price', $value, $context),
'1 EUR'
);

$this->assertSame(
$attributeValueFormatter->format('price', $value, $context+['format' => '%currency% %amount%']),
'EUR 1'
);

$this->assertSame(
$attributeValueFormatter->format('price', $value, $context+['format' => '%currency% %amount%', 'dec' => 2, 'dec_point' => ',']),
'EUR 1,00'
);
}
}
Loading

0 comments on commit 89d8655

Please sign in to comment.