Skip to content

Commit 2ff0636

Browse files
author
Can Demiralp
committed
[EPC-9489] Write unit tests
1 parent 472d8a5 commit 2ff0636

File tree

2 files changed

+119
-38
lines changed

2 files changed

+119
-38
lines changed

Model/Config/Backend/DonationAmounts.php

+21-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Adyen\Payment\Model\Config\Backend;
1313

14-
use Adyen\Payment\Helper\Data;
1514
use Magento\Framework\App\Cache\TypeListInterface;
1615
use Magento\Framework\App\Config\ScopeConfigInterface;
1716
use Magento\Framework\App\Config\Value;
@@ -20,6 +19,7 @@
2019
use Magento\Framework\Model\ResourceModel\AbstractResource;
2120
use Magento\Framework\Phrase;
2221
use Magento\Framework\Registry;
22+
use Magento\Framework\Validator\Exception;
2323
use Magento\Store\Model\StoreManagerInterface;
2424

2525
/**
@@ -29,36 +29,39 @@
2929
class DonationAmounts extends Value
3030
{
3131
/**
32-
* @var Data
32+
* @param Context $context
33+
* @param Registry $registry
34+
* @param ScopeConfigInterface $config
35+
* @param TypeListInterface $cacheTypeList
36+
* @param StoreManagerInterface $storeManager
37+
* @param AbstractResource|null $resource
38+
* @param AbstractDb|null $resourceCollection
39+
* @param array $data
3340
*/
34-
protected $_adyenHelper;
35-
36-
/**
37-
* @var StoreManagerInterface
38-
*/
39-
protected $storeManager;
40-
4141
public function __construct(
4242
Context $context,
4343
Registry $registry,
4444
ScopeConfigInterface $config,
4545
TypeListInterface $cacheTypeList,
46-
StoreManagerInterface $storeManager,
46+
protected readonly StoreManagerInterface $storeManager,
4747
AbstractResource $resource = null,
4848
AbstractDb $resourceCollection = null,
4949
array $data = []
5050
) {
51-
$this->storeManager = $storeManager;
5251
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
5352
}
5453

