@@ -173,4 +173,85 @@ public function testPossibleFlowWithItem()
173
173
$ this ->_model ->removeAllItems ();
174
174
$ this ->assertEquals ([], $ this ->_model ->getItems ());
175
175
}
176
+
177
+ public function testEachCallsMethodOnEachItemWithNoArgs ()
178
+ {
179
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
180
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testCallback ' ]);
181
+ $ item ->expects ($ this ->once ())->method ('testCallback ' )->with ();
182
+ $ this ->_model ->addItem ($ item );
183
+ }
184
+ $ this ->_model ->each ('testCallback ' );
185
+ }
186
+
187
+ public function testEachCallsMethodOnEachItemWithArgs ()
188
+ {
189
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
190
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testCallback ' ]);
191
+ $ item ->expects ($ this ->once ())->method ('testCallback ' )->with ('a ' , 'b ' , 'c ' );
192
+ $ this ->_model ->addItem ($ item );
193
+ }
194
+ $ this ->_model ->each ('testCallback ' , ['a ' , 'b ' , 'c ' ]);
195
+ }
196
+
197
+ public function testCallsClosureWithEachItemAndNoArgs ()
198
+ {
199
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
200
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testCallback ' ]);
201
+ $ item ->expects ($ this ->once ())->method ('testCallback ' )->with ();
202
+ $ this ->_model ->addItem ($ item );
203
+ }
204
+ $ this ->_model ->each (function ($ item ) {
205
+ $ item ->testCallback ();
206
+ });
207
+ }
208
+
209
+ public function testCallsClosureWithEachItemAndArgs ()
210
+ {
211
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
212
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testItemCallback ' ]);
213
+ $ item ->expects ($ this ->once ())->method ('testItemCallback ' )->with ('a ' , 'b ' , 'c ' );
214
+ $ this ->_model ->addItem ($ item );
215
+ }
216
+ $ this ->_model ->each (function ($ item , ...$ args ) {
217
+ $ item ->testItemCallback (...$ args );
218
+ }, ['a ' , 'b ' , 'c ' ]);
219
+ }
220
+
221
+ public function testCallsCallableArrayWithEachItemNoArgs ()
222
+ {
223
+ $ mockCallbackObject = $ this ->getMockBuilder ('DummyEachCallbackInstance ' )
224
+ ->setMethods (['testObjCallback ' ])
225
+ ->getMock ();
226
+ $ mockCallbackObject ->method ('testObjCallback ' )->willReturnCallback (function ($ item , ...$ args ) {
227
+ $ item ->testItemCallback (...$ args );
228
+ });
229
+
230
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
231
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testItemCallback ' ]);
232
+ $ item ->expects ($ this ->once ())->method ('testItemCallback ' )->with ();
233
+ $ this ->_model ->addItem ($ item );
234
+ }
235
+
236
+ $ this ->_model ->each ([$ mockCallbackObject , 'testObjCallback ' ]);
237
+ }
238
+
239
+ public function testCallsCallableArrayWithEachItemAndArgs ()
240
+ {
241
+ $ mockCallbackObject = $ this ->getMockBuilder ('DummyEachCallbackInstance ' )
242
+ ->setMethods (['testObjCallback ' ])
243
+ ->getMock ();
244
+ $ mockCallbackObject ->method ('testObjCallback ' )->willReturnCallback (function ($ item , ...$ args ) {
245
+ $ item ->testItemCallback (...$ args );
246
+ });
247
+
248
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
249
+ $ item = $ this ->getMock (\Magento \Framework \DataObject::class, ['testItemCallback ' ]);
250
+ $ item ->expects ($ this ->once ())->method ('testItemCallback ' )->with ('a ' , 'b ' , 'c ' );
251
+ $ this ->_model ->addItem ($ item );
252
+ }
253
+
254
+ $ callback = [$ mockCallbackObject , 'testObjCallback ' ];
255
+ $ this ->_model ->each ($ callback , ['a ' , 'b ' , 'c ' ]);
256
+ }
176
257
}
0 commit comments