Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Payment\Helper\Data;
10 
12 
13 class DataTest extends \PHPUnit\Framework\TestCase
14 {
16  private $helper;
17 
19  private $scopeConfig;
20 
22  private $initialConfig;
23 
25  private $methodFactory;
26 
30  private $layoutMock;
31 
35  private $appEmulation;
36 
37  protected function setUp()
38  {
39  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40  $className = \Magento\Payment\Helper\Data::class;
41  $arguments = $objectManagerHelper->getConstructArguments($className);
43  $context = $arguments['context'];
44  $this->scopeConfig = $context->getScopeConfig();
45  $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
46  $layoutFactoryMock = $arguments['layoutFactory'];
47  $layoutFactoryMock->expects($this->once())->method('create')->willReturn($this->layoutMock);
48 
49  $this->methodFactory = $arguments['paymentMethodFactory'];
50  $this->appEmulation = $arguments['appEmulation'];
51  $this->initialConfig = $arguments['initialConfig'];
52 
53  $this->helper = $objectManagerHelper->getObject($className, $arguments);
54  }
55 
56  public function testGetMethodInstance()
57  {
58  list($code, $class, $methodInstance) = ['method_code', 'method_class', 'method_instance'];
59 
60  $this->scopeConfig->expects(
61  $this->once()
62  )->method(
63  'getValue'
64  )->will(
65  $this->returnValue(
66  $class
67  )
68  );
69  $this->methodFactory->expects(
70  $this->any()
71  )->method(
72  'create'
73  )->with(
74  $class
75  )->will(
76  $this->returnValue(
77  $methodInstance
78  )
79  );
80 
81  $this->assertEquals($methodInstance, $this->helper->getMethodInstance($code));
82  }
83 
88  {
89  $this->scopeConfig->expects($this->once())
90  ->method('getValue')
91  ->willReturn(null);
92 
93  $this->helper->getMethodInstance('code');
94  }
95 
102  public function testSortMethods(array $methodA, array $methodB)
103  {
104  $this->initialConfig->expects($this->once())
105  ->method('getData')
106  ->will(
107  $this->returnValue(
108  [
109  \Magento\Payment\Helper\Data::XML_PATH_PAYMENT_METHODS => [
110  $methodA['code'] => $methodA['data'],
111  $methodB['code'] => $methodB['data'],
112  'empty' => [],
113 
114  ]
115  ]
116  )
117  );
118 
119  $this->scopeConfig->expects(new MethodInvokedAtIndex(0))
120  ->method('getValue')
121  ->with(sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, $methodA['code']))
122  ->will($this->returnValue(\Magento\Payment\Model\Method\AbstractMethod::class));
123  $this->scopeConfig->expects(new MethodInvokedAtIndex(1))
124  ->method('getValue')
125  ->with(
126  sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, $methodB['code'])
127  )
128  ->will($this->returnValue(\Magento\Payment\Model\Method\AbstractMethod::class));
129  $this->scopeConfig->expects(new MethodInvokedAtIndex(2))
130  ->method('getValue')
131  ->with(sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, 'empty'))
132  ->will($this->returnValue(null));
133 
134  $methodInstanceMockA = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
135  ->getMockForAbstractClass();
136  $methodInstanceMockA->expects($this->any())
137  ->method('isAvailable')
138  ->will($this->returnValue(true));
139  $methodInstanceMockA->expects($this->any())
140  ->method('getConfigData')
141  ->with('sort_order', null)
142  ->will($this->returnValue($methodA['data']['sort_order']));
143 
144  $methodInstanceMockB = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
145  ->getMockForAbstractClass();
146  $methodInstanceMockB->expects($this->any())
147  ->method('isAvailable')
148  ->will($this->returnValue(true));
149  $methodInstanceMockB->expects($this->any())
150  ->method('getConfigData')
151  ->with('sort_order', null)
152  ->will($this->returnValue($methodB['data']['sort_order']));
153 
154  $this->methodFactory->expects($this->at(0))
155  ->method('create')
156  ->will($this->returnValue($methodInstanceMockA));
157 
158  $this->methodFactory->expects($this->at(1))
159  ->method('create')
160  ->will($this->returnValue($methodInstanceMockB));
161 
162  $sortedMethods = $this->helper->getStoreMethods();
163  $this->assertTrue(
164  array_shift($sortedMethods)->getConfigData('sort_order')
165  < array_shift($sortedMethods)->getConfigData('sort_order')
166  );
167  }
168 
169  public function testGetMethodFormBlock()
170  {
171  list($blockType, $methodCode) = ['method_block_type', 'method_code'];
172 
173  $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
174  ->getMockForAbstractClass();
175  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
176  ->disableOriginalConstructor()
177  ->setMethods([])
178  ->getMock();
179  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
180  ->disableOriginalConstructor()
181  ->setMethods(['setMethod', 'toHtml'])
182  ->getMock();
183 
184  $methodMock->expects($this->once())->method('getFormBlockType')->willReturn($blockType);
185  $methodMock->expects($this->once())->method('getCode')->willReturn($methodCode);
186  $layoutMock->expects($this->once())->method('createBlock')
187  ->with($blockType, $methodCode)
188  ->willReturn($blockMock);
189  $blockMock->expects($this->once())->method('setMethod')->with($methodMock);
190 
191  $this->assertSame($blockMock, $this->helper->getMethodFormBlock($methodMock, $layoutMock));
192  }
193 
194  public function testGetInfoBlock()
195  {
196  $blockType = 'method_block_type';
197 
198  $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
199  ->getMockForAbstractClass();
200  $infoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
201  ->disableOriginalConstructor()
202  ->setMethods([])
203  ->getMock();
204  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
205  ->disableOriginalConstructor()
206  ->setMethods(['setInfo', 'toHtml'])
207  ->getMock();
208 
209  $infoMock->expects($this->once())->method('getMethodInstance')->willReturn($methodMock);
210  $methodMock->expects($this->once())->method('getInfoBlockType')->willReturn($blockType);
211  $this->layoutMock->expects($this->once())->method('createBlock')
212  ->with($blockType)
213  ->willReturn($blockMock);
214  $blockMock->expects($this->once())->method('setInfo')->with($infoMock);
215 
216  $this->assertSame($blockMock, $this->helper->getInfoBlock($infoMock));
217  }
218 
219  public function testGetInfoBlockHtml()
220  {
221  list($storeId, $blockHtml, $secureMode, $blockType) = [1, 'HTML MARKUP', true, 'method_block_type'];
222 
223  $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
224  ->getMockForAbstractClass();
225  $infoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
226  ->disableOriginalConstructor()
227  ->setMethods([])
228  ->getMock();
229  $paymentBlockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
230  ->disableOriginalConstructor()
231  ->setMethods(['setArea', 'setIsSecureMode', 'getMethod', 'setStore', 'toHtml', 'setInfo'])
232  ->getMock();
233 
234  $this->appEmulation->expects($this->once())->method('startEnvironmentEmulation')->with($storeId);
235  $infoMock->expects($this->once())->method('getMethodInstance')->willReturn($methodMock);
236  $methodMock->expects($this->once())->method('getInfoBlockType')->willReturn($blockType);
237  $this->layoutMock->expects($this->once())->method('createBlock')
238  ->with($blockType)
239  ->willReturn($paymentBlockMock);
240  $paymentBlockMock->expects($this->once())->method('setInfo')->with($infoMock);
241  $paymentBlockMock->expects($this->once())->method('setArea')
242  ->with(\Magento\Framework\App\Area::AREA_FRONTEND)
243  ->willReturnSelf();
244  $paymentBlockMock->expects($this->once())->method('setIsSecureMode')
245  ->with($secureMode);
246  $paymentBlockMock->expects($this->once())->method('getMethod')
247  ->willReturn($methodMock);
248  $methodMock->expects($this->once())->method('setStore')->with($storeId);
249  $paymentBlockMock->expects($this->once())->method('toHtml')
250  ->willReturn($blockHtml);
251  $this->appEmulation->expects($this->once())->method('stopEnvironmentEmulation');
252 
253  $this->assertEquals($blockHtml, $this->helper->getInfoBlockHtml($infoMock, $storeId));
254  }
255 
259  public function getSortMethodsDataProvider()
260  {
261  return [
262  [
263  ['code' => 'methodA', 'data' => ['sort_order' => 0]],
264  ['code' => 'methodB', 'data' => ['sort_order' => 1]]
265  ],
266  [
267  ['code' => 'methodA', 'data' => ['sort_order' => 2]],
268  ['code' => 'methodB', 'data' => ['sort_order' => 1]],
269  ]
270  ];
271  }
272 }
testSortMethods(array $methodA, array $methodB)
Definition: DataTest.php:102
$_option $_optionId $class
Definition: date.phtml:13
const XML_PATH_PAYMENT_METHODS
Definition: Data.php:27
$arguments
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31