Skip to content

Commit bb88d3d

Browse files
committed
Fix Data\Collection::each() method
1 parent ada45cc commit bb88d3d

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

lib/internal/Magento/Framework/Data/Collection.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,29 @@ public function walk($callback, array $args = [])
515515
}
516516

517517
/**
518-
* @param string|array $objMethod
518+
* Call method or callback on each item in the collection.
519+
*
520+
* @param string|array|\Closure $objMethod
519521
* @param array $args
520522
* @return void
521523
*/
522524
public function each($objMethod, $args = [])
523525
{
524-
foreach ($args->_items as $k => $item) {
525-
$args->_items[$k] = call_user_func($objMethod, $item);
526+
if ($objMethod instanceof \Closure) {
527+
foreach ($this->getItems() as $item) {
528+
$objMethod($item, ...$args);
529+
}
530+
} elseif (is_array($objMethod)) {
531+
foreach ($this->getItems() as $item) {
532+
call_user_func($objMethod, $item, ...$args);
533+
}
534+
} else {
535+
foreach ($this->getItems() as $item) {
536+
$item->$objMethod(...$args);
537+
}
526538
}
527539
}
528-
540+
529541
/**
530542
* Setting data for all collection items
531543
*

lib/internal/Magento/Framework/Data/Test/Unit/CollectionTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,85 @@ public function testPossibleFlowWithItem()
173173
$this->_model->removeAllItems();
174174
$this->assertEquals([], $this->_model->getItems());
175175
}
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+
}
176257
}

0 commit comments

Comments
 (0)