From dd4c003174538a2662b7ed47b0502fc08fbde910 Mon Sep 17 00:00:00 2001 From: vasilvestre Date: Mon, 10 Oct 2022 18:53:23 +0200 Subject: [PATCH 1/8] Enable phpspec --- spec/Exporter/ResourceExporterSpec.php | 2 +- .../Exporter/Transformer/Handler/ArrayToStringHandlerSpec.php | 2 +- .../Transformer/Handler/DateTimeToStringHandlerSpec.php | 2 +- .../Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php | 2 +- spec/Exporter/Transformer/PoolSpec.php | 4 ++-- spec/Importer/JsonResourceImporterSpec.php | 2 +- spec/Importer/ResourceImporterSpec.php | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/Exporter/ResourceExporterSpec.php b/spec/Exporter/ResourceExporterSpec.php index 256d5060..f9b97c93 100644 --- a/spec/Exporter/ResourceExporterSpec.php +++ b/spec/Exporter/ResourceExporterSpec.php @@ -40,7 +40,7 @@ function it_should_export_but_also_transform( PluginInterface $plugin ) { $generator = new RewindableGenerator(function () { - return [new DateTimeToStringHandler()]; + yield new DateTimeToStringHandler(); }, $count = 1); $pool = new Pool($generator); diff --git a/spec/Exporter/Transformer/Handler/ArrayToStringHandlerSpec.php b/spec/Exporter/Transformer/Handler/ArrayToStringHandlerSpec.php index e02d2f4a..759a4348 100644 --- a/spec/Exporter/Transformer/Handler/ArrayToStringHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/ArrayToStringHandlerSpec.php @@ -41,7 +41,7 @@ function it_should_process_via_pool() $array = ['a', 'b', 'c']; $generator = new RewindableGenerator(function () { - return [$this->getWrappedObject()]; + yield $this->getWrappedObject(); }, $count = 1); $pool = new Pool($generator); diff --git a/spec/Exporter/Transformer/Handler/DateTimeToStringHandlerSpec.php b/spec/Exporter/Transformer/Handler/DateTimeToStringHandlerSpec.php index bf3b693e..881011ac 100644 --- a/spec/Exporter/Transformer/Handler/DateTimeToStringHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/DateTimeToStringHandlerSpec.php @@ -41,7 +41,7 @@ function it_should_process_via_pool() $date = \DateTime::createFromFormat('Y-m-d', '2018-01-01'); $generator = new RewindableGenerator(function () { - return [$this->getWrappedObject()]; + yield $this->getWrappedObject(); }, $count = 1); $pool = new Pool($generator); diff --git a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php index 68bea55d..26864633 100644 --- a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php @@ -43,7 +43,7 @@ function it_should_process_directly() function it_should_process_via_pool() { $generator = new RewindableGenerator(function () { - return [$this->getWrappedObject()]; + yield $this->getWrappedObject(); }, $count = 1); $pool = new Pool($generator); diff --git a/spec/Exporter/Transformer/PoolSpec.php b/spec/Exporter/Transformer/PoolSpec.php index e720ff93..2dac51b2 100644 --- a/spec/Exporter/Transformer/PoolSpec.php +++ b/spec/Exporter/Transformer/PoolSpec.php @@ -24,8 +24,8 @@ function it_should_implement_interface() function let(HandlerInterface $dateTimeToStringHandler) { - $generator = new RewindableGenerator(function () use ($dateTimeToStringHandler) { - return [$dateTimeToStringHandler]; + $generator = new RewindableGenerator(function () use ($dateTimeToStringHandler): \Traversable { + yield $dateTimeToStringHandler; }, $count = 1); $this->beConstructedWith($generator); diff --git a/spec/Importer/JsonResourceImporterSpec.php b/spec/Importer/JsonResourceImporterSpec.php index 5d826119..61e9e66c 100644 --- a/spec/Importer/JsonResourceImporterSpec.php +++ b/spec/Importer/JsonResourceImporterSpec.php @@ -4,7 +4,7 @@ namespace spec\FriendsOfSylius\SyliusImportExportPlugin\Importer; -use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectManager; use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImporterResultInterface; use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImportResultLoggerInterface; use FriendsOfSylius\SyliusImportExportPlugin\Importer\JsonResourceImporter; diff --git a/spec/Importer/ResourceImporterSpec.php b/spec/Importer/ResourceImporterSpec.php index 0ebc28f4..3b3f9428 100644 --- a/spec/Importer/ResourceImporterSpec.php +++ b/spec/Importer/ResourceImporterSpec.php @@ -4,7 +4,7 @@ namespace spec\FriendsOfSylius\SyliusImportExportPlugin\Importer; -use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectManager; use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImporterInterface; use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImporterResultInterface; use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImportResultLoggerInterface; From 40378d60a9370680137a59fd673c1c81af4c4a26 Mon Sep 17 00:00:00 2001 From: vasilvestre Date: Mon, 10 Oct 2022 19:19:39 +0200 Subject: [PATCH 2/8] PHPSpec does work --- .../Handler/IntegerToMoneyFormatHandlerSpec.php | 10 +++++++--- .../Handler/IntegerToMoneyFormatHandler.php | 13 ++++++++----- src/Resources/config/services.yml | 1 + 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php index 26864633..440340be 100644 --- a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php @@ -9,6 +9,8 @@ use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\HandlerInterface; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Pool; use PhpSpec\ObjectBehavior; +use Prophecy\Prophet; +use Sylius\Component\Locale\Context\LocaleContextInterface; use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; use Webmozart\Assert\Assert; @@ -16,7 +18,9 @@ class IntegerToMoneyFormatHandlerSpec extends ObjectBehavior { function let() { - $this->beConstructedWith(['test']); + $contextLocaleInterface = (new Prophet)->prophesize(LocaleContextInterface::class); + $this->beConstructedWith(['test'], ($contextLocaleInterface)->reveal()); + $contextLocaleInterface->getLocaleCode()->willReturn('en'); } function it_is_initializable() @@ -37,7 +41,7 @@ function it_should_implement() function it_should_process_directly() { $this->handle('test', 10000)->shouldBeString(); - $this->handle('test', 12345)->shouldBe('123.45'); + $this->handle('test', 12345)->shouldBe('€123.45'); } function it_should_process_via_pool() @@ -50,6 +54,6 @@ function it_should_process_via_pool() $result = $pool->handle('test', 12345); - Assert::same('123.45', $result); + Assert::same('€123.45', $result); } } diff --git a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php index 280ac674..08eb483f 100644 --- a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php +++ b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php @@ -5,22 +5,25 @@ namespace FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler; +use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatter; +use Sylius\Component\Locale\Context\LocaleContextInterface; final class IntegerToMoneyFormatHandler extends Handler { /** @var array */ private $keys; - /** @var string */ - private $format; + /** @var LocaleContextInterface */ + private $localeContext; /** * @param string[] $allowedKeys + * @param LocaleContextInterface $localeContext */ - public function __construct(array $allowedKeys, string $format = '%.2n') + public function __construct(array $allowedKeys, $localeContext) { $this->keys = $allowedKeys; - $this->format = $format; + $this->localeContext = $localeContext; } /** @@ -28,7 +31,7 @@ public function __construct(array $allowedKeys, string $format = '%.2n') */ protected function process($key, $value): ?string { - return money_format($this->format, $value / 100); + return (new MoneyFormatter())->format($value, "EUR", $this->localeContext->getLocaleCode()); } protected function allows($key, $value): bool diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 5c703673..efdb0535 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -96,6 +96,7 @@ services: class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler\IntegerToMoneyFormatHandler arguments: - ['Total'] + - "@sylius.context.locale" tags: - { name: sylius.exporter_transformer } From 3625d7b5d03f37900f8c80d1880f470eba6ed732 Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:13:32 +0200 Subject: [PATCH 3/8] Try number_format --- src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php index 08eb483f..437427a2 100644 --- a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php +++ b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php @@ -31,6 +31,7 @@ public function __construct(array $allowedKeys, $localeContext) */ protected function process($key, $value): ?string { + return number_format($value, 2, '.', ''); return (new MoneyFormatter())->format($value, "EUR", $this->localeContext->getLocaleCode()); } From 85845d9e97d021c2d31c0e1c1f19461c9c071977 Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:13:57 +0200 Subject: [PATCH 4/8] Update IntegerToMoneyFormatHandlerSpec.php --- .../Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php index 440340be..8a436de4 100644 --- a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php @@ -41,7 +41,7 @@ function it_should_implement() function it_should_process_directly() { $this->handle('test', 10000)->shouldBeString(); - $this->handle('test', 12345)->shouldBe('€123.45'); + $this->handle('test', 12345)->shouldBe('123.45'); } function it_should_process_via_pool() @@ -54,6 +54,6 @@ function it_should_process_via_pool() $result = $pool->handle('test', 12345); - Assert::same('€123.45', $result); + Assert::same('123.45', $result); } } From d1b596ed00823d3d0a8977f4c75b8454a23371fd Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:33:13 +0200 Subject: [PATCH 5/8] -- --- .../Handler/IntegerToMoneyFormatHandlerSpec.php | 6 ++---- .../Handler/IntegerToMoneyFormatHandler.php | 14 +++++--------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php index 8a436de4..297ba1c0 100644 --- a/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php +++ b/spec/Exporter/Transformer/Handler/IntegerToMoneyFormatHandlerSpec.php @@ -10,7 +10,6 @@ use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Pool; use PhpSpec\ObjectBehavior; use Prophecy\Prophet; -use Sylius\Component\Locale\Context\LocaleContextInterface; use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; use Webmozart\Assert\Assert; @@ -18,9 +17,7 @@ class IntegerToMoneyFormatHandlerSpec extends ObjectBehavior { function let() { - $contextLocaleInterface = (new Prophet)->prophesize(LocaleContextInterface::class); - $this->beConstructedWith(['test'], ($contextLocaleInterface)->reveal()); - $contextLocaleInterface->getLocaleCode()->willReturn('en'); + $this->beConstructedWith(['test']); } function it_is_initializable() @@ -42,6 +39,7 @@ function it_should_process_directly() { $this->handle('test', 10000)->shouldBeString(); $this->handle('test', 12345)->shouldBe('123.45'); + $this->handle('test', 987654)->shouldBe('9876.54'); } function it_should_process_via_pool() diff --git a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php index 437427a2..9589fa7b 100644 --- a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php +++ b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php @@ -5,25 +5,22 @@ namespace FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler; use FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler; -use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatter; -use Sylius\Component\Locale\Context\LocaleContextInterface; final class IntegerToMoneyFormatHandler extends Handler { /** @var array */ private $keys; - /** @var LocaleContextInterface */ - private $localeContext; + private $format; /** * @param string[] $allowedKeys - * @param LocaleContextInterface $localeContext + * @param string $format */ - public function __construct(array $allowedKeys, $localeContext) + public function __construct(array $allowedKeys, string $format = '%0.2f') { $this->keys = $allowedKeys; - $this->localeContext = $localeContext; + $this->format = $format; } /** @@ -31,8 +28,7 @@ public function __construct(array $allowedKeys, $localeContext) */ protected function process($key, $value): ?string { - return number_format($value, 2, '.', ''); - return (new MoneyFormatter())->format($value, "EUR", $this->localeContext->getLocaleCode()); + return sprintf($this->format, $value/100); } protected function allows($key, $value): bool From 54ef7dfa325373e60d530d7ba6797233f8f5f935 Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:36:05 +0200 Subject: [PATCH 6/8] Update IntegerToMoneyFormatHandler.php --- .../Transformer/Handler/IntegerToMoneyFormatHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php index 9589fa7b..52283de1 100644 --- a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php +++ b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php @@ -28,7 +28,7 @@ public function __construct(array $allowedKeys, string $format = '%0.2f') */ protected function process($key, $value): ?string { - return sprintf($this->format, $value/100); + return sprintf($this->format, $value / 100); } protected function allows($key, $value): bool From c6804a91aface51a2838cc289d3cdc412aa9533d Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:36:29 +0200 Subject: [PATCH 7/8] Update IntegerToMoneyFormatHandler.php --- .../Transformer/Handler/IntegerToMoneyFormatHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php index 52283de1..eb11597e 100644 --- a/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php +++ b/src/Exporter/Transformer/Handler/IntegerToMoneyFormatHandler.php @@ -11,11 +11,11 @@ final class IntegerToMoneyFormatHandler extends Handler /** @var array */ private $keys; + /** @var string */ private $format; /** * @param string[] $allowedKeys - * @param string $format */ public function __construct(array $allowedKeys, string $format = '%0.2f') { From b3e31fffb98e70f99d268fcde2be978a85395d6d Mon Sep 17 00:00:00 2001 From: Valentin Silvestre Date: Wed, 12 Oct 2022 09:36:47 +0200 Subject: [PATCH 8/8] Update services.yml --- src/Resources/config/services.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index efdb0535..5c703673 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -96,7 +96,6 @@ services: class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Transformer\Handler\IntegerToMoneyFormatHandler arguments: - ['Total'] - - "@sylius.context.locale" tags: - { name: sylius.exporter_transformer }