99
1010use Magento \Catalog \Model \Product ;
1111use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
12+ use Magento \CatalogRule \Api \Data \RuleInterface ;
1213use Magento \CatalogRule \Model \Indexer \Rule \RuleProductProcessor ;
1314use Magento \CatalogRule \Model \Rule ;
1415use Magento \CatalogRule \Model \Rule \Condition \CombineFactory ;
3132 */
3233class RuleTest extends TestCase
3334{
34- /** @var Rule */
35- protected $ rule ;
35+ /**
36+ * @var Rule
37+ */
38+ private $ rule ;
3639
37- /** @var ObjectManager */
40+ /**
41+ * @var ObjectManager
42+ */
3843 private $ objectManager ;
3944
40- /** @var StoreManagerInterface|MockObject */
41- protected $ storeManager ;
45+ /**
46+ * @var StoreManagerInterface|MockObject
47+ */
48+ private $ storeManager ;
4249
43- /** @var MockObject */
44- protected $ combineFactory ;
50+ /**
51+ * @var CombineFactory|MockObject
52+ */
53+ private $ combineFactory ;
4554
46- /** @var Store|MockObject */
47- protected $ storeModel ;
55+ /**
56+ * @var Store|MockObject
57+ */
58+ private $ storeModel ;
4859
49- /** @var Website|MockObject */
50- protected $ websiteModel ;
60+ /**
61+ * @var Website|MockObject
62+ */
63+ private $ websiteModel ;
5164
52- /** @var Combine|MockObject */
53- protected $ condition ;
65+ /**
66+ * @var Combine|MockObject
67+ */
68+ private $ condition ;
5469
5570 /**
5671 * @var RuleProductProcessor|MockObject
5772 */
58- protected $ _ruleProductProcessor ;
73+ private $ _ruleProductProcessor ;
5974
6075 /**
6176 * @var CollectionFactory|MockObject
6277 */
63- protected $ _productCollectionFactory ;
78+ private $ _productCollectionFactory ;
6479
6580 /**
6681 * @var Iterator|MockObject
6782 */
68- protected $ _resourceIterator ;
83+ private $ _resourceIterator ;
6984
7085 /**
7186 * @var Product|MockObject
7287 */
73- protected $ productModel ;
88+ private $ productModel ;
7489
7590 /**
7691 * Set up before test
@@ -85,15 +100,15 @@ protected function setUp(): void
85100 $ this ->combineFactory = $ this ->createPartialMock (
86101 CombineFactory::class,
87102 [
88- 'create '
103+ 'create ' ,
89104 ]
90105 );
91106 $ this ->productModel = $ this ->createPartialMock (
92107 Product::class,
93108 [
94109 '__wakeup ' ,
95110 'getId ' ,
96- 'setData '
111+ 'setData ' ,
97112 ]
98113 );
99114 $ this ->condition = $ this ->getMockBuilder (Combine::class)
@@ -106,7 +121,7 @@ protected function setUp(): void
106121 [
107122 '__wakeup ' ,
108123 'getId ' ,
109- 'getDefaultStore '
124+ 'getDefaultStore ' ,
110125 ]
111126 );
112127 $ this ->_ruleProductProcessor = $ this ->createMock (
@@ -192,7 +207,7 @@ public function testCallbackValidateProduct($validate)
192207 'has_options ' => '0 ' ,
193208 'required_options ' => '0 ' ,
194209 '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 ' ,
196211 ];
197212 $ this ->storeManager ->expects ($ this ->any ())->method ('getWebsites ' )->with (false )
198213 ->willReturn ([$ this ->websiteModel , $ this ->websiteModel ]);
@@ -263,14 +278,14 @@ public function validateDataDataProvider()
263278 'simple_action ' => 'by_fixed ' ,
264279 'discount_amount ' => '123 ' ,
265280 ],
266- true
281+ true ,
267282 ],
268283 [
269284 [
270285 'simple_action ' => 'by_percent ' ,
271286 'discount_amount ' => '9,99 ' ,
272287 ],
273- true
288+ true ,
274289 ],
275290 [
276291 [
@@ -279,7 +294,7 @@ public function validateDataDataProvider()
279294 ],
280295 [
281296 'Percentage discount should be between 0 and 100. ' ,
282- ]
297+ ],
283298 ],
284299 [
285300 [
@@ -288,7 +303,7 @@ public function validateDataDataProvider()
288303 ],
289304 [
290305 'Percentage discount should be between 0 and 100. ' ,
291- ]
306+ ],
292307 ],
293308 [
294309 [
@@ -297,7 +312,7 @@ public function validateDataDataProvider()
297312 ],
298313 [
299314 'Discount value should be 0 or greater. ' ,
300- ]
315+ ],
301316 ],
302317 [
303318 [
@@ -306,7 +321,7 @@ public function validateDataDataProvider()
306321 ],
307322 [
308323 'Unknown action. ' ,
309- ]
324+ ],
310325 ],
311326 ];
312327 }
@@ -325,33 +340,48 @@ public function testAfterDelete()
325340 }
326341
327342 /**
328- * Test after update action for inactive rule
343+ * Test after update action for active and deactivated rule.
329344 *
345+ * @dataProvider afterUpdateDataProvider
346+ * @param int $active
330347 * @return void
331348 */
332- public function testAfterUpdateInactive ( )
349+ public function testAfterUpdate ( int $ active )
333350 {
334351 $ 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 );
337357 $ this ->rule ->afterSave ();
338358 }
339359
340360 /**
341- * Test after update action for active rule
361+ * Test after update action for inactive rule.
342362 *
343363 * @return void
344364 */
345- public function testAfterUpdateActive ()
365+ public function testAfterUpdateInactiveRule ()
346366 {
347367 $ 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 ' );
352371 $ this ->rule ->afterSave ();
353372 }
354373
374+ /**
375+ * @return array
376+ */
377+ public function afterUpdateDataProvider (): array
378+ {
379+ return [
380+ ['active ' => 0 ],
381+ ['active ' => 1 ],
382+ ];
383+ }
384+
355385 /**
356386 * Test isRuleBehaviorChanged action
357387 *
0 commit comments