From 89d86550d1a85dc7d127455308b4c96bf53fe1c9 Mon Sep 17 00:00:00 2001 From: Thijs De Paepe Date: Mon, 25 Sep 2023 16:12:09 +0200 Subject: [PATCH] added: more formatter tests --- .../MultiValuePresenterFormatter.php | 11 +- .../ReplaceAttributeCodeFormatter.php | 2 +- .../BooleanAttributeFormatterTest.php | 42 ++++++ .../MetricAttributeFormatterTest.php | 56 ++++++++ .../MultiValuePresenterFormatterTest.php | 54 +++++++ .../PriceCollectionFormatterTest.php | 50 +++++++ .../ReplaceAttributeCodeFormatterTest.php | 133 ++++++++---------- 7 files changed, 271 insertions(+), 77 deletions(-) create mode 100644 tests/Component/AttributeFormatter/BooleanAttributeFormatterTest.php create mode 100644 tests/Component/AttributeFormatter/MetricAttributeFormatterTest.php create mode 100644 tests/Component/AttributeFormatter/MultiValuePresenterFormatterTest.php create mode 100644 tests/Component/AttributeFormatter/PriceCollectionFormatterTest.php diff --git a/src/Component/AttributeFormatter/MultiValuePresenterFormatter.php b/src/Component/AttributeFormatter/MultiValuePresenterFormatter.php index c07123b5..75bea600 100644 --- a/src/Component/AttributeFormatter/MultiValuePresenterFormatter.php +++ b/src/Component/AttributeFormatter/MultiValuePresenterFormatter.php @@ -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 ''; @@ -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; diff --git a/src/Component/AttributeFormatter/ReplaceAttributeCodeFormatter.php b/src/Component/AttributeFormatter/ReplaceAttributeCodeFormatter.php index 7dfd7838..3870120a 100644 --- a/src/Component/AttributeFormatter/ReplaceAttributeCodeFormatter.php +++ b/src/Component/AttributeFormatter/ReplaceAttributeCodeFormatter.php @@ -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]; diff --git a/tests/Component/AttributeFormatter/BooleanAttributeFormatterTest.php b/tests/Component/AttributeFormatter/BooleanAttributeFormatterTest.php new file mode 100644 index 00000000..1278cc8c --- /dev/null +++ b/tests/Component/AttributeFormatter/BooleanAttributeFormatterTest.php @@ -0,0 +1,42 @@ + [ + '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'); + } +} \ No newline at end of file diff --git a/tests/Component/AttributeFormatter/MetricAttributeFormatterTest.php b/tests/Component/AttributeFormatter/MetricAttributeFormatterTest.php new file mode 100644 index 00000000..13c49577 --- /dev/null +++ b/tests/Component/AttributeFormatter/MetricAttributeFormatterTest.php @@ -0,0 +1,56 @@ +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' + ); + } +} \ No newline at end of file diff --git a/tests/Component/AttributeFormatter/MultiValuePresenterFormatterTest.php b/tests/Component/AttributeFormatter/MultiValuePresenterFormatterTest.php new file mode 100644 index 00000000..7c4e0614 --- /dev/null +++ b/tests/Component/AttributeFormatter/MultiValuePresenterFormatterTest.php @@ -0,0 +1,54 @@ +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' + ); + } +} \ No newline at end of file diff --git a/tests/Component/AttributeFormatter/PriceCollectionFormatterTest.php b/tests/Component/AttributeFormatter/PriceCollectionFormatterTest.php new file mode 100644 index 00000000..5112302e --- /dev/null +++ b/tests/Component/AttributeFormatter/PriceCollectionFormatterTest.php @@ -0,0 +1,50 @@ +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' + ); + } +} \ No newline at end of file diff --git a/tests/Component/AttributeFormatter/ReplaceAttributeCodeFormatterTest.php b/tests/Component/AttributeFormatter/ReplaceAttributeCodeFormatterTest.php index 00bdd68f..79d51d8a 100644 --- a/tests/Component/AttributeFormatter/ReplaceAttributeCodeFormatterTest.php +++ b/tests/Component/AttributeFormatter/ReplaceAttributeCodeFormatterTest.php @@ -2,6 +2,9 @@ namespace Component\AttributeFormatter; +use Misery\Component\AttributeFormatter\AttributeValueFormatter; +use Misery\Component\AttributeFormatter\MultiValuePresenterFormatter; +use Misery\Component\AttributeFormatter\PropertyFormatterRegistry; use Misery\Component\AttributeFormatter\ReplaceAttributeCodeFormatter; use Misery\Component\Reader\ItemCollection; use Misery\Component\Source\Source; @@ -14,96 +17,84 @@ public function dataCollection() return new ItemCollection([ [ 'code' => 'red', + 'attribute' => 'color', 'labels-nl_BE' => 'Rood', - ] + ], + [ + 'code' => 'green', + 'attribute' => 'color', + 'labels-nl_BE' => 'Groen', + ], + [ + 'code' => 'blue', + 'attribute' => 'color', + 'labels-nl_BE' => 'Blauw', + ], ]); } -// public function dataProviderForFormat() -// { -// return [ -// // Test case 1: String input -// [ -// 'value' => 'Hello {value}', -// 'context' => [ -// 'separator' => '-', -// 'current-attribute-code' => 'attribute_code', -// ], -// 'expectedResult' => 'Hello attribute_code', -// ], -// // Test case 2: Array input -// [ -// 'value' => ['Item 1: {value}', 'Item 2: {value}'], -// 'context' => [ -// 'separator' => '-', -// 'current-attribute-code' => 'attribute_code', -// ], -// 'expectedResult' => ['Item 1: attribute_code', 'Item 2: attribute_code'], -// ], -// // Add more test cases here as needed -// ]; -// } - public function testFormatWithStringValue() { - $formatter = new ReplaceAttributeCodeFormatter( - Source::createSimple($this->dataCollection(), 'test') + $registry = new PropertyFormatterRegistry(); + $registry->addAll( + new ReplaceAttributeCodeFormatter( + Source::createSimple($this->dataCollection(), 'simple-source') + ), + new MultiValuePresenterFormatter(), ); + $formatter = new AttributeValueFormatter($registry); + $formatter->setAttributeTypesAndCodes([ + 'color' => 'pim_catalog_simpleselect', + ]); $context = [ - 'source' => 'test', + 'source' => 'simple-source', 'filter' => [ 'attribute' => '{attribute-code}', - 'code' => '{value}' + 'code' => '{value}', ], 'return' => 'labels-nl_BE', - // 'separator' => '-', - // 'current-attribute-code' => 'attribute_code', + 'current-attribute-code' => 'color', ]; $value = 'red'; $expectedResult = 'Rood'; - $formattedValue = $formatter->format($value, $context); + $formattedValue = $formatter->format('color', $value, $context); + + $this->assertEquals($expectedResult, $formattedValue); + } + + public function testFormatWithArrayValue() + { + $registry = new PropertyFormatterRegistry(); + $registry->addAll( + new ReplaceAttributeCodeFormatter( + Source::createSimple($this->dataCollection(), 'simple-source') + ), + new MultiValuePresenterFormatter(), + ); + $formatter = new AttributeValueFormatter($registry); + $formatter->setAttributeTypesAndCodes([ + 'color' => 'pim_catalog_multiselect', + ]); + + $context = [ + 'source' => 'simple-source', + 'filter' => [ + 'attribute' => '{attribute-code}', + 'code' => '{value}', + ], + 'value-separator' => '-', + 'return' => 'labels-nl_BE', + 'current-attribute-code' => 'color', + ]; + + $value = ['red', 'green']; + $expectedResult = 'Rood-Groen'; + + $formattedValue = $formatter->format('color', $value, $context); $this->assertEquals($expectedResult, $formattedValue); } -// -// public function testFormatWithArrayValue() -// { -// $source = new Source(); // You should create an instance of your Source class here -// $formatter = new ReplaceAttributeCodeFormatter($source); -// -// $context = [ -// 'separator' => '-', -// 'current-attribute-code' => 'attribute_code', -// ]; -// -// $value = ['Item 1: {value}', 'Item 2: {value}']; -// $expectedResult = ['Item 1: attribute_code', 'Item 2: attribute_code']; -// -// $formattedValue = $formatter->format($value, $context); -// -// $this->assertEquals($expectedResult, $formattedValue); -// } -// -// public function testFormatWithFilter() -// { -// $source = new Source(); // You should create an instance of your Source class here -// $formatter = new ReplaceAttributeCodeFormatter($source); -// -// $context = [ -// 'separator' => '-', -// 'current-attribute-code' => 'attribute_code', -// 'filter' => ['some_filter'], -// 'return' => 'some_field', -// ]; -// -// // Define your expected result based on your source data and filter -// $expectedResult = 'Expected Result'; -// -// $formattedValue = $formatter->format('Input Value', $context); -// -// $this->assertEquals($expectedResult, $formattedValue); -// } }