ResponseTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Controller\Transparent;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Registry;
  10. use Magento\Framework\Session\Generic as Session;
  11. use Magento\Framework\View\Result\Layout;
  12. use Magento\Framework\View\Result\LayoutFactory;
  13. use Magento\Paypal\Controller\Transparent\Response;
  14. use Magento\Paypal\Model\Payflow\Service\Response\Transaction;
  15. use Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator;
  16. use Magento\Paypal\Model\Payflow\Transparent;
  17. use Magento\Sales\Api\PaymentFailuresInterface;
  18. /**
  19. * Test for class \Magento\Paypal\Controller\Transparent\Response
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class ResponseTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /** @var Response|\PHPUnit_Framework_MockObject_MockObject */
  26. private $object;
  27. /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. private $requestMock;
  29. /** @var Registry|\PHPUnit_Framework_MockObject_MockObject */
  30. private $coreRegistryMock;
  31. /** @var LayoutFactory|\PHPUnit_Framework_MockObject_MockObject */
  32. private $resultLayoutFactoryMock;
  33. /** @var Layout|\PHPUnit_Framework_MockObject_MockObject */
  34. private $resultLayoutMock;
  35. /** @var Context|\PHPUnit_Framework_MockObject_MockObject */
  36. private $contextMock;
  37. /** @var Transaction|\PHPUnit_Framework_MockObject_MockObject */
  38. private $transactionMock;
  39. /** @var ResponseValidator|\PHPUnit_Framework_MockObject_MockObject */
  40. private $responseValidatorMock;
  41. /**
  42. * @var Transparent | \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $payflowFacade;
  45. /**
  46. * @var PaymentFailuresInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $paymentFailures;
  49. /**
  50. * @var Session|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $sessionTransparent;
  53. /**
  54. * @inheritdoc
  55. */
  56. protected function setUp()
  57. {
  58. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  59. ->setMethods(['getPostValue'])
  60. ->disableOriginalConstructor()
  61. ->getMockForAbstractClass();
  62. $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\ViewInterface::class)
  63. ->disableOriginalConstructor()
  64. ->getMockForAbstractClass();
  65. $this->coreRegistryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
  66. ->setMethods(['register'])
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->resultLayoutMock = $this->getMockBuilder(\Magento\Framework\View\Result\Layout::class)
  70. ->setMethods(['addDefaultHandle', 'getLayout', 'getUpdate', 'load'])
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->resultLayoutFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\LayoutFactory::class)
  74. ->setMethods(['create'])
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->resultLayoutFactoryMock->expects($this->once())
  78. ->method('create')
  79. ->willReturn($this->resultLayoutMock);
  80. $this->transactionMock = $this->getMockBuilder(
  81. \Magento\Paypal\Model\Payflow\Service\Response\Transaction::class
  82. )->setMethods(['getResponseObject', 'validateResponse', 'savePaymentInQuote'])
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  86. ->setMethods(['getRequest'])
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->contextMock->expects($this->once())
  90. ->method('getRequest')
  91. ->willReturn($this->requestMock);
  92. $this->responseValidatorMock = $this->getMockBuilder(
  93. \Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator::class
  94. )->disableOriginalConstructor()
  95. ->getMock();
  96. $this->payflowFacade = $this->getMockBuilder(Transparent::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods([])
  99. ->getMock();
  100. $this->paymentFailures = $this->getMockBuilder(PaymentFailuresInterface::class)
  101. ->disableOriginalConstructor()
  102. ->setMethods(['handle'])
  103. ->getMock();
  104. $this->sessionTransparent = $this->getMockBuilder(Session::class)
  105. ->disableOriginalConstructor()
  106. ->setMethods(['getQuoteId'])
  107. ->getMock();
  108. $this->object = new Response(
  109. $this->contextMock,
  110. $this->coreRegistryMock,
  111. $this->transactionMock,
  112. $this->responseValidatorMock,
  113. $this->resultLayoutFactoryMock,
  114. $this->payflowFacade,
  115. $this->sessionTransparent,
  116. $this->paymentFailures
  117. );
  118. }
  119. public function testExecute()
  120. {
  121. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $this->transactionMock->expects($this->once())
  125. ->method('getResponseObject')
  126. ->willReturn($objectMock);
  127. $this->responseValidatorMock->expects($this->once())
  128. ->method('validate')
  129. ->with($objectMock, $this->payflowFacade);
  130. $this->transactionMock->expects($this->once())
  131. ->method('savePaymentInQuote')
  132. ->with($objectMock);
  133. $this->coreRegistryMock->expects($this->once())
  134. ->method('register')
  135. ->with('transparent_form_params', $this->logicalNot($this->arrayHasKey('error')));
  136. $this->resultLayoutMock->expects($this->once())
  137. ->method('addDefaultHandle')
  138. ->willReturnSelf();
  139. $this->resultLayoutMock->expects($this->once())
  140. ->method('getLayout')
  141. ->willReturn($this->getLayoutMock());
  142. $this->paymentFailures->expects($this->never())
  143. ->method('handle');
  144. $this->assertInstanceOf(\Magento\Framework\Controller\ResultInterface::class, $this->object->execute());
  145. }
  146. public function testExecuteWithException()
  147. {
  148. $objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $this->transactionMock->expects($this->once())
  152. ->method('getResponseObject')
  153. ->willReturn($objectMock);
  154. $this->responseValidatorMock->expects($this->once())
  155. ->method('validate')
  156. ->with($objectMock, $this->payflowFacade)
  157. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Error')));
  158. $this->coreRegistryMock->expects($this->once())
  159. ->method('register')
  160. ->with('transparent_form_params', $this->arrayHasKey('error'));
  161. $this->resultLayoutMock->expects($this->once())
  162. ->method('addDefaultHandle')
  163. ->willReturnSelf();
  164. $this->resultLayoutMock->expects($this->once())
  165. ->method('getLayout')
  166. ->willReturn($this->getLayoutMock());
  167. $this->sessionTransparent->method('getQuoteId')
  168. ->willReturn(1);
  169. $this->paymentFailures->expects($this->once())
  170. ->method('handle')
  171. ->with(1)
  172. ->willReturnSelf();
  173. $this->assertInstanceOf(\Magento\Framework\Controller\ResultInterface::class, $this->object->execute());
  174. }
  175. /**
  176. * @return \Magento\Framework\View\Layout | \PHPUnit_Framework_MockObject_MockObject
  177. */
  178. private function getLayoutMock()
  179. {
  180. $processorInterfaceMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ProcessorInterface::class)
  181. ->getMockForAbstractClass();
  182. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  183. ->setMethods(['getUpdate'])
  184. ->disableOriginalConstructor()
  185. ->getMock();
  186. $layoutMock->expects($this->once())
  187. ->method('getUpdate')
  188. ->willReturn($processorInterfaceMock);
  189. return $layoutMock;
  190. }
  191. }