FormTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Block\Transparent;
  7. use Magento\Checkout\Model\Session;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\DataObject;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Payment\Model\Method\TransparentInterface;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class FormTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var FormTesting | \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $form;
  22. /**
  23. * @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $requestMock;
  26. /**
  27. * @var UrlInterface | \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $urlBuilderMock;
  30. /**
  31. * @var TransparentInterface | \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $methodMock;
  34. /**
  35. * @var Session | \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $checkoutSessionMock;
  38. protected function setUp()
  39. {
  40. $objectManagerHelper = new ObjectManager($this);
  41. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  42. ->setMethods(['getParam'])
  43. ->getMockForAbstractClass();
  44. $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  45. ->setMethods(['getUrl'])
  46. ->getMockForAbstractClass();
  47. $context = $objectManagerHelper->getObject(
  48. \Magento\Framework\View\Element\Template\Context::class,
  49. [
  50. 'request' => $this->requestMock,
  51. 'urlBuilder' => $this->urlBuilderMock
  52. ]
  53. );
  54. $this->methodMock = $this->getMockBuilder(\Magento\Payment\Model\Method\TransparentInterface::class)
  55. ->getMock();
  56. $this->checkoutSessionMock = $this->getMockBuilder(\Magento\Checkout\Model\Session::class)
  57. ->setMethods([])
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $paymentConfigMock = $this->getMockBuilder(\Magento\Payment\Model\Config::class)
  61. ->setMethods([])
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->form = new FormTesting(
  65. $context,
  66. $paymentConfigMock,
  67. $this->checkoutSessionMock
  68. );
  69. }
  70. public function testIsAjaxRequest()
  71. {
  72. $this->requestMock->expects($this->exactly(2))
  73. ->method('getParam')
  74. ->with('isAjax')
  75. ->willReturnOnConsecutiveCalls(true, false);
  76. $this->assertTrue($this->form->isAjaxRequest());
  77. $this->assertFalse($this->form->isAjaxRequest());
  78. }
  79. /**
  80. * @param string $fieldName
  81. * @param mixed $fieldValue
  82. * @param mixed $expected
  83. *
  84. * @dataProvider getMethodConfigDataDataProvider
  85. */
  86. public function testGetMethodConfigData($fieldName, $fieldValue, $expected)
  87. {
  88. $this->initializeMethodWithConfigMock([[$fieldName, null, $fieldValue]]);
  89. $this->form->setMethod($this->methodMock);
  90. $this->assertEquals($expected, $this->form->getMethodConfigData($fieldName));
  91. }
  92. /**
  93. * Initializes method mock with config mock
  94. *
  95. * @param array $configMap
  96. */
  97. private function initializeMethodWithConfigMock(array $configMap = [])
  98. {
  99. $configInterface = $this->getMockBuilder(\Magento\Payment\Model\Method\ConfigInterface::class)
  100. ->getMock();
  101. $configInterface->expects($this->any())
  102. ->method('getValue')
  103. ->willReturnMap($configMap);
  104. $this->methodMock->expects($this->any())
  105. ->method('getConfigInterface')
  106. ->willReturn($configInterface);
  107. }
  108. /**
  109. * Data provider for testGetMethodConfigData
  110. *
  111. * @see testGetMethodConfigData
  112. *
  113. * @case #1 Set string value
  114. * @case #2 Set boolean value
  115. *
  116. * @return array
  117. */
  118. public function getMethodConfigDataDataProvider()
  119. {
  120. return [
  121. ['gateway_name', 'payment_gateway', 'payment_gateway'],
  122. ['sandbox_flag', true, true],
  123. ];
  124. }
  125. /**
  126. * @dataProvider getCgiUrlDataProvider
  127. *
  128. * @param $sandboxFlag
  129. * @param $cgiUrlTestMode
  130. * @param $cgiUrl
  131. * @param $expectedUrl
  132. */
  133. public function testGetCgiUrl($sandboxFlag, $cgiUrlTestMode, $cgiUrl, $expectedUrl)
  134. {
  135. $this->initializeMethodWithConfigMock(
  136. [
  137. ['sandbox_flag', null, $sandboxFlag],
  138. ['cgi_url_test_mode', null, $cgiUrlTestMode],
  139. ['cgi_url', null, $cgiUrl]
  140. ]
  141. );
  142. $this->form->setMethod($this->methodMock);
  143. $this->assertEquals($expectedUrl, $this->form->getCgiUrl());
  144. }
  145. /**
  146. * Data provider for testGetCgiUrl
  147. *
  148. * @see testGetCgiUrl
  149. *
  150. * @case #1 The sandboxFlag is 1 we expected cgi_url_test_mode_value
  151. * @case #2 The sandboxFlag is 0 we expected cgi_url_value
  152. *
  153. * @return array
  154. */
  155. public function getCgiUrlDataProvider()
  156. {
  157. return [
  158. [
  159. 1,
  160. 'cgi_url_test_mode_value',
  161. 'cgi_url_value',
  162. 'cgi_url_test_mode_value'
  163. ],
  164. [
  165. 0,
  166. 'cgi_url_test_mode_value',
  167. 'cgi_url_value',
  168. 'cgi_url_value'
  169. ],
  170. ];
  171. }
  172. public function testGetOrderUrl()
  173. {
  174. $orderUrlPattern = 'order_url';
  175. $builtOrderUrl = 'built_url';
  176. $this->initializeMethodWithConfigMock([['place_order_url', null, $orderUrlPattern]]);
  177. $this->urlBuilderMock->expects($this->once())
  178. ->method('getUrl')
  179. ->with($orderUrlPattern)
  180. ->willReturn($builtOrderUrl);
  181. $this->form->setMethod($this->methodMock);
  182. $this->assertEquals($builtOrderUrl, $this->form->getOrderUrl());
  183. }
  184. public function testGetDateDelim()
  185. {
  186. $dateDelimiter = '/';
  187. $this->initializeMethodWithConfigMock([['date_delim', null, $dateDelimiter]]);
  188. $this->form->setMethod($this->methodMock);
  189. $this->assertEquals($dateDelimiter, $this->form->getDateDelim());
  190. }
  191. public function testGetCardFieldsMap()
  192. {
  193. $ccfields = 'x_card_code,x_exp_date,x_card_num';
  194. $this->initializeMethodWithConfigMock([['ccfields', null, $ccfields]]);
  195. $this->form->setMethod($this->methodMock);
  196. $expected = json_encode(['cccvv' => 'x_card_code', 'ccexpdate' => 'x_exp_date', 'ccnum' => 'x_card_num']);
  197. $this->assertEquals($expected, $this->form->getCardFieldsMap());
  198. }
  199. public function testToHtmlShouldRender()
  200. {
  201. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  202. ->disableOriginalConstructor()
  203. ->getMock();
  204. $paymentMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Payment::class)
  205. ->disableOriginalConstructor()
  206. ->getMock();
  207. $this->checkoutSessionMock->expects($this->once())
  208. ->method('getQuote')
  209. ->willReturn($quoteMock);
  210. $quoteMock->expects($this->once())
  211. ->method('getPayment')
  212. ->willReturn($paymentMock);
  213. $paymentMock->expects($this->once())
  214. ->method('getMethodInstance')
  215. ->willReturn($this->methodMock);
  216. $this->form->toHtml();
  217. }
  218. public function testToHtmlShouldNotRenderEmptyQuote()
  219. {
  220. $this->checkoutSessionMock->expects($this->once())
  221. ->method('getQuote')
  222. ->willReturn(null);
  223. $this->assertEmpty($this->form->toHtml());
  224. }
  225. public function testToHtmlShouldNotRenderEmptyPayment()
  226. {
  227. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  228. ->disableOriginalConstructor()
  229. ->getMock();
  230. $this->checkoutSessionMock->expects($this->once())
  231. ->method('getQuote')
  232. ->willReturn($quoteMock);
  233. $quoteMock->expects($this->once())
  234. ->method('getPayment')
  235. ->willReturn(null);
  236. $this->assertEmpty($this->form->toHtml());
  237. }
  238. public function testGetMethodSuccess()
  239. {
  240. $this->form->setMethod($this->methodMock);
  241. $this->assertSame($this->methodMock, $this->form->getMethod());
  242. }
  243. public function testGetMethodNotTransparentInterface()
  244. {
  245. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  246. $this->expectExceptionMessage((string)__('We cannot retrieve the transparent payment method model object.'));
  247. $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
  248. ->getMockForAbstractClass();
  249. $this->form->setMethod($methodMock);
  250. $this->form->getMethod();
  251. }
  252. }