28
28
use Magento \SalesRule \Model \Rule \Condition \Product as SalesRuleProduct ;
29
29
use PHPUnit \Framework \MockObject \MockObject ;
30
30
use PHPUnit \Framework \TestCase ;
31
+ use Magento \Catalog \Model \ProductCategoryList ;
31
32
32
33
/**
33
34
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -114,6 +115,10 @@ private function createValidator(): SalesRuleProduct
114
115
->getMock ()
115
116
);
116
117
118
+ $ productCategoryList = $ this ->getMockBuilder (ProductCategoryList::class)
119
+ ->disableOriginalConstructor ()
120
+ ->getMock ();
121
+
117
122
return new SalesRuleProduct (
118
123
$ contextMock ,
119
124
$ backendHelperMock ,
@@ -122,60 +127,17 @@ private function createValidator(): SalesRuleProduct
122
127
$ productRepositoryMock ,
123
128
$ productMock ,
124
129
$ collectionMock ,
125
- $ formatMock
130
+ $ formatMock ,
131
+ [],
132
+ $ productCategoryList
126
133
);
127
134
}
128
135
129
136
public function testChildIsUsedForValidation ()
130
137
{
131
- $ configurableProductMock = $ this ->createProductMock ();
132
- $ configurableProductMock
133
- ->expects ($ this ->any ())
134
- ->method ('getTypeId ' )
135
- ->willReturn (Configurable::TYPE_CODE );
136
- $ configurableProductMock
137
- ->expects ($ this ->any ())
138
- ->method ('hasData ' )
139
- ->with ('special_price ' )
140
- ->willReturn (false );
141
-
142
- /* @var AbstractItem|MockObject $item */
143
- $ item = $ this ->getMockBuilder (AbstractItem::class)
144
- ->disableOriginalConstructor ()
145
- ->setMethods (['setProduct ' , 'getProduct ' , 'getChildren ' ])
146
- ->getMockForAbstractClass ();
147
- $ item ->expects ($ this ->any ())
148
- ->method ('getProduct ' )
149
- ->willReturn ($ configurableProductMock );
150
-
151
- $ simpleProductMock = $ this ->createProductMock ();
152
- $ simpleProductMock
153
- ->expects ($ this ->any ())
154
- ->method ('getTypeId ' )
155
- ->willReturn (Type::TYPE_SIMPLE );
156
- $ simpleProductMock
157
- ->expects ($ this ->any ())
158
- ->method ('hasData ' )
159
- ->with ('special_price ' )
160
- ->willReturn (true );
161
-
162
- $ childItem = $ this ->getMockBuilder (AbstractItem::class)
163
- ->disableOriginalConstructor ()
164
- ->setMethods (['getProduct ' ])
165
- ->getMockForAbstractClass ();
166
- $ childItem ->expects ($ this ->any ())
167
- ->method ('getProduct ' )
168
- ->willReturn ($ simpleProductMock );
169
-
170
- $ item ->expects ($ this ->any ())
171
- ->method ('getChildren ' )
172
- ->willReturn ([$ childItem ]);
173
- $ item ->expects ($ this ->once ())
174
- ->method ('setProduct ' )
175
- ->with ($ simpleProductMock );
176
-
138
+ $ item = $ this ->configurableProductTestSetUp ();
139
+ $ item ->expects ($ this ->once ())->method ('setProduct ' );
177
140
$ this ->validator ->setAttribute ('special_price ' );
178
-
179
141
$ this ->validatorPlugin ->beforeValidate ($ this ->validator , $ item );
180
142
}
181
143
@@ -209,8 +171,28 @@ private function createProductMock(): MockObject
209
171
return $ productMock ;
210
172
}
211
173
212
- public function testChildIsNotUsedForValidation ()
174
+ public function configurableProductTestSetUp ()
213
175
{
176
+ $ configurableProductMock = $ this ->createProductMock ();
177
+ $ configurableProductMock
178
+ ->expects ($ this ->any ())
179
+ ->method ('getTypeId ' )
180
+ ->willReturn (Configurable::TYPE_CODE );
181
+ $ configurableProductMock
182
+ ->expects ($ this ->any ())
183
+ ->method ('hasData ' )
184
+ ->with ('special_price ' )
185
+ ->willReturn (false );
186
+
187
+ /* @var AbstractItem|MockObject $item */
188
+ $ item = $ this ->getMockBuilder (AbstractItem::class)
189
+ ->disableOriginalConstructor ()
190
+ ->setMethods (['setProduct ' , 'getProduct ' , 'getChildren ' ])
191
+ ->getMockForAbstractClass ();
192
+ $ item ->expects ($ this ->any ())
193
+ ->method ('getProduct ' )
194
+ ->willReturn ($ configurableProductMock );
195
+
214
196
$ simpleProductMock = $ this ->createProductMock ();
215
197
$ simpleProductMock
216
198
->expects ($ this ->any ())
@@ -222,17 +204,27 @@ public function testChildIsNotUsedForValidation()
222
204
->with ('special_price ' )
223
205
->willReturn (true );
224
206
225
- /* @var AbstractItem|MockObject $item */
226
- $ item = $ this ->getMockBuilder (AbstractItem::class)
207
+ $ childItem = $ this ->getMockBuilder (AbstractItem::class)
227
208
->disableOriginalConstructor ()
228
- ->setMethods (['setProduct ' , ' getProduct ' ])
209
+ ->setMethods (['getProduct ' ])
229
210
->getMockForAbstractClass ();
230
- $ item ->expects ($ this ->any ())
211
+ $ childItem ->expects ($ this ->any ())
231
212
->method ('getProduct ' )
232
213
->willReturn ($ simpleProductMock );
233
214
234
- $ this ->validator ->setAttribute ('special_price ' );
215
+ $ item ->expects ($ this ->any ())
216
+ ->method ('getChildren ' )
217
+ ->willReturn ([$ childItem ]);
218
+
219
+ return $ item ;
220
+ }
235
221
222
+ public function testChildIsNotUsedForValidation ()
223
+ {
224
+ $ item = $ this ->configurableProductTestSetUp ();
225
+ $ item ->expects ($ this ->never ())->method ('setProduct ' );
226
+ $ this ->validator ->setAttribute ('special_price ' );
227
+ $ this ->validator ->setAttributeScope ('parent ' );
236
228
$ this ->validatorPlugin ->beforeValidate ($ this ->validator , $ item );
237
229
}
238
230
@@ -266,7 +258,7 @@ public function testChildIsNotUsedForValidationWhenConfigurableProductIsMissingC
266
258
->willReturn ([]);
267
259
268
260
$ this ->validator ->setAttribute ('special_price ' );
269
-
261
+ $ item -> expects ( $ this -> never ())-> method ( ' setProduct ' );
270
262
$ this ->validatorPlugin ->beforeValidate ($ this ->validator , $ item );
271
263
}
272
264
}
0 commit comments