|
12 | 12 | namespace Adyen\Payment\Test\Unit\Model\Config\Backend;
|
13 | 13 |
|
14 | 14 | 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; |
15 | 27 |
|
16 |
| -class DonationAmountsTest extends \PHPUnit\Framework\TestCase |
| 28 | +class DonationAmountsTest extends AbstractAdyenTestCase |
17 | 29 | {
|
| 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 = []; |
18 | 39 |
|
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 | + } |
20 | 59 |
|
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 |
22 | 66 | {
|
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 | + ); |
27 | 77 | }
|
28 | 78 |
|
29 |
| - protected function setUp(): void |
| 79 | + /** |
| 80 | + * @return void |
| 81 | + * @throws Exception |
| 82 | + */ |
| 83 | + public function testValidateBeforeSaveSuccess() |
30 | 84 | {
|
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 | + ]; |
43 | 92 |
|
| 93 | + $this->generateSutClass(); |
44 | 94 |
|
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(); |
47 | 117 | }
|
48 | 118 |
|
49 | 119 | /**
|
50 | 120 | * @dataProvider donationAmountsProvider
|
51 | 121 | */
|
52 | 122 | public function testValidateDonationAmounts($donationAmounts, $expectedResult)
|
53 | 123 | {
|
| 124 | + $this->generateSutClass(); |
| 125 | + |
54 | 126 | $result = $this->donationAmounts->validateDonationAmounts(explode(',', (string) $donationAmounts));
|
55 | 127 | $this->assertEquals($result, $expectedResult);
|
56 | 128 | }
|
57 | 129 |
|
58 |
| - public static function donationAmountsProvider() |
| 130 | + /** |
| 131 | + * @return array[] |
| 132 | + */ |
| 133 | + private static function donationAmountsProvider(): array |
59 | 134 | {
|
60 | 135 | return array(
|
61 | 136 | array(
|
|
0 commit comments