CouponManagementTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model;
  8. use Magento\Quote\Model\CouponManagement;
  9. class CouponManagementTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var CouponManagement
  13. */
  14. protected $couponManagement;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $quoteRepositoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $quoteMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $storeMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $quoteAddressMock;
  31. protected function setUp()
  32. {
  33. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  34. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  35. $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  36. 'getItemsCount',
  37. 'setCouponCode',
  38. 'collectTotals',
  39. 'save',
  40. 'getShippingAddress',
  41. 'getCouponCode',
  42. 'getStoreId',
  43. '__wakeup'
  44. ]);
  45. $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
  46. 'setCollectShippingRates',
  47. '__wakeup'
  48. ]);
  49. $this->couponManagement = new CouponManagement(
  50. $this->quoteRepositoryMock
  51. );
  52. }
  53. public function testGetCoupon()
  54. {
  55. $cartId = 11;
  56. $couponCode = 'test_coupon_code';
  57. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getCouponCode', '__wakeup']);
  58. $quoteMock->expects($this->any())->method('getCouponCode')->will($this->returnValue($couponCode));
  59. $this->quoteRepositoryMock->expects($this->once())
  60. ->method('getActive')
  61. ->with($cartId)
  62. ->will($this->returnValue($quoteMock));
  63. $this->assertEquals($couponCode, $this->couponManagement->get($cartId));
  64. }
  65. /**
  66. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  67. * @expectedExceptionMessage The "33" Cart doesn't contain products.
  68. */
  69. public function testSetWhenCartDoesNotContainsProducts()
  70. {
  71. $cartId = 33;
  72. $this->quoteRepositoryMock->expects($this->once())
  73. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  74. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
  75. $this->couponManagement->set($cartId, 'coupon_code');
  76. }
  77. /**
  78. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  79. * @expectedExceptionMessage The coupon code couldn't be applied. Verify the coupon code and try again.
  80. */
  81. public function testSetWhenCouldNotApplyCoupon()
  82. {
  83. $cartId = 33;
  84. $couponCode = '153a-ABC';
  85. $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  86. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
  87. $this->quoteRepositoryMock->expects($this->once())
  88. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  89. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  90. $this->quoteMock->expects($this->once())
  91. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  92. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  93. $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
  94. $exceptionMessage = "The coupon code couldn't be applied. Verify the coupon code and try again.";
  95. $exception = new \Magento\Framework\Exception\CouldNotDeleteException(__($exceptionMessage));
  96. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  97. $this->quoteRepositoryMock->expects($this->once())
  98. ->method('save')
  99. ->with($this->quoteMock)
  100. ->willThrowException($exception);
  101. $this->couponManagement->set($cartId, $couponCode);
  102. }
  103. /**
  104. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  105. * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
  106. */
  107. public function testSetWhenCouponCodeIsInvalid()
  108. {
  109. $cartId = 33;
  110. $couponCode = '153a-ABC';
  111. $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  112. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
  113. $this->quoteRepositoryMock->expects($this->once())
  114. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  115. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  116. $this->quoteMock->expects($this->once())
  117. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  118. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  119. $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
  120. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  121. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  122. $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('invalidCoupon'));
  123. $this->couponManagement->set($cartId, $couponCode);
  124. }
  125. public function testSet()
  126. {
  127. $cartId = 33;
  128. $couponCode = '153a-ABC';
  129. $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  130. $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
  131. $this->quoteRepositoryMock->expects($this->once())
  132. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  133. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  134. $this->quoteMock->expects($this->once())
  135. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  136. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  137. $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
  138. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  139. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  140. $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue($couponCode));
  141. $this->assertTrue($this->couponManagement->set($cartId, $couponCode));
  142. }
  143. /**
  144. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  145. * @expectedExceptionMessage The "65" Cart doesn't contain products.
  146. */
  147. public function testDeleteWhenCartDoesNotContainsProducts()
  148. {
  149. $cartId = 65;
  150. $this->quoteRepositoryMock->expects($this->once())
  151. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  152. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
  153. $this->quoteMock->expects($this->never())->method('getShippingAddress');
  154. $this->couponManagement->remove($cartId);
  155. }
  156. /**
  157. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  158. * @expectedExceptionMessage The coupon code couldn't be deleted. Verify the coupon code and try again.
  159. */
  160. public function testDeleteWhenCouldNotDeleteCoupon()
  161. {
  162. $cartId = 65;
  163. $this->quoteRepositoryMock->expects($this->once())
  164. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  165. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  166. $this->quoteMock->expects($this->once())
  167. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  168. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  169. $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
  170. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  171. $exceptionMessage = "The coupon code couldn't be deleted. Verify the coupon code and try again.";
  172. $exception = new \Magento\Framework\Exception\CouldNotSaveException(__($exceptionMessage));
  173. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  174. $this->quoteRepositoryMock->expects($this->once())
  175. ->method('save')
  176. ->with($this->quoteMock)
  177. ->willThrowException($exception);
  178. $this->couponManagement->remove($cartId);
  179. }
  180. /**
  181. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  182. * @expectedExceptionMessage The coupon code couldn't be deleted. Verify the coupon code and try again.
  183. */
  184. public function testDeleteWhenCouponIsNotEmpty()
  185. {
  186. $cartId = 65;
  187. $this->quoteRepositoryMock->expects($this->once())
  188. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  189. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  190. $this->quoteMock->expects($this->once())
  191. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  192. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  193. $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
  194. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  195. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  196. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  197. $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('123_ABC'));
  198. $this->couponManagement->remove($cartId);
  199. }
  200. public function testDelete()
  201. {
  202. $cartId = 65;
  203. $this->quoteRepositoryMock->expects($this->once())
  204. ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
  205. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
  206. $this->quoteMock->expects($this->once())
  207. ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
  208. $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
  209. $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
  210. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  211. $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
  212. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
  213. $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue(''));
  214. $this->assertTrue($this->couponManagement->remove($cartId));
  215. }
  216. }