CouponManagementServiceTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Model\Service;
  7. /**
  8. * Class CouponManagementServiceTest
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class CouponManagementServiceTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Model\Service\CouponManagementService
  16. */
  17. protected $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $couponFactory;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $ruleFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $collectionFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $couponGenerator;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $resourceModel;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $couponMassDeleteResultFactory;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $couponMassDeleteResult;
  46. /**
  47. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  48. */
  49. protected $objectManager;
  50. /**
  51. * Setup the test
  52. */
  53. protected function setUp()
  54. {
  55. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  56. $className = \Magento\SalesRule\Model\CouponFactory::class;
  57. $this->couponFactory = $this->createMock($className);
  58. $className = \Magento\SalesRule\Model\RuleFactory::class;
  59. $this->ruleFactory = $this->createPartialMock($className, ['create']);
  60. $className = \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory::class;
  61. $this->collectionFactory = $this->createPartialMock($className, ['create']);
  62. $className = \Magento\SalesRule\Model\Coupon\Massgenerator::class;
  63. $this->couponGenerator = $this->createMock($className);
  64. $className = \Magento\SalesRule\Model\Spi\CouponResourceInterface::class;
  65. $this->resourceModel = $this->createMock($className);
  66. $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface::class;
  67. $this->couponMassDeleteResult = $this->createMock($className);
  68. $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory::class;
  69. $this->couponMassDeleteResultFactory = $this->createPartialMock($className, ['create']);
  70. $this->couponMassDeleteResultFactory
  71. ->expects($this->any())
  72. ->method('create')
  73. ->willReturn($this->couponMassDeleteResult);
  74. $this->model = $this->objectManager->getObject(
  75. \Magento\SalesRule\Model\Service\CouponManagementService::class,
  76. [
  77. 'couponFactory' => $this->couponFactory,
  78. 'ruleFactory' => $this->ruleFactory,
  79. 'collectionFactory' => $this->collectionFactory,
  80. 'couponGenerator' => $this->couponGenerator,
  81. 'resourceModel' => $this->resourceModel,
  82. 'couponMassDeleteResultFactory' => $this->couponMassDeleteResultFactory,
  83. ]
  84. );
  85. }
  86. /**
  87. * test Generate
  88. */
  89. public function testGenerate()
  90. {
  91. $className = \Magento\SalesRule\Model\Data\CouponGenerationSpec::class;
  92. /**
  93. * @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  94. */
  95. $couponSpec = $this->createPartialMock(
  96. $className,
  97. ['getRuleId', 'getQuantity', 'getFormat', 'getLength', 'setData']
  98. );
  99. $couponSpec->expects($this->atLeastOnce())->method('getRuleId')->willReturn(1);
  100. $couponSpec->expects($this->once())->method('getQuantity')->willReturn(1);
  101. $couponSpec->expects($this->once())->method('getFormat')->willReturn('num');
  102. $couponSpec->expects($this->once())->method('getLength')->willReturn(1);
  103. $this->couponGenerator->expects($this->atLeastOnce())->method('setData');
  104. $this->couponGenerator->expects($this->once())->method('validateData')->willReturn(true);
  105. $this->couponGenerator->expects($this->once())->method('generatePool');
  106. $this->couponGenerator->expects($this->once())->method('getGeneratedCodes')->willReturn([]);
  107. /**
  108. * @var \Magento\SalesRule\Model\Rule $rule
  109. */
  110. $rule = $this->createPartialMock(
  111. \Magento\SalesRule\Model\Rule::class,
  112. ['load', 'getRuleId', 'getToDate', 'getUsesPerCoupon', 'getUsesPerCustomer', 'getUseAutoGeneration']
  113. );
  114. $rule->expects($this->any())->method('load')->willReturnSelf();
  115. $rule->expects($this->any())->method('getRuleId')->willReturn(1);
  116. $rule->expects($this->any())->method('getToDate')->willReturn('2015-07-31 00:00:00');
  117. $rule->expects($this->any())->method('getUsesPerCoupon')->willReturn(20);
  118. $rule->expects($this->any())->method('getUsesPerCustomer')->willReturn(5);
  119. $rule->expects($this->any())->method('getUseAutoGeneration')->willReturn(true);
  120. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  121. $result = $this->model->generate($couponSpec);
  122. $this->assertEquals([], $result);
  123. }
  124. /**
  125. * test Generate with validation Exception
  126. * @throws \Magento\Framework\Exception\InputException
  127. */
  128. public function testGenerateValidationException()
  129. {
  130. $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
  131. /**
  132. * @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  133. */
  134. $couponSpec = $this->createMock($className);
  135. /**
  136. * @var \Magento\SalesRule\Model\Rule $rule
  137. */
  138. $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
  139. $rule->expects($this->any())->method('load')->willReturnSelf();
  140. $rule->expects($this->any())->method('getRuleId')->willReturn(1);
  141. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  142. $this->couponGenerator->expects($this->once())->method('validateData')
  143. ->willThrowException(new \Magento\Framework\Exception\InputException());
  144. $this->expectException(\Magento\Framework\Exception\InputException::class);
  145. $this->model->generate($couponSpec);
  146. }
  147. /**
  148. * test Generate with localized Exception
  149. * @throws \Magento\Framework\Exception\LocalizedException
  150. */
  151. public function testGenerateLocalizedException()
  152. {
  153. $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
  154. /**
  155. * @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  156. */
  157. $couponSpec = $this->createMock($className);
  158. /**
  159. * @var \Magento\SalesRule\Model\Rule $rule
  160. */
  161. $rule = $this->createPartialMock(
  162. \Magento\SalesRule\Model\Rule::class,
  163. ['load', 'getRuleId', 'getUseAutoGeneration']
  164. );
  165. $rule->expects($this->any())->method('load')->willReturnSelf();
  166. $rule->expects($this->any())->method('getRuleId')->willReturn(1);
  167. $rule->expects($this->once())->method('getUseAutoGeneration')
  168. ->willThrowException(
  169. new \Magento\Framework\Exception\LocalizedException(
  170. __('Error occurred when generating coupons: %1', '1')
  171. )
  172. );
  173. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  174. $this->couponGenerator->expects($this->once())->method('validateData')->willReturn(true);
  175. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  176. $this->model->generate($couponSpec);
  177. }
  178. /**
  179. * test Generate with localized Exception
  180. * @throws \Magento\Framework\Exception\LocalizedException
  181. */
  182. public function testGenerateNoSuchEntity()
  183. {
  184. $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
  185. /**
  186. * @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  187. */
  188. $couponSpec = $this->createMock($className);
  189. /**
  190. * @var \Magento\SalesRule\Model\Rule $rule
  191. */
  192. $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
  193. $rule->expects($this->any())->method('load')->willReturnSelf();
  194. $rule->expects($this->any())->method('getRuleId')->willReturn(false);
  195. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  196. $this->couponGenerator->expects($this->once())->method('validateData')->willReturn(true);
  197. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  198. $this->model->generate($couponSpec);
  199. }
  200. /**
  201. * test DeleteByIds with Ignore non existing
  202. */
  203. public function testDeleteByIdsIgnore()
  204. {
  205. $ids = [1, 2, 3];
  206. $className = \Magento\SalesRule\Model\Coupon::class;
  207. /**
  208. * @var \Magento\SalesRule\Model\Coupon $coupon
  209. */
  210. $coupon = $this->createMock($className);
  211. $coupon->expects($this->exactly(3))->method('delete');
  212. $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
  213. /**
  214. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $couponCollection
  215. */
  216. $couponCollection = $this->createMock($className);
  217. $couponCollection->expects($this->once())->method('addFieldToFilter')->willReturnSelf();
  218. $couponCollection->expects($this->once())->method('getItems')->willReturn([$coupon, $coupon, $coupon]);
  219. $this->collectionFactory->expects($this->once())->method('create')->willReturn($couponCollection);
  220. $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
  221. $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
  222. $this->model->deleteByIds($ids, true);
  223. }
  224. /**
  225. * test DeleteByIds with not Ignore non existing
  226. * @throws \Magento\Framework\Exception\LocalizedException
  227. */
  228. public function testDeleteByAnyNoIgnore()
  229. {
  230. $ids = [1, 2, 3];
  231. $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
  232. /**
  233. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $couponCollection
  234. */
  235. $couponCollection = $this->createMock($className);
  236. $couponCollection->expects($this->once())->method('addFieldToFilter')->willReturnSelf();
  237. $this->collectionFactory->expects($this->once())->method('create')->willReturn($couponCollection);
  238. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  239. $this->model->deleteByIds($ids, false);
  240. }
  241. /**
  242. * test DeleteByIds with not Ignore non existing
  243. */
  244. public function testDeleteByAnyExceptionOnDelete()
  245. {
  246. $ids = [1, 2, 3];
  247. /**
  248. * @var \Magento\SalesRule\Model\Coupon $coupon
  249. */
  250. $className = \Magento\SalesRule\Model\Coupon::class;
  251. $coupon = $this->createMock($className);
  252. $coupon->expects($this->any())->method('delete')->willThrowException(new \Exception());
  253. /**
  254. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $couponCollection
  255. */
  256. $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
  257. $couponCollection = $this->createMock($className);
  258. $couponCollection->expects($this->once())->method('addFieldToFilter')->willReturnSelf();
  259. $couponCollection->expects($this->once())->method('getItems')->willReturn([$coupon, $coupon, $coupon]);
  260. $this->collectionFactory->expects($this->once())->method('create')->willReturn($couponCollection);
  261. $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
  262. $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
  263. $this->model->deleteByIds($ids, true);
  264. }
  265. /**
  266. * test DeleteByCodes
  267. */
  268. public function testDeleteByCodes()
  269. {
  270. $ids = [1, 2, 3];
  271. $className = \Magento\SalesRule\Model\Coupon::class;
  272. /**
  273. * @var \Magento\SalesRule\Model\Coupon $coupon
  274. */
  275. $coupon = $this->createMock($className);
  276. $coupon->expects($this->exactly(3))->method('delete');
  277. $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
  278. /**
  279. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $couponCollection
  280. */
  281. $couponCollection = $this->createMock($className);
  282. $couponCollection->expects($this->once())->method('addFieldToFilter')->willReturnSelf();
  283. $couponCollection->expects($this->once())->method('getItems')->willReturn([$coupon, $coupon, $coupon]);
  284. $this->collectionFactory->expects($this->once())->method('create')->willReturn($couponCollection);
  285. $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
  286. $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
  287. $this->model->deleteByCodes($ids, true);
  288. }
  289. }