CouponPostTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Controller\Cart;
  7. use Magento\Checkout\Controller\Cart\Index;
  8. /**
  9. * Class IndexTest
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class CouponPostTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Index
  17. */
  18. protected $controller;
  19. /**
  20. * @var \Magento\Checkout\Model\Session | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $checkoutSession;
  23. /**
  24. * @var \Magento\Framework\App\Request\Http | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $request;
  27. /**
  28. * @var \Magento\Framework\App\Response\Http | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $response;
  31. /**
  32. * @var \Magento\Quote\Model\Quote | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $quote;
  35. /**
  36. * @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $eventManager;
  39. /**
  40. * @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $objectManagerMock;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $cart;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $messageManager;
  51. /**
  52. * @var \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $couponFactory;
  55. /**
  56. * @var \PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $quoteRepository;
  59. /**
  60. * @var \PHPUnit_Framework_MockObject_MockObject
  61. */
  62. private $redirect;
  63. /**
  64. * @var \PHPUnit_Framework_MockObject_MockObject
  65. */
  66. private $redirectFactory;
  67. /**
  68. * @return void
  69. */
  70. protected function setUp()
  71. {
  72. $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  73. $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  74. $this->quote = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  75. 'setCouponCode',
  76. 'getItemsCount',
  77. 'getShippingAddress',
  78. 'setCollectShippingRates',
  79. 'getCouponCode',
  80. 'collectTotals',
  81. 'save'
  82. ]);
  83. $this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
  84. $this->checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class);
  85. $this->objectManagerMock = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
  86. 'get', 'escapeHtml'
  87. ]);
  88. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $context = $this->createMock(\Magento\Framework\App\Action\Context::class);
  92. $context->expects($this->once())
  93. ->method('getObjectManager')
  94. ->willReturn($this->objectManagerMock);
  95. $context->expects($this->once())
  96. ->method('getRequest')
  97. ->willReturn($this->request);
  98. $context->expects($this->once())
  99. ->method('getResponse')
  100. ->willReturn($this->response);
  101. $context->expects($this->once())
  102. ->method('getEventManager')
  103. ->willReturn($this->eventManager);
  104. $context->expects($this->once())
  105. ->method('getMessageManager')
  106. ->willReturn($this->messageManager);
  107. $this->redirectFactory =
  108. $this->createMock(\Magento\Framework\Controller\Result\RedirectFactory::class);
  109. $this->redirect = $this->createMock(\Magento\Store\App\Response\Redirect::class);
  110. $this->redirect->expects($this->any())
  111. ->method('getRefererUrl')
  112. ->willReturn(null);
  113. $context->expects($this->once())
  114. ->method('getRedirect')
  115. ->willReturn($this->redirect);
  116. $context->expects($this->once())
  117. ->method('getResultRedirectFactory')
  118. ->willReturn($this->redirectFactory);
  119. $this->cart = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->couponFactory = $this->getMockBuilder(\Magento\SalesRule\Model\CouponFactory::class)
  123. ->disableOriginalConstructor()
  124. ->setMethods(['create'])
  125. ->getMock();
  126. $this->quoteRepository = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  127. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  128. $this->controller = $objectManagerHelper->getObject(
  129. \Magento\Checkout\Controller\Cart\CouponPost::class,
  130. [
  131. 'context' => $context,
  132. 'checkoutSession' => $this->checkoutSession,
  133. 'cart' => $this->cart,
  134. 'couponFactory' => $this->couponFactory,
  135. 'quoteRepository' => $this->quoteRepository
  136. ]
  137. );
  138. }
  139. public function testExecuteWithEmptyCoupon()
  140. {
  141. $this->request->expects($this->at(0))
  142. ->method('getParam')
  143. ->with('remove')
  144. ->willReturn(0);
  145. $this->request->expects($this->at(1))
  146. ->method('getParam')
  147. ->with('coupon_code')
  148. ->willReturn('');
  149. $this->cart->expects($this->once())
  150. ->method('getQuote')
  151. ->willReturn($this->quote);
  152. $this->controller->execute();
  153. }
  154. public function testExecuteWithGoodCouponAndItems()
  155. {
  156. $this->request->expects($this->at(0))
  157. ->method('getParam')
  158. ->with('remove')
  159. ->willReturn(0);
  160. $this->request->expects($this->at(1))
  161. ->method('getParam')
  162. ->with('coupon_code')
  163. ->willReturn('CODE');
  164. $this->cart->expects($this->any())
  165. ->method('getQuote')
  166. ->willReturn($this->quote);
  167. $this->quote->expects($this->at(0))
  168. ->method('getCouponCode')
  169. ->willReturn('OLDCODE');
  170. $coupon = $this->createMock(\Magento\SalesRule\Model\Coupon::class);
  171. $this->couponFactory->expects($this->once())
  172. ->method('create')
  173. ->willReturn($coupon);
  174. $coupon->expects($this->once())->method('load')->willReturnSelf();
  175. $coupon->expects($this->once())->method('getId')->willReturn(1);
  176. $this->quote->expects($this->any())
  177. ->method('getItemsCount')
  178. ->willReturn(1);
  179. $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  180. $this->quote->expects($this->any())
  181. ->method('setCollectShippingRates')
  182. ->with(true);
  183. $this->quote->expects($this->any())
  184. ->method('getShippingAddress')
  185. ->willReturn($shippingAddress);
  186. $this->quote->expects($this->any())
  187. ->method('collectTotals')
  188. ->willReturn($this->quote);
  189. $this->quote->expects($this->any())
  190. ->method('setCouponCode')
  191. ->with('CODE')
  192. ->willReturnSelf();
  193. $this->quote->expects($this->any())
  194. ->method('getCouponCode')
  195. ->willReturn('CODE');
  196. $this->messageManager->expects($this->once())
  197. ->method('addSuccessMessage')
  198. ->willReturnSelf();
  199. $this->objectManagerMock->expects($this->once())
  200. ->method('get')
  201. ->willReturnSelf();
  202. $this->controller->execute();
  203. }
  204. public function testExecuteWithGoodCouponAndNoItems()
  205. {
  206. $this->request->expects($this->at(0))
  207. ->method('getParam')
  208. ->with('remove')
  209. ->willReturn(0);
  210. $this->request->expects($this->at(1))
  211. ->method('getParam')
  212. ->with('coupon_code')
  213. ->willReturn('CODE');
  214. $this->cart->expects($this->any())
  215. ->method('getQuote')
  216. ->willReturn($this->quote);
  217. $this->quote->expects($this->at(0))
  218. ->method('getCouponCode')
  219. ->willReturn('OLDCODE');
  220. $this->quote->expects($this->any())
  221. ->method('getItemsCount')
  222. ->willReturn(0);
  223. $coupon = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  224. $coupon->expects($this->once())
  225. ->method('getId')
  226. ->willReturn(1);
  227. $this->couponFactory->expects($this->once())
  228. ->method('create')
  229. ->willReturn($coupon);
  230. $this->checkoutSession->expects($this->once())
  231. ->method('getQuote')
  232. ->willReturn($this->quote);
  233. $this->quote->expects($this->any())
  234. ->method('setCouponCode')
  235. ->with('CODE')
  236. ->willReturnSelf();
  237. $this->messageManager->expects($this->once())
  238. ->method('addSuccessMessage')
  239. ->willReturnSelf();
  240. $this->objectManagerMock->expects($this->once())
  241. ->method('get')
  242. ->willReturnSelf();
  243. $this->controller->execute();
  244. }
  245. public function testExecuteWithBadCouponAndItems()
  246. {
  247. $this->request->expects($this->at(0))
  248. ->method('getParam')
  249. ->with('remove')
  250. ->willReturn(0);
  251. $this->request->expects($this->at(1))
  252. ->method('getParam')
  253. ->with('coupon_code')
  254. ->willReturn('');
  255. $this->cart->expects($this->any())
  256. ->method('getQuote')
  257. ->willReturn($this->quote);
  258. $this->quote->expects($this->at(0))
  259. ->method('getCouponCode')
  260. ->willReturn('OLDCODE');
  261. $this->quote->expects($this->any())
  262. ->method('getItemsCount')
  263. ->willReturn(1);
  264. $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  265. $this->quote->expects($this->any())
  266. ->method('setCollectShippingRates')
  267. ->with(true);
  268. $this->quote->expects($this->any())
  269. ->method('getShippingAddress')
  270. ->willReturn($shippingAddress);
  271. $this->quote->expects($this->any())
  272. ->method('collectTotals')
  273. ->willReturn($this->quote);
  274. $this->quote->expects($this->any())
  275. ->method('setCouponCode')
  276. ->with('')
  277. ->willReturnSelf();
  278. $this->messageManager->expects($this->once())
  279. ->method('addSuccessMessage')
  280. ->with('You canceled the coupon code.')
  281. ->willReturnSelf();
  282. $this->controller->execute();
  283. }
  284. public function testExecuteWithBadCouponAndNoItems()
  285. {
  286. $this->request->expects($this->at(0))
  287. ->method('getParam')
  288. ->with('remove')
  289. ->willReturn(0);
  290. $this->request->expects($this->at(1))
  291. ->method('getParam')
  292. ->with('coupon_code')
  293. ->willReturn('CODE');
  294. $this->cart->expects($this->any())
  295. ->method('getQuote')
  296. ->willReturn($this->quote);
  297. $this->quote->expects($this->at(0))
  298. ->method('getCouponCode')
  299. ->willReturn('OLDCODE');
  300. $this->quote->expects($this->any())
  301. ->method('getItemsCount')
  302. ->willReturn(0);
  303. $coupon = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  304. $coupon->expects($this->once())
  305. ->method('getId')
  306. ->willReturn(0);
  307. $this->couponFactory->expects($this->once())
  308. ->method('create')
  309. ->willReturn($coupon);
  310. $this->messageManager->expects($this->once())
  311. ->method('addErrorMessage')
  312. ->willReturnSelf();
  313. $this->objectManagerMock->expects($this->once())
  314. ->method('get')
  315. ->willReturnSelf();
  316. $this->controller->execute();
  317. }
  318. }