9
9
10
10
use Magento \Catalog \Model \Product ;
11
11
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
12
+ use Magento \CatalogRule \Api \Data \RuleInterface ;
12
13
use Magento \CatalogRule \Model \Indexer \Rule \RuleProductProcessor ;
13
14
use Magento \CatalogRule \Model \Rule ;
14
15
use Magento \CatalogRule \Model \Rule \Condition \CombineFactory ;
31
32
*/
32
33
class RuleTest extends TestCase
33
34
{
34
- /** @var Rule */
35
- protected $ rule ;
35
+ /**
36
+ * @var Rule
37
+ */
38
+ private $ rule ;
36
39
37
- /** @var ObjectManager */
40
+ /**
41
+ * @var ObjectManager
42
+ */
38
43
private $ objectManager ;
39
44
40
- /** @var StoreManagerInterface|MockObject */
41
- protected $ storeManager ;
45
+ /**
46
+ * @var StoreManagerInterface|MockObject
47
+ */
48
+ private $ storeManager ;
42
49
43
- /** @var MockObject */
44
- protected $ combineFactory ;
50
+ /**
51
+ * @var CombineFactory|MockObject
52
+ */
53
+ private $ combineFactory ;
45
54
46
- /** @var Store|MockObject */
47
- protected $ storeModel ;
55
+ /**
56
+ * @var Store|MockObject
57
+ */
58
+ private $ storeModel ;
48
59
49
- /** @var Website|MockObject */
50
- protected $ websiteModel ;
60
+ /**
61
+ * @var Website|MockObject
62
+ */
63
+ private $ websiteModel ;
51
64
52
- /** @var Combine|MockObject */
53
- protected $ condition ;
65
+ /**
66
+ * @var Combine|MockObject
67
+ */
68
+ private $ condition ;
54
69
55
70
/**
56
71
* @var RuleProductProcessor|MockObject
57
72
*/
58
- protected $ _ruleProductProcessor ;
73
+ private $ _ruleProductProcessor ;
59
74
60
75
/**
61
76
* @var CollectionFactory|MockObject
62
77
*/
63
- protected $ _productCollectionFactory ;
78
+ private $ _productCollectionFactory ;
64
79
65
80
/**
66
81
* @var Iterator|MockObject
67
82
*/
68
- protected $ _resourceIterator ;
83
+ private $ _resourceIterator ;
69
84
70
85
/**
71
86
* @var Product|MockObject
72
87
*/
73
- protected $ productModel ;
88
+ private $ productModel ;
74
89
75
90
/**
76
91
* Set up before test
@@ -85,15 +100,15 @@ protected function setUp(): void
85
100
$ this ->combineFactory = $ this ->createPartialMock (
86
101
CombineFactory::class,
87
102
[
88
- 'create '
103
+ 'create ' ,
89
104
]
90
105
);
91
106
$ this ->productModel = $ this ->createPartialMock (
92
107
Product::class,
93
108
[
94
109
'__wakeup ' ,
95
110
'getId ' ,
96
- 'setData '
111
+ 'setData ' ,
97
112
]
98
113
);
99
114
$ this ->condition = $ this ->getMockBuilder (Combine::class)
@@ -106,7 +121,7 @@ protected function setUp(): void
106
121
[
107
122
'__wakeup ' ,
108
123
'getId ' ,
109
- 'getDefaultStore '
124
+ 'getDefaultStore ' ,
110
125
]
111
126
);
112
127
$ this ->_ruleProductProcessor = $ this ->createMock (
@@ -192,7 +207,7 @@ public function testCallbackValidateProduct($validate)
192
207
'has_options ' => '0 ' ,
193
208
'required_options ' => '0 ' ,
194
209
'created_at ' => '2014-06-25 13:14:30 ' ,
195
- 'updated_at ' => '2014-06-25 14:37:15 '
210
+ 'updated_at ' => '2014-06-25 14:37:15 ' ,
196
211
];
197
212
$ this ->storeManager ->expects ($ this ->any ())->method ('getWebsites ' )->with (false )
198
213
->willReturn ([$ this ->websiteModel , $ this ->websiteModel ]);
@@ -263,14 +278,14 @@ public function validateDataDataProvider()
263
278
'simple_action ' => 'by_fixed ' ,
264
279
'discount_amount ' => '123 ' ,
265
280
],
266
- true
281
+ true ,
267
282
],
268
283
[
269
284
[
270
285
'simple_action ' => 'by_percent ' ,
271
286
'discount_amount ' => '9,99 ' ,
272
287
],
273
- true
288
+ true ,
274
289
],
275
290
[
276
291
[
@@ -279,7 +294,7 @@ public function validateDataDataProvider()
279
294
],
280
295
[
281
296
'Percentage discount should be between 0 and 100. ' ,
282
- ]
297
+ ],
283
298
],
284
299
[
285
300
[
@@ -288,7 +303,7 @@ public function validateDataDataProvider()
288
303
],
289
304
[
290
305
'Percentage discount should be between 0 and 100. ' ,
291
- ]
306
+ ],
292
307
],
293
308
[
294
309
[
@@ -297,7 +312,7 @@ public function validateDataDataProvider()
297
312
],
298
313
[
299
314
'Discount value should be 0 or greater. ' ,
300
- ]
315
+ ],
301
316
],
302
317
[
303
318
[
@@ -306,7 +321,7 @@ public function validateDataDataProvider()
306
321
],
307
322
[
308
323
'Unknown action. ' ,
309
- ]
324
+ ],
310
325
],
311
326
];
312
327
}
@@ -325,33 +340,48 @@ public function testAfterDelete()
325
340
}
326
341
327
342
/**
328
- * Test after update action for inactive rule
343
+ * Test after update action for active and deactivated rule.
329
344
*
345
+ * @dataProvider afterUpdateDataProvider
346
+ * @param int $active
330
347
* @return void
331
348
*/
332
- public function testAfterUpdateInactive ( )
349
+ public function testAfterUpdate ( int $ active )
333
350
{
334
351
$ this ->rule ->isObjectNew (false );
335
- $ this ->rule ->setIsActive (0 );
336
- $ this ->_ruleProductProcessor ->expects ($ this ->never ())->method ('getIndexer ' );
352
+ $ this ->rule ->setIsActive ($ active );
353
+ $ this ->rule ->setOrigData (RuleInterface::IS_ACTIVE , 1 );
354
+ $ indexer = $ this ->getMockForAbstractClass (IndexerInterface::class);
355
+ $ indexer ->expects ($ this ->once ())->method ('invalidate ' );
356
+ $ this ->_ruleProductProcessor ->expects ($ this ->once ())->method ('getIndexer ' )->willReturn ($ indexer );
337
357
$ this ->rule ->afterSave ();
338
358
}
339
359
340
360
/**
341
- * Test after update action for active rule
361
+ * Test after update action for inactive rule.
342
362
*
343
363
* @return void
344
364
*/
345
- public function testAfterUpdateActive ()
365
+ public function testAfterUpdateInactiveRule ()
346
366
{
347
367
$ this ->rule ->isObjectNew (false );
348
- $ this ->rule ->setIsActive (1 );
349
- $ indexer = $ this ->getMockForAbstractClass (IndexerInterface::class);
350
- $ indexer ->expects ($ this ->once ())->method ('invalidate ' );
351
- $ this ->_ruleProductProcessor ->expects ($ this ->once ())->method ('getIndexer ' )->willReturn ($ indexer );
368
+ $ this ->rule ->setIsActive (0 );
369
+ $ this ->rule ->setOrigData (RuleInterface::IS_ACTIVE , 0 );
370
+ $ this ->_ruleProductProcessor ->expects ($ this ->never ())->method ('getIndexer ' );
352
371
$ this ->rule ->afterSave ();
353
372
}
354
373
374
+ /**
375
+ * @return array
376
+ */
377
+ public function afterUpdateDataProvider (): array
378
+ {
379
+ return [
380
+ ['active ' => 0 ],
381
+ ['active ' => 1 ],
382
+ ];
383
+ }
384
+
355
385
/**
356
386
* Test isRuleBehaviorChanged action
357
387
*
0 commit comments