Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Framework\Data\Collection::each() method #7330

Merged
merged 1 commit into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/internal/Magento/Framework/Data/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,17 +515,29 @@ public function walk($callback, array $args = [])
}

/**
* @param string|array $objMethod
* Call method or callback on each item in the collection.
*
* @param string|array|\Closure $objMethod
* @param array $args
* @return void
*/
public function each($objMethod, $args = [])
{
foreach ($args->_items as $k => $item) {
$args->_items[$k] = call_user_func($objMethod, $item);
if ($objMethod instanceof \Closure) {
foreach ($this->getItems() as $item) {
$objMethod($item, ...$args);
}
} elseif (is_array($objMethod)) {
foreach ($this->getItems() as $item) {
call_user_func($objMethod, $item, ...$args);
}
} else {
foreach ($this->getItems() as $item) {
$item->$objMethod(...$args);
}
}
}

/**
* Setting data for all collection items
*
Expand Down
81 changes: 81 additions & 0 deletions lib/internal/Magento/Framework/Data/Test/Unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,85 @@ public function testPossibleFlowWithItem()
$this->_model->removeAllItems();
$this->assertEquals([], $this->_model->getItems());
}

public function testEachCallsMethodOnEachItemWithNoArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with();
$this->_model->addItem($item);
}
$this->_model->each('testCallback');
}

public function testEachCallsMethodOnEachItemWithArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}
$this->_model->each('testCallback', ['a', 'b', 'c']);
}

public function testCallsClosureWithEachItemAndNoArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with();
$this->_model->addItem($item);
}
$this->_model->each(function ($item) {
$item->testCallback();
});
}

public function testCallsClosureWithEachItemAndArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}
$this->_model->each(function ($item, ...$args) {
$item->testItemCallback(...$args);
}, ['a', 'b', 'c']);
}

public function testCallsCallableArrayWithEachItemNoArgs()
{
$mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
->setMethods(['testObjCallback'])
->getMock();
$mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
$item->testItemCallback(...$args);
});

for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with();
$this->_model->addItem($item);
}

$this->_model->each([$mockCallbackObject, 'testObjCallback']);
}

public function testCallsCallableArrayWithEachItemAndArgs()
{
$mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
->setMethods(['testObjCallback'])
->getMock();
$mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
$item->testItemCallback(...$args);
});

for ($i = 0; $i < 3; $i++) {
$item = $this->getMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}

$callback = [$mockCallbackObject, 'testObjCallback'];
$this->_model->each($callback, ['a', 'b', 'c']);
}
}