Skip to content

Commit

Permalink
Issue Preventing Checkout with Configurable Products in Low Stock (#62)
Browse files Browse the repository at this point in the history
* #59 Configuration provider

* Formatting

* Write unit tests

* Fix unit tests

---------

Co-authored-by: Sushmita Thakur <[email protected]>
Co-authored-by: Khushboo <[email protected]>
Co-authored-by: Can Demiralp <[email protected]>
Co-authored-by: Can Demiralp <[email protected]>
  • Loading branch information
5 people authored Nov 8, 2024
1 parent e318ec2 commit 11e6f49
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 14 deletions.
30 changes: 30 additions & 0 deletions Model/CompositeConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Adyen\Hyva\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
class CompositeConfigProvider implements ConfigProviderInterface
{
/**
* @param array $configProviders
*/
public function __construct(
private readonly array $configProviders
) { }

/**
* @return array
*/
public function getConfig(): array
{
$config = [];

foreach ($this->configProviders as $configProvider) {
$config = array_merge_recursive($config, $configProvider->getConfig());
}

return $config;
}
}
5 changes: 2 additions & 3 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'])) {
Expand Down
65 changes: 65 additions & 0 deletions Test/Unit/Model/CompositeConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2024 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/

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);
}
}
19 changes: 8 additions & 11 deletions Test/Unit/Model/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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')
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@
</argument>
</arguments>
</type>

<type name="Adyen\Hyva\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="ccform" xsi:type="object">Magento\Payment\Model\CcGenericConfigProvider</item>
<item name="cc_card_config_provider" xsi:type="object">Magento\Payment\Model\CcConfigProvider</item>
<item name="tax_config_provider" xsi:type="object">Magento\Tax\Model\TaxConfigProvider</item>
<item name="vault_config_provider" xsi:type="object">Magento\Vault\Model\Ui\VaultConfigProvider</item>
<item name="vault_cards_config_provider" xsi:type="object">Magento\Vault\Model\Ui\TokensConfigProvider</item>
<item name="adyen_generic_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenGenericConfigProvider</item>
<item name="adyen_cc_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenCcConfigProvider</item>
<item name="adyen_pm_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenPmConfigProvider</item>
<item name="adyen_pos_cloud_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider</item>
<item name="adyen_giftcard_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenGiftcardConfigProvider</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 11e6f49

Please sign in to comment.