54+
/**
55+
* @return $this
56+
* @throws Exception
57+
*/
5558
public function validateBeforeSave(): DonationAmounts
5659
{
5760
if (
58-
(bool)$this->getFieldsetDataValue('active') &&
61+
$this->getFieldsetDataValue('active') &&
5962
!$this->validateDonationAmounts(explode(',', $this->getValue()))
6063
) {
61-
throw new \Magento\Framework\Validator\Exception(
64+
throw new Exception(
6265
new Phrase(
6366
'The Adyen Giving donation amounts are not valid, please enter amounts higher than zero separated by commas.'
6467
)
@@ -68,8 +71,11 @@ public function validateBeforeSave(): DonationAmounts
6871
return $this;
6972
}
7073

71-
72-
public function validateDonationAmounts($amounts = array())
74+
/**
75+
* @param array $amounts
76+
* @return bool
77+
*/
78+
public function validateDonationAmounts(array $amounts = []): bool
7379
{
7480
// Fail if the field is empty, the array is associative
7581
if (empty($amounts) || array_values($amounts) !== $amounts) {

Test/Unit/Model/Config/Backend/DonationAmountsTest.php

+98-23
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,125 @@
1212
namespace Adyen\Payment\Test\Unit\Model\Config\Backend;
1313

1414
use Adyen\Payment\Model\Config\Backend\DonationAmounts;
15+
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
16+
use Magento\Directory\Model\Currency;
17+
use Magento\Framework\App\Cache\TypeListInterface;
18+
use Magento\Framework\App\Config\ScopeConfigInterface;
19+
use Magento\Framework\Data\Collection\AbstractDb;
20+
use Magento\Framework\Model\Context;
21+
use Magento\Framework\Model\ResourceModel\AbstractResource;
22+
use Magento\Framework\Registry;
23+
use Magento\Framework\Validator\Exception;
24+
use Magento\Store\Model\Store;
25+
use Magento\Store\Model\StoreManagerInterface;
26+
use PHPUnit\Framework\MockObject\MockObject;
1527

16-
class DonationAmountsTest extends \PHPUnit\Framework\TestCase
28+
class DonationAmountsTest extends AbstractAdyenTestCase
1729
{
30+
protected ?DonationAmounts $donationAmounts;
31+
protected Context|MockObject $contextMock;
32+
protected Registry|MockObject $registryMock;
33+
protected ScopeConfigInterface|MockObject $scopeConfigMock;
34+
protected TypeListInterface|MockObject $typeListMock;
35+
protected AbstractResource|MockObject $abstractResourceMock;
36+
protected AbstractDb|MockObject $abstractDbMock;
37+
protected StoreManagerInterface|MockObject $storeManagerMock;
38+
protected array $data = [];
1839

19-
private $donationAmounts;
40+
/**
41+
* @return void
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->contextMock = $this->createMock(Context::class);
46+
$this->registryMock = $this->createMock(Registry::class);
47+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
48+
$this->typeListMock = $this->createMock(TypeListInterface::class);
49+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
50+
$this->abstractResourceMock = $this->createMock(AbstractResource::class);
51+
$this->abstractDbMock = $this->createMock(AbstractDb::class);
52+
53+
$currency = $this->createMock(Currency::class);
54+
$currency->method('getRate')->willReturn(1);
55+
$storeMock = $this->createMock(Store::class);
56+
$storeMock->method('getBaseCurrency')->willReturn($currency);
57+
$this->storeManagerMock->method('getStore')->willReturn($storeMock);
58+
}
2059

21-
private function getSimpleMock($originalClassName, $methods = [])
60+
/**
61+
* This method is required as `data` property needs to be adjusted independently after setUp() method runs.
62+
*
63+
* @return void
64+
*/
65+
private function generateSutClass(): void
2266
{
23-
return $this->getMockBuilder($originalClassName)
24-
->disableOriginalConstructor()
25-
->setMethods($methods)
26-
->getMock();
67+
$this->donationAmounts = new DonationAmounts(
68+
$this->contextMock,
69+
$this->registryMock,
70+
$this->scopeConfigMock,
71+
$this->typeListMock,
72+
$this->storeManagerMock,
73+
$this->abstractResourceMock,
74+
$this->abstractDbMock,
75+
$this->data
76+
);
2777
}
2878

29-
protected function setUp(): void
79+
/**
80+
* @return void
81+
* @throws Exception
82+
*/
83+
public function testValidateBeforeSaveSuccess()
3084
{
31-
$context = $this->getSimpleMock(\Magento\Framework\Model\Context::class);
32-
$registry = $this->getSimpleMock(\Magento\Framework\Registry::class);
33-
$scopeConfigInterface = $this->getSimpleMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
34-
$typeListInterface = $this->getSimpleMock(\Magento\Framework\App\Cache\TypeListInterface::class);
35-
$abstractResource = $this->getSimpleMock(\Magento\Framework\Model\ResourceModel\AbstractResource::class);
36-
$abstractDb = $this->getSimpleMock(\Magento\Framework\Data\Collection\AbstractDb::class);
37-
$storeManagerInterface = $this->getSimpleMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
38-
$storeInterface = $this->getSimpleMock(\Magento\Store\Model\Store::class, ['getBaseCurrency']);
39-
$currency = $this->getSimpleMock(\Magento\Directory\Model\Currency::class, ['getRate']);
40-
$currency->method('getRate')->willReturn(1);
41-
$storeInterface->method('getBaseCurrency')->willReturn($currency);
42-
$storeManagerInterface->method('getStore')->willReturn($storeInterface);
85+
$this->data = [
86+
'fieldset_data' => [
87+
'active' => true,
88+
89+
],
90+
'value' => '1,5,10'
91+
];
4392

93+
$this->generateSutClass();
4494

45-
$this->donationAmounts = new DonationAmounts($context, $registry, $scopeConfigInterface, $typeListInterface,
46-
$storeManagerInterface, $abstractResource, $abstractDb);
95+
$result = $this->donationAmounts->validateBeforeSave();
96+
$this->assertInstanceOf(DonationAmounts::class, $result);
97+
}
98+
99+
/**
100+
* @return void
101+
* @throws Exception
102+
*/
103+
public function testValidateBeforeSaveFail()
104+
{
105+
$this->data = [
106+
'fieldset_data' => [
107+
'active' => true,
108+
109+
],
110+
'value' => ''
111+
];
112+
113+
$this->generateSutClass();
114+
115+
$this->expectException(Exception::class);
116+
$this->donationAmounts->validateBeforeSave();
47117
}
48118

49119
/**
50120
* @dataProvider donationAmountsProvider
51121
*/
52122
public function testValidateDonationAmounts($donationAmounts, $expectedResult)
53123
{
124+
$this->generateSutClass();
125+
54126
$result = $this->donationAmounts->validateDonationAmounts(explode(',', (string) $donationAmounts));
55127
$this->assertEquals($result, $expectedResult);
56128
}
57129

58-
public static function donationAmountsProvider()
130+
/**
131+
* @return array[]
132+
*/
133+
private static function donationAmountsProvider(): array
59134
{
60135
return array(
61136
array(

0 commit comments

Comments
 (0)