PlaceOrderTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Braintree\Test\Unit\Controller\Paypal;
  8. use Magento\Braintree\Controller\Paypal\PlaceOrder;
  9. use Magento\Braintree\Gateway\Config\PayPal\Config;
  10. use Magento\Braintree\Model\Paypal\Helper\OrderPlace;
  11. use Magento\Checkout\Model\Session;
  12. use Magento\Framework\App\Action\Context;
  13. use Magento\Framework\App\RequestInterface;
  14. use Magento\Framework\Controller\ResultFactory;
  15. use Magento\Framework\Controller\ResultInterface;
  16. use Magento\Framework\Message\ManagerInterface;
  17. use Magento\Quote\Model\Quote;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Class PlaceOrderTest
  22. *
  23. * @see \Magento\Braintree\Controller\Paypal\PlaceOrder
  24. *
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. */
  27. class PlaceOrderTest extends \PHPUnit\Framework\TestCase
  28. {
  29. /**
  30. * @var OrderPlace|MockObject
  31. */
  32. private $orderPlace;
  33. /**
  34. * @var Config|MockObject
  35. */
  36. private $config;
  37. /**
  38. * @var Session|MockObject
  39. */
  40. private $checkoutSession;
  41. /**
  42. * @var RequestInterface|MockObject
  43. */
  44. private $request;
  45. /**
  46. * @var ResultFactory|MockObject
  47. */
  48. private $resultFactory;
  49. /**
  50. * @var ManagerInterface|MockObject
  51. */
  52. private $messageManager;
  53. /**
  54. * @var PlaceOrder
  55. */
  56. private $placeOrder;
  57. /**
  58. * @var MockObject
  59. */
  60. private $logger;
  61. /**
  62. * @inheritdoc
  63. */
  64. protected function setUp()
  65. {
  66. /** @var Context|MockObject $context */
  67. $context = $this->getMockBuilder(Context::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->request = $this->getMockBuilder(RequestInterface::class)
  71. ->setMethods(['getPostValue'])
  72. ->getMockForAbstractClass();
  73. $this->resultFactory = $this->getMockBuilder(ResultFactory::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->checkoutSession = $this->getMockBuilder(Session::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->config = $this->getMockBuilder(Config::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->orderPlace = $this->getMockBuilder(OrderPlace::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->messageManager = $this->getMockBuilder(ManagerInterface::class)
  86. ->getMockForAbstractClass();
  87. $context->method('getRequest')
  88. ->willReturn($this->request);
  89. $context->method('getResultFactory')
  90. ->willReturn($this->resultFactory);
  91. $context->method('getMessageManager')
  92. ->willReturn($this->messageManager);
  93. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->placeOrder = new PlaceOrder(
  97. $context,
  98. $this->config,
  99. $this->checkoutSession,
  100. $this->orderPlace,
  101. $this->logger
  102. );
  103. }
  104. /**
  105. * Checks if an order is placed successfully.
  106. *
  107. * @throws \Magento\Framework\Exception\LocalizedException
  108. * @throws \Magento\Framework\Exception\NotFoundException
  109. */
  110. public function testExecute()
  111. {
  112. $agreement = ['test-data'];
  113. $quoteMock = $this->getQuoteMock();
  114. $quoteMock->method('getItemsCount')
  115. ->willReturn(1);
  116. $resultMock = $this->getResultMock();
  117. $resultMock->method('setPath')
  118. ->with('checkout/onepage/success')
  119. ->willReturnSelf();
  120. $this->resultFactory->method('create')
  121. ->with(ResultFactory::TYPE_REDIRECT)
  122. ->willReturn($resultMock);
  123. $this->request->method('getPostValue')
  124. ->with('agreement', [])
  125. ->willReturn($agreement);
  126. $this->checkoutSession->method('getQuote')
  127. ->willReturn($quoteMock);
  128. $this->orderPlace->method('execute')
  129. ->with($quoteMock, [0]);
  130. $this->messageManager->expects(self::never())
  131. ->method('addExceptionMessage');
  132. self::assertEquals($this->placeOrder->execute(), $resultMock);
  133. }
  134. /**
  135. * Checks a negative scenario during place order action.
  136. *
  137. * @throws \Magento\Framework\Exception\LocalizedException
  138. * @throws \Magento\Framework\Exception\NotFoundException
  139. */
  140. public function testExecuteException()
  141. {
  142. $agreement = ['test-data'];
  143. $quote = $this->getQuoteMock();
  144. $quote->method('getItemsCount')
  145. ->willReturn(0);
  146. $quote->method('getReservedOrderId')
  147. ->willReturn('000000111');
  148. $resultMock = $this->getResultMock();
  149. $resultMock->method('setPath')
  150. ->with('checkout/cart')
  151. ->willReturnSelf();
  152. $this->resultFactory->method('create')
  153. ->with(ResultFactory::TYPE_REDIRECT)
  154. ->willReturn($resultMock);
  155. $this->request->method('getPostValue')
  156. ->with('agreement', [])
  157. ->willReturn($agreement);
  158. $this->checkoutSession->method('getQuote')
  159. ->willReturn($quote);
  160. $this->orderPlace->expects(self::never())
  161. ->method('execute');
  162. $this->messageManager->method('addExceptionMessage')
  163. ->with(
  164. self::isInstanceOf('\InvalidArgumentException'),
  165. 'The order #000000111 cannot be processed.'
  166. );
  167. self::assertEquals($this->placeOrder->execute(), $resultMock);
  168. }
  169. /**
  170. * Gets mock object for a result.
  171. *
  172. * @return ResultInterface|MockObject
  173. */
  174. private function getResultMock()
  175. {
  176. return $this->getMockBuilder(ResultInterface::class)
  177. ->setMethods(['setPath'])
  178. ->getMockForAbstractClass();
  179. }
  180. /**
  181. * Gets mock object for a quote.
  182. *
  183. * @return Quote|MockObject
  184. */
  185. private function getQuoteMock()
  186. {
  187. return $this->getMockBuilder(Quote::class)
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. }
  191. }