|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Payment\Test\Unit\Model; |
| 7 | + |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class PaymentMethodListTest. |
| 12 | + */ |
| 13 | +class PaymentMethodListTest extends \PHPUnit_Framework_TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var ObjectManagerHelper |
| 17 | + */ |
| 18 | + private $objectManagerHelper; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Payment\Model\PaymentMethodList|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $paymentMethodList; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Payment\Api\Data\PaymentMethodInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $methodFactoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $helperMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * Setup. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function setUp() |
| 41 | + { |
| 42 | + $this->methodFactoryMock = $this->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterfaceFactory::class) |
| 43 | + ->setMethods(['create']) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->getMock(); |
| 46 | + $this->helperMock = $this->getMockBuilder(\Magento\Payment\Helper\Data::class) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->getMock(); |
| 49 | + |
| 50 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 51 | + $this->paymentMethodList = $this->objectManagerHelper->getObject( |
| 52 | + \Magento\Payment\Model\PaymentMethodList::class, |
| 53 | + [ |
| 54 | + 'methodFactory' => $this->methodFactoryMock, |
| 55 | + 'helper' => $this->helperMock |
| 56 | + ] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Setup getList method. |
| 62 | + * |
| 63 | + * @param array $paymentMethodConfig |
| 64 | + * @param array $methodInstancesMap |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + private function setUpGetList($paymentMethodConfig, $methodInstancesMap) |
| 68 | + { |
| 69 | + $this->helperMock->expects($this->once()) |
| 70 | + ->method('getPaymentMethods') |
| 71 | + ->willReturn($paymentMethodConfig); |
| 72 | + $this->helperMock->expects($this->any()) |
| 73 | + ->method('getMethodInstance') |
| 74 | + ->willReturnMap($methodInstancesMap); |
| 75 | + |
| 76 | + $this->methodFactoryMock->expects($this->any()) |
| 77 | + ->method('create') |
| 78 | + ->willReturnCallback(function ($data) { |
| 79 | + $paymentMethod = $this->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterface::class) |
| 80 | + ->getMockForAbstractClass(); |
| 81 | + $paymentMethod->expects($this->any()) |
| 82 | + ->method('getCode') |
| 83 | + ->willReturn($data['code']); |
| 84 | + $paymentMethod->expects($this->any()) |
| 85 | + ->method('getIsActive') |
| 86 | + ->willReturn($data['isActive']); |
| 87 | + |
| 88 | + return $paymentMethod; |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test getList. |
| 94 | + * |
| 95 | + * @param int $storeId |
| 96 | + * @param array $paymentMethodConfig |
| 97 | + * @param array $methodInstancesMap |
| 98 | + * @param array $expected |
| 99 | + * @return void |
| 100 | + * |
| 101 | + * @dataProvider getListDataProvider |
| 102 | + */ |
| 103 | + public function testGetList($storeId, $paymentMethodConfig, $methodInstancesMap, $expected) |
| 104 | + { |
| 105 | + $this->setUpGetList($paymentMethodConfig, $methodInstancesMap); |
| 106 | + |
| 107 | + $codes = array_map( |
| 108 | + function ($method) { |
| 109 | + return $method->getCode(); |
| 110 | + }, |
| 111 | + $this->paymentMethodList->getList($storeId) |
| 112 | + ); |
| 113 | + |
| 114 | + $this->assertEquals($expected, $codes); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Data provider for getList. |
| 119 | + * |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + public function getListDataProvider() |
| 123 | + { |
| 124 | + return [ |
| 125 | + [ |
| 126 | + 1, |
| 127 | + ['method_code_1' => [], 'method_code_2' => []], |
| 128 | + [ |
| 129 | + ['method_code_1', $this->mockPaymentMethodInstance(1, 10, 'method_code_1', 'title', true)], |
| 130 | + ['method_code_2', $this->mockPaymentMethodInstance(1, 5, 'method_code_2', 'title', true)] |
| 131 | + ], |
| 132 | + ['method_code_2', 'method_code_1'] |
| 133 | + ] |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test getActiveList. |
| 139 | + * |
| 140 | + * @param int $storeId |
| 141 | + * @param array $paymentMethodConfig |
| 142 | + * @param array $methodInstancesMap |
| 143 | + * @param array $expected |
| 144 | + * @return void |
| 145 | + * |
| 146 | + * @dataProvider getActiveListDataProvider |
| 147 | + */ |
| 148 | + public function testGetActiveList($storeId, $paymentMethodConfig, $methodInstancesMap, $expected) |
| 149 | + { |
| 150 | + $this->setUpGetList($paymentMethodConfig, $methodInstancesMap); |
| 151 | + |
| 152 | + $codes = array_map( |
| 153 | + function ($method) { |
| 154 | + return $method->getCode(); |
| 155 | + }, |
| 156 | + $this->paymentMethodList->getActiveList($storeId) |
| 157 | + ); |
| 158 | + |
| 159 | + $this->assertEquals($expected, $codes); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Data provider for getActiveList. |
| 164 | + * |
| 165 | + * @return array |
| 166 | + */ |
| 167 | + public function getActiveListDataProvider() |
| 168 | + { |
| 169 | + return [ |
| 170 | + [ |
| 171 | + 1, |
| 172 | + ['method_code_1' => [], 'method_code_2' => []], |
| 173 | + [ |
| 174 | + ['method_code_1', $this->mockPaymentMethodInstance(1, 10, 'method_code_1', 'title', false)], |
| 175 | + ['method_code_2', $this->mockPaymentMethodInstance(1, 5, 'method_code_2', 'title', true)] |
| 176 | + ], |
| 177 | + ['method_code_2'] |
| 178 | + ] |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Mock payment method instance. |
| 184 | + * |
| 185 | + * @param int $storeId |
| 186 | + * @param int $sortOrder |
| 187 | + * @param string $code |
| 188 | + * @param string $title |
| 189 | + * @param bool $isActive |
| 190 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 191 | + */ |
| 192 | + private function mockPaymentMethodInstance($storeId, $sortOrder, $code, $title, $isActive) |
| 193 | + { |
| 194 | + $paymentMethodInstance = $this->getMockBuilder(\Magento\Payment\Model\Method\AbstractMethod::class) |
| 195 | + ->setMethods(['getCode', 'getTitle', 'isActive', 'getConfigData']) |
| 196 | + ->disableOriginalConstructor() |
| 197 | + ->getMockForAbstractClass(); |
| 198 | + $paymentMethodInstance->expects($this->any()) |
| 199 | + ->method('getConfigData') |
| 200 | + ->willReturnMap([ |
| 201 | + ['sort_order', $storeId, $sortOrder] |
| 202 | + ]); |
| 203 | + $paymentMethodInstance->expects($this->any()) |
| 204 | + ->method('getCode') |
| 205 | + ->willReturn($code); |
| 206 | + $paymentMethodInstance->expects($this->any()) |
| 207 | + ->method('getTitle') |
| 208 | + ->willReturn($title); |
| 209 | + $paymentMethodInstance->expects($this->any()) |
| 210 | + ->method('isActive') |
| 211 | + ->willReturn($isActive); |
| 212 | + |
| 213 | + return $paymentMethodInstance; |
| 214 | + } |
| 215 | +} |
0 commit comments