CollectionTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit\Data;
  7. use Magento\Framework\Data\Collection;
  8. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  9. /**
  10. * Class CollectionTest
  11. * @package Magento\Framework\Test\Unit\Data
  12. */
  13. class CollectionTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Collection
  17. */
  18. private $collection;
  19. public function setUp()
  20. {
  21. $factoryMock = $this->createMock(EntityFactoryInterface::class);
  22. $this->collection = new Collection($factoryMock);
  23. }
  24. /**
  25. * Test that callback works correctly for all items in collection.
  26. * @see https://github.com/magento/magento2/pull/5742
  27. */
  28. public function testWalk()
  29. {
  30. $objOne = new \Magento\Framework\DataObject(['id' => 1, 'name' => 'one']);
  31. $objTwo = new \Magento\Framework\DataObject(['id' => 2, 'name' => 'two']);
  32. $objThree = new \Magento\Framework\DataObject(['id' => 3, 'name' => 'three']);
  33. $this->collection->addItem($objOne);
  34. $this->collection->addItem($objTwo);
  35. $this->collection->addItem($objThree);
  36. $this->assertEquals([1, 2, 3], $this->collection->getAllIds(), 'Items added incorrectly to the collection');
  37. $this->collection->walk([$this, 'modifyObjectNames'], ['test prefix']);
  38. $this->assertEquals([1, 2, 3], $this->collection->getAllIds(), 'Incorrect IDs after callback function');
  39. $expectedNames = [
  40. 'test prefix one',
  41. 'test prefix two',
  42. 'test prefix three'
  43. ];
  44. $this->assertEquals(
  45. $expectedNames,
  46. $this->collection->getColumnValues('name'),
  47. 'Incorrect Names after callback function'
  48. );
  49. }
  50. /**
  51. * Callback function.
  52. *
  53. * @param \Magento\Framework\DataObject $object
  54. * @param string $prefix
  55. */
  56. public function modifyObjectNames(\Magento\Framework\DataObject $object, $prefix)
  57. {
  58. $object->setData('name', $prefix . ' ' . $object->getData('name'));
  59. }
  60. }