DataTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Helper;
  7. use \Magento\Payment\Helper\Data;
  8. use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
  9. class DataTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Payment\Helper\Data */
  12. private $helper;
  13. /** @var \PHPUnit_Framework_MockObject_MockObject */
  14. private $scopeConfig;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject */
  16. private $initialConfig;
  17. /** @var \PHPUnit_Framework_MockObject_MockObject */
  18. private $methodFactory;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $layoutMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $appEmulation;
  27. protected function setUp()
  28. {
  29. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  30. $className = \Magento\Payment\Helper\Data::class;
  31. $arguments = $objectManagerHelper->getConstructArguments($className);
  32. /** @var \Magento\Framework\App\Helper\Context $context */
  33. $context = $arguments['context'];
  34. $this->scopeConfig = $context->getScopeConfig();
  35. $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  36. $layoutFactoryMock = $arguments['layoutFactory'];
  37. $layoutFactoryMock->expects($this->once())->method('create')->willReturn($this->layoutMock);
  38. $this->methodFactory = $arguments['paymentMethodFactory'];
  39. $this->appEmulation = $arguments['appEmulation'];
  40. $this->initialConfig = $arguments['initialConfig'];
  41. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  42. }
  43. public function testGetMethodInstance()
  44. {
  45. list($code, $class, $methodInstance) = ['method_code', 'method_class', 'method_instance'];
  46. $this->scopeConfig->expects(
  47. $this->once()
  48. )->method(
  49. 'getValue'
  50. )->will(
  51. $this->returnValue(
  52. $class
  53. )
  54. );
  55. $this->methodFactory->expects(
  56. $this->any()
  57. )->method(
  58. 'create'
  59. )->with(
  60. $class
  61. )->will(
  62. $this->returnValue(
  63. $methodInstance
  64. )
  65. );
  66. $this->assertEquals($methodInstance, $this->helper->getMethodInstance($code));
  67. }
  68. /**
  69. * @expectedException \UnexpectedValueException
  70. */
  71. public function testGetMethodInstanceWithException()
  72. {
  73. $this->scopeConfig->expects($this->once())
  74. ->method('getValue')
  75. ->willReturn(null);
  76. $this->helper->getMethodInstance('code');
  77. }
  78. /**
  79. * @param array $methodA
  80. * @param array $methodB
  81. *
  82. * @dataProvider getSortMethodsDataProvider
  83. */
  84. public function testSortMethods(array $methodA, array $methodB)
  85. {
  86. $this->initialConfig->expects($this->once())
  87. ->method('getData')
  88. ->will(
  89. $this->returnValue(
  90. [
  91. \Magento\Payment\Helper\Data::XML_PATH_PAYMENT_METHODS => [
  92. $methodA['code'] => $methodA['data'],
  93. $methodB['code'] => $methodB['data'],
  94. 'empty' => [],
  95. ]
  96. ]
  97. )
  98. );
  99. $this->scopeConfig->expects(new MethodInvokedAtIndex(0))
  100. ->method('getValue')
  101. ->with(sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, $methodA['code']))
  102. ->will($this->returnValue(\Magento\Payment\Model\Method\AbstractMethod::class));
  103. $this->scopeConfig->expects(new MethodInvokedAtIndex(1))
  104. ->method('getValue')
  105. ->with(
  106. sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, $methodB['code'])
  107. )
  108. ->will($this->returnValue(\Magento\Payment\Model\Method\AbstractMethod::class));
  109. $this->scopeConfig->expects(new MethodInvokedAtIndex(2))
  110. ->method('getValue')
  111. ->with(sprintf('%s/%s/model', Data::XML_PATH_PAYMENT_METHODS, 'empty'))
  112. ->will($this->returnValue(null));
  113. $methodInstanceMockA = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  114. ->getMockForAbstractClass();
  115. $methodInstanceMockA->expects($this->any())
  116. ->method('isAvailable')
  117. ->will($this->returnValue(true));
  118. $methodInstanceMockA->expects($this->any())
  119. ->method('getConfigData')
  120. ->with('sort_order', null)
  121. ->will($this->returnValue($methodA['data']['sort_order']));
  122. $methodInstanceMockB = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  123. ->getMockForAbstractClass();
  124. $methodInstanceMockB->expects($this->any())
  125. ->method('isAvailable')
  126. ->will($this->returnValue(true));
  127. $methodInstanceMockB->expects($this->any())
  128. ->method('getConfigData')
  129. ->with('sort_order', null)
  130. ->will($this->returnValue($methodB['data']['sort_order']));
  131. $this->methodFactory->expects($this->at(0))
  132. ->method('create')
  133. ->will($this->returnValue($methodInstanceMockA));
  134. $this->methodFactory->expects($this->at(1))
  135. ->method('create')
  136. ->will($this->returnValue($methodInstanceMockB));
  137. $sortedMethods = $this->helper->getStoreMethods();
  138. $this->assertTrue(
  139. array_shift($sortedMethods)->getConfigData('sort_order')
  140. < array_shift($sortedMethods)->getConfigData('sort_order')
  141. );
  142. }
  143. public function testGetMethodFormBlock()
  144. {
  145. list($blockType, $methodCode) = ['method_block_type', 'method_code'];
  146. $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  147. ->getMockForAbstractClass();
  148. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  149. ->disableOriginalConstructor()
  150. ->setMethods([])
  151. ->getMock();
  152. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
  153. ->disableOriginalConstructor()
  154. ->setMethods(['setMethod', 'toHtml'])
  155. ->getMock();
  156. $methodMock->expects($this->once())->method('getFormBlockType')->willReturn($blockType);
  157. $methodMock->expects($this->once())->method('getCode')->willReturn($methodCode);
  158. $layoutMock->expects($this->once())->method('createBlock')
  159. ->with($blockType, $methodCode)
  160. ->willReturn($blockMock);
  161. $blockMock->expects($this->once())->method('setMethod')->with($methodMock);
  162. $this->assertSame($blockMock, $this->helper->getMethodFormBlock($methodMock, $layoutMock));
  163. }
  164. public function testGetInfoBlock()
  165. {
  166. $blockType = 'method_block_type';
  167. $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  168. ->getMockForAbstractClass();
  169. $infoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
  170. ->disableOriginalConstructor()
  171. ->setMethods([])
  172. ->getMock();
  173. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
  174. ->disableOriginalConstructor()
  175. ->setMethods(['setInfo', 'toHtml'])
  176. ->getMock();
  177. $infoMock->expects($this->once())->method('getMethodInstance')->willReturn($methodMock);
  178. $methodMock->expects($this->once())->method('getInfoBlockType')->willReturn($blockType);
  179. $this->layoutMock->expects($this->once())->method('createBlock')
  180. ->with($blockType)
  181. ->willReturn($blockMock);
  182. $blockMock->expects($this->once())->method('setInfo')->with($infoMock);
  183. $this->assertSame($blockMock, $this->helper->getInfoBlock($infoMock));
  184. }
  185. public function testGetInfoBlockHtml()
  186. {
  187. list($storeId, $blockHtml, $secureMode, $blockType) = [1, 'HTML MARKUP', true, 'method_block_type'];
  188. $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  189. ->getMockForAbstractClass();
  190. $infoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
  191. ->disableOriginalConstructor()
  192. ->setMethods([])
  193. ->getMock();
  194. $paymentBlockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
  195. ->disableOriginalConstructor()
  196. ->setMethods(['setArea', 'setIsSecureMode', 'getMethod', 'setStore', 'toHtml', 'setInfo'])
  197. ->getMock();
  198. $this->appEmulation->expects($this->once())->method('startEnvironmentEmulation')->with($storeId);
  199. $infoMock->expects($this->once())->method('getMethodInstance')->willReturn($methodMock);
  200. $methodMock->expects($this->once())->method('getInfoBlockType')->willReturn($blockType);
  201. $this->layoutMock->expects($this->once())->method('createBlock')
  202. ->with($blockType)
  203. ->willReturn($paymentBlockMock);
  204. $paymentBlockMock->expects($this->once())->method('setInfo')->with($infoMock);
  205. $paymentBlockMock->expects($this->once())->method('setArea')
  206. ->with(\Magento\Framework\App\Area::AREA_FRONTEND)
  207. ->willReturnSelf();
  208. $paymentBlockMock->expects($this->once())->method('setIsSecureMode')
  209. ->with($secureMode);
  210. $paymentBlockMock->expects($this->once())->method('getMethod')
  211. ->willReturn($methodMock);
  212. $methodMock->expects($this->once())->method('setStore')->with($storeId);
  213. $paymentBlockMock->expects($this->once())->method('toHtml')
  214. ->willReturn($blockHtml);
  215. $this->appEmulation->expects($this->once())->method('stopEnvironmentEmulation');
  216. $this->assertEquals($blockHtml, $this->helper->getInfoBlockHtml($infoMock, $storeId));
  217. }
  218. /**
  219. * @return array
  220. */
  221. public function getSortMethodsDataProvider()
  222. {
  223. return [
  224. [
  225. ['code' => 'methodA', 'data' => ['sort_order' => 0]],
  226. ['code' => 'methodB', 'data' => ['sort_order' => 1]]
  227. ],
  228. [
  229. ['code' => 'methodA', 'data' => ['sort_order' => 2]],
  230. ['code' => 'methodB', 'data' => ['sort_order' => 1]],
  231. ]
  232. ];
  233. }
  234. }