7
7
namespace Magento \CatalogRule \Test \Unit \Model ;
8
8
9
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
10
+ use Magento \Framework \ObjectManagerInterface ;
11
+ use Magento \Framework \Unserialize \SecureUnserializer ;
10
12
11
13
/**
12
14
* Class RuleTest
@@ -56,26 +58,31 @@ class RuleTest extends \PHPUnit_Framework_TestCase
56
58
*/
57
59
protected $ productModel ;
58
60
61
+ /**
62
+ * @var SecureUnserializer
63
+ */
64
+ private $ unserialize ;
65
+
59
66
/**
60
67
* Set up before test
61
68
*
62
69
* @return void
63
70
*/
64
71
protected function setUp ()
65
72
{
66
- $ this ->storeManager = $ this ->getMock (' Magento\Store\Model\StoreManagerInterface ' );
67
- $ this ->storeModel = $ this ->getMock (' Magento\Store\Model\Store ' , ['__wakeup ' , 'getId ' ], [], '' , false );
73
+ $ this ->storeManager = $ this ->getMock (\ Magento \Store \Model \StoreManagerInterface::class );
74
+ $ this ->storeModel = $ this ->getMock (\ Magento \Store \Model \Store::class , ['__wakeup ' , 'getId ' ], [], '' , false );
68
75
$ this ->combineFactory = $ this ->getMock (
69
- ' Magento\CatalogRule\Model\Rule\Condition\CombineFactory ' ,
76
+ \ Magento \CatalogRule \Model \Rule \Condition \CombineFactory::class ,
70
77
[
71
- 'create '
78
+ 'create ' ,
72
79
],
73
80
[],
74
81
'' ,
75
82
false
76
83
);
77
84
$ this ->productModel = $ this ->getMock (
78
- ' Magento\Catalog\Model\Product ' ,
85
+ \ Magento \Catalog \Model \Product::class ,
79
86
[
80
87
'__wakeup ' , 'getId ' , 'setData '
81
88
],
@@ -84,53 +91,56 @@ protected function setUp()
84
91
false
85
92
);
86
93
$ this ->condition = $ this ->getMock (
87
- ' Magento\Rule\Model\Condition\Combine ' ,
94
+ \ Magento \Rule \Model \Condition \Combine::class ,
88
95
[
89
96
'setRule ' ,
90
- 'validate '
97
+ 'validate ' ,
91
98
],
92
99
[],
93
100
'' ,
94
101
false
95
102
);
96
103
$ this ->websiteModel = $ this ->getMock (
97
- ' Magento\Store\Model\Website ' ,
104
+ \ Magento \Store \Model \Website::class ,
98
105
[
99
106
'__wakeup ' ,
100
107
'getId ' ,
101
- 'getDefaultStore '
108
+ 'getDefaultStore ' ,
102
109
],
103
110
[],
104
111
'' ,
105
112
false
106
113
);
107
114
$ this ->_ruleProductProcessor = $ this ->getMock (
108
- ' \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor ' ,
115
+ \Magento \CatalogRule \Model \Indexer \Rule \RuleProductProcessor::class ,
109
116
[],
110
117
[],
111
118
'' ,
112
119
false
113
120
);
114
121
115
122
$ this ->_productCollectionFactory = $ this ->getMock (
116
- ' \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory ' ,
123
+ \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory::class ,
117
124
['create ' ],
118
125
[],
119
126
'' ,
120
127
false
121
128
);
122
129
123
130
$ this ->_resourceIterator = $ this ->getMock (
124
- ' \Magento\Framework\Model\ResourceModel\Iterator ' ,
131
+ \Magento \Framework \Model \ResourceModel \Iterator::class ,
125
132
['walk ' ],
126
133
[],
127
134
'' ,
128
135
false
129
136
);
130
137
131
138
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
139
+
140
+ $ this ->prepareObjectManager ();
141
+
132
142
$ this ->rule = $ this ->objectManagerHelper ->getObject (
133
- ' Magento\CatalogRule\Model\Rule ' ,
143
+ \ Magento \CatalogRule \Model \Rule::class ,
134
144
[
135
145
'storeManager ' => $ this ->storeManager ,
136
146
'combineFactory ' => $ this ->combineFactory ,
@@ -161,7 +171,7 @@ public function testCallbackValidateProduct($validate)
161
171
'has_options ' => '0 ' ,
162
172
'required_options ' => '0 ' ,
163
173
'created_at ' => '2014-06-25 13:14:30 ' ,
164
- 'updated_at ' => '2014-06-25 14:37:15 '
174
+ 'updated_at ' => '2014-06-25 14:37:15 ' ,
165
175
];
166
176
$ this ->storeManager ->expects ($ this ->any ())->method ('getWebsites ' )->with (true )
167
177
->will ($ this ->returnValue ([$ this ->websiteModel , $ this ->websiteModel ]));
@@ -236,15 +246,15 @@ public function validateDataDataProvider()
236
246
'sub_simple_action ' => 'by_percent ' ,
237
247
'sub_discount_amount ' => '123 ' ,
238
248
],
239
- true
249
+ true ,
240
250
],
241
251
[
242
252
[
243
253
'simple_action ' => 'by_percent ' ,
244
254
'discount_amount ' => '9,99 ' ,
245
255
'sub_is_enable ' => '0 ' ,
246
256
],
247
- true
257
+ true ,
248
258
],
249
259
[
250
260
[
@@ -256,7 +266,7 @@ public function validateDataDataProvider()
256
266
],
257
267
[
258
268
'Percentage discount should be between 0 and 100. ' ,
259
- ]
269
+ ],
260
270
],
261
271
[
262
272
[
@@ -269,7 +279,7 @@ public function validateDataDataProvider()
269
279
[
270
280
'Percentage discount should be between 0 and 100. ' ,
271
281
'Percentage discount should be between 0 and 100. ' ,
272
- ]
282
+ ],
273
283
],
274
284
[
275
285
[
@@ -281,7 +291,7 @@ public function validateDataDataProvider()
281
291
],
282
292
[
283
293
'Percentage discount should be between 0 and 100. ' ,
284
- ]
294
+ ],
285
295
],
286
296
[
287
297
[
@@ -294,7 +304,7 @@ public function validateDataDataProvider()
294
304
[
295
305
'Discount value should be 0 or greater. ' ,
296
306
'Discount value should be 0 or greater. ' ,
297
- ]
307
+ ],
298
308
],
299
309
[
300
310
[
@@ -304,7 +314,7 @@ public function validateDataDataProvider()
304
314
],
305
315
[
306
316
'Unknown action. ' ,
307
- ]
317
+ ],
308
318
],
309
319
];
310
320
}
@@ -316,7 +326,7 @@ public function validateDataDataProvider()
316
326
*/
317
327
public function testAfterDelete ()
318
328
{
319
- $ indexer = $ this ->getMock (' \Magento\Framework\Indexer\IndexerInterface ' );
329
+ $ indexer = $ this ->getMock (\Magento \Framework \Indexer \IndexerInterface::class );
320
330
$ indexer ->expects ($ this ->once ())->method ('invalidate ' );
321
331
$ this ->_ruleProductProcessor ->expects ($ this ->once ())->method ('getIndexer ' )->will ($ this ->returnValue ($ indexer ));
322
332
$ this ->rule ->afterDelete ();
@@ -330,7 +340,7 @@ public function testAfterDelete()
330
340
public function testAfterUpdate ()
331
341
{
332
342
$ this ->rule ->isObjectNew (false );
333
- $ indexer = $ this ->getMock (' \Magento\Framework\Indexer\IndexerInterface ' );
343
+ $ indexer = $ this ->getMock (\Magento \Framework \Indexer \IndexerInterface::class );
334
344
$ indexer ->expects ($ this ->once ())->method ('invalidate ' );
335
345
$ this ->_ruleProductProcessor ->expects ($ this ->once ())->method ('getIndexer ' )->will ($ this ->returnValue ($ indexer ));
336
346
$ this ->rule ->afterSave ();
@@ -352,7 +362,7 @@ public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjec
352
362
{
353
363
$ this ->rule ->setData ('website_ids ' , []);
354
364
$ this ->rule ->isObjectNew ($ isObjectNew );
355
- $ indexer = $ this ->getMock (' \Magento\Framework\Indexer\IndexerInterface ' );
365
+ $ indexer = $ this ->getMock (\Magento \Framework \Indexer \IndexerInterface::class );
356
366
$ indexer ->expects ($ this ->any ())->method ('invalidate ' );
357
367
$ this ->_ruleProductProcessor ->expects ($ this ->any ())->method ('getIndexer ' )->will ($ this ->returnValue ($ indexer ));
358
368
@@ -383,4 +393,37 @@ public function isRuleBehaviorChangedDataProvider()
383
393
[['name ' , 'description ' ], ['name ' , 'important_data ' ], true , true ],
384
394
];
385
395
}
396
+
397
+ /**
398
+ * Prepares ObjectManager mock.
399
+ *
400
+ * @return void
401
+ */
402
+ private function prepareObjectManager ()
403
+ {
404
+ $ objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
405
+ ->disableOriginalConstructor ()
406
+ ->getMock ();
407
+
408
+ $ this ->unserialize = $ this ->getMockBuilder (SecureUnserializer::class)
409
+ ->disableOriginalConstructor ()
410
+ ->getMock ();
411
+
412
+ $ objectManagerMock ->expects ($ this ->any ())->method ('get ' )->willReturn (
413
+ [SecureUnserializer::class, $ this ->unserialize ]
414
+ );
415
+
416
+ \Magento \Framework \App \ObjectManager::setInstance ($ objectManagerMock );
417
+ }
418
+
419
+ /**
420
+ * @inheritdoc
421
+ */
422
+ protected function tearDown ()
423
+ {
424
+ $ reflectionClass = new \ReflectionClass (\Magento \Framework \App \ObjectManager::class);
425
+ $ reflectionProperty = $ reflectionClass ->getProperty ('_instance ' );
426
+ $ reflectionProperty ->setAccessible (true );
427
+ $ reflectionProperty ->setValue (null , null );
428
+ }
386
429
}
0 commit comments