65 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
67 $className = \Magento\SalesRule\Model\CouponFactory::class;
68 $this->couponFactory = $this->createMock(
$className);
70 $className = \Magento\SalesRule\Model\RuleFactory::class;
71 $this->ruleFactory = $this->createPartialMock(
$className, [
'create']);
73 $className = \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory::class;
74 $this->collectionFactory = $this->createPartialMock(
$className, [
'create']);
76 $className = \Magento\SalesRule\Model\Coupon\Massgenerator::class;
77 $this->couponGenerator = $this->createMock(
$className);
79 $className = \Magento\SalesRule\Model\Spi\CouponResourceInterface::class;
80 $this->resourceModel = $this->createMock(
$className);
82 $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface::class;
83 $this->couponMassDeleteResult = $this->createMock(
$className);
85 $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory::class;
86 $this->couponMassDeleteResultFactory = $this->createPartialMock(
$className, [
'create']);
87 $this->couponMassDeleteResultFactory
88 ->expects($this->any())
90 ->willReturn($this->couponMassDeleteResult);
92 $this->model = $this->objectManager->getObject(
93 \
Magento\SalesRule\Model\Service\CouponManagementService::class,
95 'couponFactory' => $this->couponFactory,
96 'ruleFactory' => $this->ruleFactory,
97 'collectionFactory' => $this->collectionFactory,
98 'couponGenerator' => $this->couponGenerator,
99 'resourceModel' => $this->resourceModel,
100 'couponMassDeleteResultFactory' => $this->couponMassDeleteResultFactory,
108 public function testGenerate()
110 $className = \Magento\SalesRule\Model\Data\CouponGenerationSpec::class;
114 $couponSpec = $this->createPartialMock(
116 [
'getRuleId',
'getQuantity',
'getFormat',
'getLength',
'setData']
119 $couponSpec->expects($this->atLeastOnce())->method(
'getRuleId')->willReturn(1);
120 $couponSpec->expects($this->once())->method(
'getQuantity')->willReturn(1);
121 $couponSpec->expects($this->once())->method(
'getFormat')->willReturn(
'num');
122 $couponSpec->expects($this->once())->method(
'getLength')->willReturn(1);
124 $this->couponGenerator->expects($this->atLeastOnce())->method(
'setData');
125 $this->couponGenerator->expects($this->once())->method(
'validateData')->willReturn(
true);
126 $this->couponGenerator->expects($this->once())->method(
'generatePool');
127 $this->couponGenerator->expects($this->once())->method(
'getGeneratedCodes')->willReturn([]);
132 $rule = $this->createPartialMock(
133 \
Magento\SalesRule\Model\Rule::class,
134 [
'load',
'getRuleId',
'getToDate',
'getUsesPerCoupon',
'getUsesPerCustomer',
'getUseAutoGeneration']
137 $rule->expects($this->any())->method(
'load')->willReturnSelf();
138 $rule->expects($this->any())->method(
'getRuleId')->willReturn(1);
139 $rule->expects($this->any())->method(
'getToDate')->willReturn(
'2015-07-31 00:00:00');
140 $rule->expects($this->any())->method(
'getUsesPerCoupon')->willReturn(20);
141 $rule->expects($this->any())->method(
'getUsesPerCustomer')->willReturn(5);
142 $rule->expects($this->any())->method(
'getUseAutoGeneration')->willReturn(
true);
144 $this->ruleFactory->expects($this->any())->method(
'create')->willReturn(
$rule);
146 $result = $this->model->generate($couponSpec);
147 $this->assertEquals([],
$result);
154 public function testGenerateValidationException()
156 $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
165 $rule = $this->createPartialMock(\
Magento\SalesRule\Model\Rule::class, [
'load',
'getRuleId']);
167 $rule->expects($this->any())->method(
'load')->willReturnSelf();
168 $rule->expects($this->any())->method(
'getRuleId')->willReturn(1);
170 $this->ruleFactory->expects($this->any())->method(
'create')->willReturn(
$rule);
172 $this->couponGenerator->expects($this->once())->method(
'validateData')
173 ->willThrowException(
new \
Magento\Framework\Exception\InputException());
174 $this->expectException(\
Magento\Framework\Exception\InputException::class);
176 $this->model->generate($couponSpec);
183 public function testGenerateLocalizedException()
185 $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
194 $rule = $this->createPartialMock(
195 \
Magento\SalesRule\Model\Rule::class,
196 [
'load',
'getRuleId',
'getUseAutoGeneration']
198 $rule->expects($this->any())->method(
'load')->willReturnSelf();
199 $rule->expects($this->any())->method(
'getRuleId')->willReturn(1);
200 $rule->expects($this->once())->method(
'getUseAutoGeneration')
201 ->willThrowException(
202 new \
Magento\Framework\Exception\LocalizedException(
203 __(
'Error occurred when generating coupons: %1',
'1')
206 $this->ruleFactory->expects($this->any())->method(
'create')->willReturn(
$rule);
208 $this->couponGenerator->expects($this->once())->method(
'validateData')->willReturn(
true);
210 $this->expectException(\
Magento\Framework\Exception\LocalizedException::class);
212 $this->model->generate($couponSpec);
219 public function testGenerateNoSuchEntity()
221 $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
230 $rule = $this->createPartialMock(\
Magento\SalesRule\Model\Rule::class, [
'load',
'getRuleId']);
232 $rule->expects($this->any())->method(
'load')->willReturnSelf();
233 $rule->expects($this->any())->method(
'getRuleId')->willReturn(
false);
235 $this->ruleFactory->expects($this->any())->method(
'create')->willReturn(
$rule);
237 $this->couponGenerator->expects($this->once())->method(
'validateData')->willReturn(
true);
239 $this->expectException(\
Magento\Framework\Exception\LocalizedException::class);
241 $this->model->generate($couponSpec);
247 public function testDeleteByIdsIgnore()
251 $className = \Magento\SalesRule\Model\Coupon::class;
256 $coupon->expects($this->exactly(3))->method(
'delete');
258 $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
262 $couponCollection = $this->createMock(
$className);
264 $couponCollection->expects($this->once())->method(
'addFieldToFilter')->willReturnSelf();
265 $couponCollection->expects($this->once())->method(
'getItems')->willReturn([
$coupon,
$coupon,
$coupon]);
266 $this->collectionFactory->expects($this->once())->method(
'create')->willReturn($couponCollection);
268 $this->couponMassDeleteResult->expects($this->once())->method(
'setFailedItems')->willReturnSelf();
269 $this->couponMassDeleteResult->expects($this->once())->method(
'setMissingItems')->willReturnSelf();
271 $this->model->deleteByIds($ids,
true);
278 public function testDeleteByAnyNoIgnore()
282 $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
286 $couponCollection = $this->createMock(
$className);
287 $couponCollection->expects($this->once())->method(
'addFieldToFilter')->willReturnSelf();
288 $this->collectionFactory->expects($this->once())->method(
'create')->willReturn($couponCollection);
290 $this->expectException(\
Magento\Framework\Exception\LocalizedException::class);
292 $this->model->deleteByIds($ids,
false);
298 public function testDeleteByAnyExceptionOnDelete()
305 $className = \Magento\SalesRule\Model\Coupon::class;
307 $coupon->expects($this->any())->method(
'delete')->willThrowException(
new \Exception());
312 $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
313 $couponCollection = $this->createMock(
$className);
314 $couponCollection->expects($this->once())->method(
'addFieldToFilter')->willReturnSelf();
315 $couponCollection->expects($this->once())->method(
'getItems')->willReturn([
$coupon,
$coupon,
$coupon]);
316 $this->collectionFactory->expects($this->once())->method(
'create')->willReturn($couponCollection);
318 $this->couponMassDeleteResult->expects($this->once())->method(
'setFailedItems')->willReturnSelf();
319 $this->couponMassDeleteResult->expects($this->once())->method(
'setMissingItems')->willReturnSelf();
321 $this->model->deleteByIds($ids,
true);
327 public function testDeleteByCodes()
331 $className = \Magento\SalesRule\Model\Coupon::class;
336 $coupon->expects($this->exactly(3))->method(
'delete');
338 $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
342 $couponCollection = $this->createMock(
$className);
344 $couponCollection->expects($this->once())->method(
'addFieldToFilter')->willReturnSelf();
345 $couponCollection->expects($this->once())->method(
'getItems')->willReturn([
$coupon,
$coupon,
$coupon]);
346 $this->collectionFactory->expects($this->once())->method(
'create')->willReturn($couponCollection);
348 $this->couponMassDeleteResult->expects($this->once())->method(
'setFailedItems')->willReturnSelf();
349 $this->couponMassDeleteResult->expects($this->once())->method(
'setMissingItems')->willReturnSelf();
351 $this->model->deleteByCodes($ids,
true);
$couponMassDeleteResultFactory