From 5b06d3e661a635c57064709579919d5515b157f1 Mon Sep 17 00:00:00 2001 From: Marjan Date: Mon, 7 Oct 2024 11:14:23 +0200 Subject: [PATCH 1/4] #59 Configuration provider --- Model/CompositeConfigProvider.php | 26 ++++++++++++++++++++++++++ Model/Configuration.php | 5 ++--- etc/di.xml | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Model/CompositeConfigProvider.php diff --git a/Model/CompositeConfigProvider.php b/Model/CompositeConfigProvider.php new file mode 100644 index 00000000..5ddf57bf --- /dev/null +++ b/Model/CompositeConfigProvider.php @@ -0,0 +1,26 @@ +configProviders = $configProviders; + } + + public function getConfig() + { + $config = []; + foreach ($this->configProviders as $configProvider) { + $config = array_merge_recursive($config, $configProvider->getConfig()); + } + return $config; + } +} diff --git a/Model/Configuration.php b/Model/Configuration.php index fd77cb68..e1f5e982 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -5,7 +5,6 @@ namespace Adyen\Hyva\Model; use Exception; -use Magento\Checkout\Model\CompositeConfigProvider; use Magento\Framework\DataObject; use Magento\Framework\DataObjectFactory; use Psr\Log\LoggerInterface; @@ -16,8 +15,8 @@ class Configuration public function __construct( CompositeConfigProvider $configProvider, - DataObjectFactory $dataObjectFactory, - LoggerInterface $logger + DataObjectFactory $dataObjectFactory, + LoggerInterface $logger ) { try { if (isset($configProvider->getConfig()['payment'])) { diff --git a/etc/di.xml b/etc/di.xml index ebec49a0..54bbc24b 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -42,4 +42,21 @@ + + + + + Magento\Payment\Model\CcGenericConfigProvider + Magento\Payment\Model\CcConfigProvider + Magento\Tax\Model\TaxConfigProvider + Magento\Vault\Model\Ui\VaultConfigProvider + Magento\Vault\Model\Ui\TokensConfigProvider + Adyen\Payment\Model\Ui\AdyenGenericConfigProvider + Adyen\Payment\Model\Ui\AdyenCcConfigProvider + Adyen\Payment\Model\Ui\AdyenPmConfigProvider + Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider + Adyen\Payment\Model\Ui\AdyenGiftcardConfigProvider + + + From b08e665e08ca3ed73a808fd8383b23906dda3024 Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Mon, 4 Nov 2024 15:37:47 +0100 Subject: [PATCH 2/4] Formatting --- Model/CompositeConfigProvider.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Model/CompositeConfigProvider.php b/Model/CompositeConfigProvider.php index 5ddf57bf..18361795 100644 --- a/Model/CompositeConfigProvider.php +++ b/Model/CompositeConfigProvider.php @@ -7,20 +7,24 @@ use Magento\Checkout\Model\ConfigProviderInterface; class CompositeConfigProvider implements ConfigProviderInterface { - private $configProviders; - + /** + * @param array $configProviders + */ public function __construct( - array $configProviders - ) { - $this->configProviders = $configProviders; - } + private readonly array $configProviders + ) { } - public function getConfig() + /** + * @return array + */ + public function getConfig(): array { $config = []; + foreach ($this->configProviders as $configProvider) { $config = array_merge_recursive($config, $configProvider->getConfig()); } + return $config; } } From 2a1713ba7b6a2bd164f2250404d1337f856213dc Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Mon, 4 Nov 2024 15:39:09 +0100 Subject: [PATCH 3/4] Write unit tests --- .../Model/CompositeConfigProviderTest.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Test/Unit/Model/CompositeConfigProviderTest.php diff --git a/Test/Unit/Model/CompositeConfigProviderTest.php b/Test/Unit/Model/CompositeConfigProviderTest.php new file mode 100644 index 00000000..4c60429d --- /dev/null +++ b/Test/Unit/Model/CompositeConfigProviderTest.php @@ -0,0 +1,65 @@ + + */ + +namespace Adyen\Hyva\Test\Unit\Model; + +use Adyen\Hyva\Model\CompositeConfigProvider; +use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; +use Magento\Checkout\Model\ConfigProviderInterface; +use PHPUnit\Framework\MockObject\MockObject; + +class CompositeConfigProviderTest extends AbstractAdyenTestCase +{ + protected ?CompositeConfigProvider $compositeConfigProvider; + protected MockObject|array $configProvidersMock; + protected MockObject|ConfigProviderInterface $singleConfigProviderMock; + + /** + * @return void + */ + public function setUp(): void + { + parent::setUp(); + + $this->singleConfigProviderMock = $this->createMock(ConfigProviderInterface::class); + $this->configProvidersMock[] = $this->singleConfigProviderMock; + + $this->compositeConfigProvider = new CompositeConfigProvider($this->configProvidersMock); + } + + /** + * @return void + */ + public function tearDown(): void + { + $this->compositeConfigProvider = null; + } + + /** + * Test case to assert all available config values provided through di.xml + * + * @return void + */ + public function testGetConfig() + { + $configMock = [ + 'config1' => 'value1', + 'config2' => 'value2' + ]; + + $this->singleConfigProviderMock->expects($this->once()) + ->method('getConfig') + ->willReturn($configMock); + + $result = $this->compositeConfigProvider->getConfig(); + $this->assertEquals($configMock, $result); + } +} From 76dc74ba7a8065723218b8eed0e8bf8fdf9bc6cf Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Wed, 6 Nov 2024 15:00:17 +0100 Subject: [PATCH 4/4] Fix unit tests --- Test/Unit/Model/ConfigurationTest.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Test/Unit/Model/ConfigurationTest.php b/Test/Unit/Model/ConfigurationTest.php index 74a0a15d..c38fab02 100644 --- a/Test/Unit/Model/ConfigurationTest.php +++ b/Test/Unit/Model/ConfigurationTest.php @@ -2,19 +2,19 @@ namespace Adyen\Hyva\Test\Unit\Model; +use Adyen\Hyva\Model\CompositeConfigProvider; use Adyen\Hyva\Model\Configuration; use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; -use Magento\Checkout\Model\CompositeConfigProvider; use Magento\Framework\DataObjectFactory; use Psr\Log\LoggerInterface; use PHPUnit\Framework\MockObject\MockObject; class ConfigurationTest extends AbstractAdyenTestCase { - private Configuration $configuration; - private CompositeConfigProvider|MockObject $configProvider; - private DataObjectFactory|MockObject $dataObjectFactory; - private LoggerInterface|MockObject $logger; + protected Configuration $configuration; + protected CompositeConfigProvider|MockObject $configProvider; + protected DataObjectFactory|MockObject $dataObjectFactory; + protected LoggerInterface|MockObject $logger; protected function setUp(): void { @@ -26,13 +26,9 @@ protected function setUp(): void ] ]; - $this->configProvider = $this->getMockBuilder(CompositeConfigProvider::class) - ->disableOriginalConstructor() - ->getMock(); + $this->configProvider = $this->createMock(CompositeConfigProvider::class); $this->dataObjectFactory = $this->createGeneratedMock(DataObjectFactory::class, ['create']); - $this->logger = $this->getMockBuilder(LoggerInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $this->logger = $this->createMock(LoggerInterface::class); $this->configProvider->expects($this->exactly(2)) ->method('getConfig') @@ -74,6 +70,7 @@ public function testGetJsonValue(): void $jsonValue = $this->configuration->getJsonValue('nonexistent/path'); $this->assertSame('null', $jsonValue); } + public function testIsCCEnableStoreDetails(): void { $result = $this->configuration->isCCEnableStoreDetails(false);