PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-paypal/Test/Unit/Model/PayflowproTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 459 lines | 332 code | 43 blank | 84 comment | 0 complexity | 771db0ea4bfea017e39b382fd8c3a974 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Paypal\Model\Payflowpro
  8. */
  9. namespace Magento\Paypal\Test\Unit\Model;
  10. use Magento\Paypal\Model\Config;
  11. use Magento\Paypal\Model\Payflowpro;
  12. use Magento\Store\Model\ScopeInterface;
  13. /**
  14. * Class PayflowproTest
  15. */
  16. class PayflowproTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var Payflowpro
  20. */
  21. protected $payflowpro;
  22. /**
  23. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  24. */
  25. protected $helper;
  26. /**
  27. * @var \Magento\Payment\Model\Method\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $configMock;
  30. /**
  31. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $storeManagerMock;
  34. /**
  35. * @var \Magento\Paypal\Model\Payflow\Service\Gateway|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $gatewayMock;
  38. /**
  39. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $scopeConfigMock;
  42. protected function setUp()
  43. {
  44. $configFactoryMock = $this->getMock(
  45. 'Magento\Payment\Model\Method\ConfigInterfaceFactory',
  46. ['create'],
  47. [],
  48. '',
  49. false
  50. );
  51. $this->configMock = $this->getMock(
  52. 'Magento\Paypal\Model\PayflowConfig',
  53. [],
  54. [],
  55. '',
  56. false
  57. );
  58. $client = $this->getMock(
  59. 'Magento\Framework\HTTP\ZendClient',
  60. [],
  61. [],
  62. '',
  63. false
  64. );
  65. $this->storeManagerMock = $this->getMockForAbstractClass(
  66. 'Magento\Store\Model\StoreManagerInterface',
  67. [],
  68. '',
  69. false,
  70. true,
  71. true,
  72. ['getStore']
  73. );
  74. $this->gatewayMock = $this->getMock(
  75. 'Magento\Paypal\Model\Payflow\Service\Gateway',
  76. [],
  77. [],
  78. '',
  79. false
  80. );
  81. $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
  82. ->setMethods(['getValue'])
  83. ->getMockForAbstractClass();
  84. $configFactoryMock->expects($this->any())
  85. ->method('create')
  86. ->willReturn($this->configMock);
  87. $client->expects($this->any())->method('create')->will($this->returnSelf());
  88. $client->expects($this->any())->method('setUri')->will($this->returnSelf());
  89. $client->expects($this->any())->method('setConfig')->will($this->returnSelf());
  90. $client->expects($this->any())->method('setMethod')->will($this->returnSelf());
  91. $client->expects($this->any())->method('setParameterPost')->will($this->returnSelf());
  92. $client->expects($this->any())->method('setHeaders')->will($this->returnSelf());
  93. $client->expects($this->any())->method('setUrlEncodeBody')->will($this->returnSelf());
  94. $client->expects($this->any())->method('request')->will($this->returnSelf());
  95. $client->expects($this->any())->method('getBody')->will($this->returnValue('RESULT name=value&name2=value2'));
  96. $clientFactory = $this->getMock('Magento\Framework\HTTP\ZendClientFactory', ['create'], [], '', false);
  97. $clientFactory->expects($this->any())->method('create')->will($this->returnValue($client));
  98. $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  99. $this->payflowpro = $this->helper->getObject(
  100. 'Magento\Paypal\Model\Payflowpro',
  101. [
  102. 'configFactory' => $configFactoryMock,
  103. 'httpClientFactory' => $clientFactory,
  104. 'storeManager' => $this->storeManagerMock,
  105. 'gateway' => $this->gatewayMock,
  106. 'scopeConfig' => $this->scopeConfigMock
  107. ]
  108. );
  109. }
  110. /**
  111. * @param mixed $amountPaid
  112. * @param string $paymentType
  113. * @param bool $expected
  114. * @dataProvider canVoidDataProvider
  115. */
  116. public function testCanVoid($amountPaid, $paymentType, $expected)
  117. {
  118. $payment = $this->helper->getObject($paymentType);
  119. $payment->setAmountPaid($amountPaid);
  120. $this->payflowpro->setInfoInstance($payment);
  121. $this->assertEquals($expected, $this->payflowpro->canVoid());
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function canVoidDataProvider()
  127. {
  128. return [
  129. [0, 'Magento\Sales\Model\Order\Payment', true],
  130. [null, 'Magento\Sales\Model\Order\Payment', true]
  131. ];
  132. }
  133. public function testCanCapturePartial()
  134. {
  135. $this->assertTrue($this->payflowpro->canCapturePartial());
  136. }
  137. public function testCanRefundPartialPerInvoice()
  138. {
  139. $this->assertTrue($this->payflowpro->canRefundPartialPerInvoice());
  140. }
  141. /**
  142. * test for _buildBasicRequest (BDCODE)
  143. */
  144. public function testFetchTransactionInfoForBN()
  145. {
  146. $response = $this->getGatewayResponseObject();
  147. $this->gatewayMock->expects($this->once())
  148. ->method('postRequest')
  149. ->willReturn($response);
  150. $this->initStoreMock();
  151. $this->configMock->expects($this->once())->method('getBuildNotationCode')
  152. ->will($this->returnValue('BNCODE'));
  153. $payment = $this->getMock('Magento\Payment\Model\Info', ['setTransactionId', '__wakeup'], [], '', false);
  154. $payment->expects($this->once())->method('setTransactionId')->will($this->returnSelf());
  155. $this->payflowpro->fetchTransactionInfo($payment, 'AD49G8N825');
  156. }
  157. /**
  158. * @param $response
  159. * @dataProvider setTransStatusDataProvider
  160. */
  161. public function testSetTransStatus($response, $paymentExpected)
  162. {
  163. $payment = $this->helper->getObject('Magento\Payment\Model\Info');
  164. $this->payflowpro->setTransStatus($payment, $response);
  165. $this->assertEquals($paymentExpected->getData(), $payment->getData());
  166. }
  167. public function setTransStatusDataProvider()
  168. {
  169. return [
  170. [
  171. 'response' => new \Magento\Framework\DataObject(
  172. [
  173. 'pnref' => 'V19A3D27B61E',
  174. 'result_code' => Payflowpro::RESPONSE_CODE_APPROVED,
  175. ]
  176. ),
  177. 'paymentExpected' => new \Magento\Framework\DataObject(
  178. [
  179. 'transaction_id' => 'V19A3D27B61E',
  180. 'is_transaction_closed' => 0,
  181. ]
  182. ),
  183. ],
  184. [
  185. 'response' => new \Magento\Framework\DataObject(
  186. [
  187. 'pnref' => 'V19A3D27B61E',
  188. 'result_code' => Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER
  189. ]
  190. ),
  191. 'paymentExpected' => new \Magento\Framework\DataObject(
  192. [
  193. 'transaction_id' => 'V19A3D27B61E',
  194. 'is_transaction_closed' => 0,
  195. 'is_transaction_pending' => true,
  196. 'is_fraud_detected' => true,
  197. ]
  198. ),
  199. ],
  200. ];
  201. }
  202. /**
  203. * @param array $expectsMethods
  204. * @param bool $result
  205. *
  206. * @dataProvider dataProviderForTestIsActive
  207. */
  208. public function testIsActive(array $expectsMethods, $result)
  209. {
  210. $storeId = 15;
  211. $i = 0;
  212. foreach ($expectsMethods as $method => $isActive) {
  213. $this->scopeConfigMock->expects($this->at($i++))
  214. ->method('getValue')
  215. ->with(
  216. "payment/{$method}/active",
  217. ScopeInterface::SCOPE_STORE,
  218. $storeId
  219. )->willReturn($isActive);
  220. }
  221. $this->assertEquals($result, $this->payflowpro->isActive($storeId));
  222. }
  223. /**
  224. * @covers \Magento\Paypal\Model\Payflowpro::capture
  225. */
  226. public function testCaptureWithBuildPlaceRequest()
  227. {
  228. $paymentMock = $this->getPaymentMock();
  229. $orderMock = $this->getOrderMock();
  230. // test case to build basic request
  231. $paymentMock->expects(static::once())
  232. ->method('getAdditionalInformation')
  233. ->with('pnref')
  234. ->willReturn(false);
  235. $paymentMock->expects(static::once())
  236. ->method('getParentTransactionId')
  237. ->willReturn(false);
  238. $paymentMock->expects(static::exactly(2))
  239. ->method('getOrder')
  240. ->willReturn($orderMock);
  241. $response = $this->execGatewayRequest();
  242. $amount = 23.03;
  243. $this->payflowpro->capture($paymentMock, $amount);
  244. static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
  245. static::assertFalse((bool)$paymentMock->getIsTransactionPending());
  246. }
  247. /**
  248. * @covers \Magento\Paypal\Model\Payflowpro::authorize
  249. */
  250. public function testAuthorize()
  251. {
  252. $paymentMock = $this->getPaymentMock();
  253. $orderMock = $this->getOrderMock();
  254. $paymentMock->expects(static::exactly(2))
  255. ->method('getOrder')
  256. ->willReturn($orderMock);
  257. $response = $this->execGatewayRequest();
  258. $amount = 43.20;
  259. $this->payflowpro->authorize($paymentMock, $amount);
  260. static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
  261. static::assertFalse((bool)$paymentMock->getIsTransactionPending());
  262. }
  263. /**
  264. * @return array
  265. */
  266. public function dataProviderForTestIsActive()
  267. {
  268. return [
  269. [
  270. 'expectsMethods' => [
  271. Config::METHOD_PAYFLOWPRO => 0,
  272. Config::METHOD_PAYMENT_PRO => 1,
  273. ],
  274. 'result' => true,
  275. ],
  276. [
  277. 'expectsMethods' => [
  278. Config::METHOD_PAYFLOWPRO => 1
  279. ],
  280. 'result' => true,
  281. ],
  282. [
  283. 'expectsMethods' => [
  284. Config::METHOD_PAYFLOWPRO => 0,
  285. Config::METHOD_PAYMENT_PRO => 0,
  286. ],
  287. 'result' => false,
  288. ],
  289. ];
  290. }
  291. /**
  292. * @covers \Magento\Paypal\Model\Payflowpro::refund()
  293. */
  294. public function testRefund()
  295. {
  296. /** @var \Magento\Sales\Model\Order\Payment $paymentMock */
  297. $paymentMock = $this->getPaymentMock();
  298. $response = $this->execGatewayRequest();
  299. $amount = 213.04;
  300. $this->payflowpro->refund($paymentMock, $amount);
  301. static::assertEquals($response['pnref'], $paymentMock->getTransactionId());
  302. static::assertTrue($paymentMock->getIsTransactionClosed());
  303. }
  304. /**
  305. * Create mock object for store model
  306. * @return void
  307. */
  308. protected function initStoreMock()
  309. {
  310. $storeId = 27;
  311. $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
  312. ->disableOriginalConstructor()
  313. ->setMethods(['getId'])
  314. ->getMock();
  315. $this->storeManagerMock->expects(static::once())
  316. ->method('getStore')
  317. ->willReturn($storeMock);
  318. $storeMock->expects(static::once())
  319. ->method('getId')
  320. ->willReturn($storeId);
  321. }
  322. /**
  323. * Create response object for Payflowpro gateway
  324. * @return \Magento\Framework\DataObject
  325. */
  326. protected function getGatewayResponseObject()
  327. {
  328. return new \Magento\Framework\DataObject(
  329. [
  330. 'result' => '0',
  331. 'pnref' => 'V19A3D27B61E',
  332. 'respmsg' => 'Approved',
  333. 'authcode' => '510PNI',
  334. 'hostcode' => 'A',
  335. 'request_id' => 'f930d3dc6824c1f7230c5529dc37ae5e',
  336. 'result_code' => '0',
  337. ]
  338. );
  339. }
  340. /**
  341. * Call payflow gateway request and return response object
  342. * @return \Magento\Framework\DataObject
  343. */
  344. protected function execGatewayRequest()
  345. {
  346. $this->initStoreMock();
  347. $response = $this->getGatewayResponseObject();
  348. $this->gatewayMock->expects(static::once())
  349. ->method('postRequest')
  350. ->with(
  351. $this->isInstanceOf('Magento\Framework\DataObject'),
  352. $this->isInstanceOf('Magento\Paypal\Model\PayflowConfig')
  353. )
  354. ->willReturn($response);
  355. return $response;
  356. }
  357. /**
  358. * Create mock object for payment model
  359. * @return \PHPUnit_Framework_MockObject_MockObject
  360. */
  361. protected function getPaymentMock()
  362. {
  363. $paymentMock = $this->getMockBuilder('Magento\Payment\Model\Info')
  364. ->disableOriginalConstructor()
  365. ->setMethods([
  366. 'getAdditionalInformation', 'getParentTransactionId', 'getOrder',
  367. 'getCcNumber', 'getCcExpMonth', 'getCcExpYear', 'getCcCid'
  368. ])
  369. ->getMock();
  370. $cardData = [
  371. 'number' => 4111111111111111,
  372. 'month' => 12,
  373. 'year' => 18,
  374. 'cvv' => 123
  375. ];
  376. $paymentMock->expects(static::any())
  377. ->method('getCcNumber')
  378. ->willReturn($cardData['number']);
  379. $paymentMock->expects(static::any())
  380. ->method('getCcExpMonth')
  381. ->willReturn($cardData['month']);
  382. $paymentMock->expects(static::any())
  383. ->method('getCcExpYear')
  384. ->willReturn($cardData['year']);
  385. $paymentMock->expects(static::any())
  386. ->method('getCcCid')
  387. ->willReturn($cardData['cvv']);
  388. return $paymentMock;
  389. }
  390. /**
  391. * Create mock object for order model
  392. * @return \PHPUnit_Framework_MockObject_MockObject
  393. */
  394. protected function getOrderMock()
  395. {
  396. $orderData = [
  397. 'currency' => 'USD',
  398. 'id' => 4,
  399. 'increment_id' => '0000004'
  400. ];
  401. $orderMock = $this->getMockBuilder('Magento\Sales\Model\Order')
  402. ->disableOriginalConstructor()
  403. ->setMethods(['getBaseCurrencyCode', 'getIncrementId', 'getId', 'getBillingAddress', 'getShippingAddress'])
  404. ->getMock();
  405. $orderMock->expects(static::once())
  406. ->method('getId')
  407. ->willReturn($orderData['id']);
  408. $orderMock->expects(static::once())
  409. ->method('getBaseCurrencyCode')
  410. ->willReturn($orderData['currency']);
  411. $orderMock->expects(static::atLeastOnce())
  412. ->method('getIncrementId')
  413. ->willReturn($orderData['increment_id']);
  414. return $orderMock;
  415. }
  416. }