Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponManagementServiceTest.php
Go to the documentation of this file.
1 <?php
7 
13 class CouponManagementServiceTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $couponFactory;
24 
28  protected $ruleFactory;
29 
33  protected $collectionFactory;
34 
38  protected $couponGenerator;
39 
43  protected $resourceModel;
44 
49 
54 
58  protected $objectManager;
59 
63  protected function setUp()
64  {
65  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
66 
67  $className = \Magento\SalesRule\Model\CouponFactory::class;
68  $this->couponFactory = $this->createMock($className);
69 
70  $className = \Magento\SalesRule\Model\RuleFactory::class;
71  $this->ruleFactory = $this->createPartialMock($className, ['create']);
72 
73  $className = \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory::class;
74  $this->collectionFactory = $this->createPartialMock($className, ['create']);
75 
76  $className = \Magento\SalesRule\Model\Coupon\Massgenerator::class;
77  $this->couponGenerator = $this->createMock($className);
78 
79  $className = \Magento\SalesRule\Model\Spi\CouponResourceInterface::class;
80  $this->resourceModel = $this->createMock($className);
81 
82  $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface::class;
83  $this->couponMassDeleteResult = $this->createMock($className);
84 
85  $className = \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory::class;
86  $this->couponMassDeleteResultFactory = $this->createPartialMock($className, ['create']);
87  $this->couponMassDeleteResultFactory
88  ->expects($this->any())
89  ->method('create')
90  ->willReturn($this->couponMassDeleteResult);
91 
92  $this->model = $this->objectManager->getObject(
93  \Magento\SalesRule\Model\Service\CouponManagementService::class,
94  [
95  'couponFactory' => $this->couponFactory,
96  'ruleFactory' => $this->ruleFactory,
97  'collectionFactory' => $this->collectionFactory,
98  'couponGenerator' => $this->couponGenerator,
99  'resourceModel' => $this->resourceModel,
100  'couponMassDeleteResultFactory' => $this->couponMassDeleteResultFactory,
101  ]
102  );
103  }
104 
108  public function testGenerate()
109  {
110  $className = \Magento\SalesRule\Model\Data\CouponGenerationSpec::class;
114  $couponSpec = $this->createPartialMock(
115  $className,
116  ['getRuleId', 'getQuantity', 'getFormat', 'getLength', 'setData']
117  );
118 
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);
123 
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([]);
128 
132  $rule = $this->createPartialMock(
133  \Magento\SalesRule\Model\Rule::class,
134  ['load', 'getRuleId', 'getToDate', 'getUsesPerCoupon', 'getUsesPerCustomer', 'getUseAutoGeneration']
135  );
136 
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);
143 
144  $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
145 
146  $result = $this->model->generate($couponSpec);
147  $this->assertEquals([], $result);
148  }
149 
154  public function testGenerateValidationException()
155  {
156  $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
160  $couponSpec = $this->createMock($className);
161 
165  $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
166 
167  $rule->expects($this->any())->method('load')->willReturnSelf();
168  $rule->expects($this->any())->method('getRuleId')->willReturn(1);
169 
170  $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
171 
172  $this->couponGenerator->expects($this->once())->method('validateData')
173  ->willThrowException(new \Magento\Framework\Exception\InputException());
174  $this->expectException(\Magento\Framework\Exception\InputException::class);
175 
176  $this->model->generate($couponSpec);
177  }
178 
183  public function testGenerateLocalizedException()
184  {
185  $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
189  $couponSpec = $this->createMock($className);
190 
194  $rule = $this->createPartialMock(
195  \Magento\SalesRule\Model\Rule::class,
196  ['load', 'getRuleId', 'getUseAutoGeneration']
197  );
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')
204  )
205  );
206  $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
207 
208  $this->couponGenerator->expects($this->once())->method('validateData')->willReturn(true);
209 
210  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
211 
212  $this->model->generate($couponSpec);
213  }
214 
219  public function testGenerateNoSuchEntity()
220  {
221  $className = \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface::class;
225  $couponSpec = $this->createMock($className);
226 
230  $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
231 
232  $rule->expects($this->any())->method('load')->willReturnSelf();
233  $rule->expects($this->any())->method('getRuleId')->willReturn(false);
234 
235  $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
236 
237  $this->couponGenerator->expects($this->once())->method('validateData')->willReturn(true);
238 
239  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
240 
241  $this->model->generate($couponSpec);
242  }
243 
247  public function testDeleteByIdsIgnore()
248  {
249  $ids = [1, 2, 3];
250 
251  $className = \Magento\SalesRule\Model\Coupon::class;
255  $coupon = $this->createMock($className);
256  $coupon->expects($this->exactly(3))->method('delete');
257 
258  $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
262  $couponCollection = $this->createMock($className);
263 
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);
267 
268  $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
269  $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
270 
271  $this->model->deleteByIds($ids, true);
272  }
273 
278  public function testDeleteByAnyNoIgnore()
279  {
280  $ids = [1, 2, 3];
281 
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);
289 
290  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
291 
292  $this->model->deleteByIds($ids, false);
293  }
294 
298  public function testDeleteByAnyExceptionOnDelete()
299  {
300  $ids = [1, 2, 3];
301 
305  $className = \Magento\SalesRule\Model\Coupon::class;
306  $coupon = $this->createMock($className);
307  $coupon->expects($this->any())->method('delete')->willThrowException(new \Exception());
308 
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);
317 
318  $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
319  $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
320 
321  $this->model->deleteByIds($ids, true);
322  }
323 
327  public function testDeleteByCodes()
328  {
329  $ids = [1, 2, 3];
330 
331  $className = \Magento\SalesRule\Model\Coupon::class;
335  $coupon = $this->createMock($className);
336  $coupon->expects($this->exactly(3))->method('delete');
337 
338  $className = \Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class;
342  $couponCollection = $this->createMock($className);
343 
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);
347 
348  $this->couponMassDeleteResult->expects($this->once())->method('setFailedItems')->willReturnSelf();
349  $this->couponMassDeleteResult->expects($this->once())->method('setMissingItems')->willReturnSelf();
350 
351  $this->model->deleteByCodes($ids, true);
352  }
353 }
$coupon
__()
Definition: __.php:13
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31