Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectionTest.php
Go to the documentation of this file.
1 <?php
8 
11 
16 class CollectionTest extends \PHPUnit\Framework\TestCase
17 {
21  private $collection;
22 
23  public function setUp()
24  {
25  $factoryMock = $this->createMock(EntityFactoryInterface::class);
26  $this->collection = new Collection($factoryMock);
27  }
28 
33  public function testWalk()
34  {
35  $objOne = new \Magento\Framework\DataObject(['id' => 1, 'name' => 'one']);
36  $objTwo = new \Magento\Framework\DataObject(['id' => 2, 'name' => 'two']);
37  $objThree = new \Magento\Framework\DataObject(['id' => 3, 'name' => 'three']);
38 
39  $this->collection->addItem($objOne);
40  $this->collection->addItem($objTwo);
41  $this->collection->addItem($objThree);
42 
43  $this->assertEquals([1, 2, 3], $this->collection->getAllIds(), 'Items added incorrectly to the collection');
44  $this->collection->walk([$this, 'modifyObjectNames'], ['test prefix']);
45 
46  $this->assertEquals([1, 2, 3], $this->collection->getAllIds(), 'Incorrect IDs after callback function');
47  $expectedNames = [
48  'test prefix one',
49  'test prefix two',
50  'test prefix three'
51  ];
52 
53  $this->assertEquals(
54  $expectedNames,
55  $this->collection->getColumnValues('name'),
56  'Incorrect Names after callback function'
57  );
58  }
59 
66  public function modifyObjectNames(\Magento\Framework\DataObject $object, $prefix)
67  {
68  $object->setData('name', $prefix . ' ' . $object->getData('name'));
69  }
70 }
$prefix
Definition: name.phtml:25
modifyObjectNames(\Magento\Framework\DataObject $object, $prefix)