CollectionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Test\Unit;
  7. /**
  8. * Class for Collection test.
  9. */
  10. class CollectionTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Data\Collection
  14. */
  15. protected $_model;
  16. /**
  17. * Set up.
  18. */
  19. protected function setUp()
  20. {
  21. $this->_model = new \Magento\Framework\Data\Collection(
  22. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  23. );
  24. }
  25. /**
  26. * Test for method removeAllItems.
  27. *
  28. * @return void
  29. */
  30. public function testRemoveAllItems()
  31. {
  32. $this->_model->addItem(new \Magento\Framework\DataObject());
  33. $this->_model->addItem(new \Magento\Framework\DataObject());
  34. $this->assertCount(2, $this->_model->getItems());
  35. $this->_model->removeAllItems();
  36. $this->assertEmpty($this->_model->getItems());
  37. }
  38. /**
  39. * Test loadWithFilter()
  40. *
  41. * @return void
  42. */
  43. public function testLoadWithFilter()
  44. {
  45. $this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $this->_model->loadWithFilter());
  46. $this->assertEmpty($this->_model->getItems());
  47. $this->_model->addItem(new \Magento\Framework\DataObject());
  48. $this->_model->addItem(new \Magento\Framework\DataObject());
  49. $this->assertCount(2, $this->_model->loadWithFilter()->getItems());
  50. }
  51. /**
  52. * Test for method etItemObjectClass
  53. *
  54. * @dataProvider setItemObjectClassDataProvider
  55. */
  56. public function testSetItemObjectClass($class)
  57. {
  58. $this->_model->setItemObjectClass($class);
  59. $this->assertAttributeSame($class, '_itemObjectClass', $this->_model);
  60. }
  61. /**
  62. * Data provider.
  63. *
  64. * @return array
  65. */
  66. public function setItemObjectClassDataProvider()
  67. {
  68. return [[\Magento\Framework\Url::class], [\Magento\Framework\DataObject::class]];
  69. }
  70. /**
  71. * Test for method setItemObjectClass with exception.
  72. *
  73. * @expectedException \InvalidArgumentException
  74. * @expectedExceptionMessage Incorrect_ClassName does not extend \Magento\Framework\DataObject
  75. */
  76. public function testSetItemObjectClassException()
  77. {
  78. $this->_model->setItemObjectClass('Incorrect_ClassName');
  79. }
  80. /**
  81. * Test for method addFilter.
  82. *
  83. * @return void
  84. */
  85. public function testAddFilter()
  86. {
  87. $this->_model->addFilter('field1', 'value');
  88. $this->assertEquals('field1', $this->_model->getFilter('field1')->getData('field'));
  89. }
  90. /**
  91. * Test for method getFilters.
  92. *
  93. * @return void
  94. */
  95. public function testGetFilters()
  96. {
  97. $this->_model->addFilter('field1', 'value');
  98. $this->_model->addFilter('field2', 'value');
  99. $this->assertEquals('field1', $this->_model->getFilter(['field1', 'field2'])[0]->getData('field'));
  100. $this->assertEquals('field2', $this->_model->getFilter(['field1', 'field2'])[1]->getData('field'));
  101. }
  102. /**
  103. * Test for method get non existion filters.
  104. *
  105. * @return void
  106. */
  107. public function testGetNonExistingFilters()
  108. {
  109. $this->assertEmpty($this->_model->getFilter([]));
  110. $this->assertEmpty($this->_model->getFilter('non_existing_filter'));
  111. }
  112. /**
  113. * Test for lag.
  114. *
  115. * @return void
  116. */
  117. public function testFlag()
  118. {
  119. $this->_model->setFlag('flag_name', 'flag_value');
  120. $this->assertEquals('flag_value', $this->_model->getFlag('flag_name'));
  121. $this->assertTrue($this->_model->hasFlag('flag_name'));
  122. $this->assertNull($this->_model->getFlag('non_existing_flag'));
  123. }
  124. /**
  125. * Test for method getCurPage.
  126. *
  127. * @return void
  128. */
  129. public function testGetCurPage()
  130. {
  131. $this->_model->setCurPage(1);
  132. $this->assertEquals(1, $this->_model->getCurPage());
  133. }
  134. /**
  135. * Test for method possibleFlowWithItem.
  136. *
  137. * @return void
  138. */
  139. public function testPossibleFlowWithItem()
  140. {
  141. $firstItemMock = $this->createPartialMock(
  142. \Magento\Framework\DataObject::class,
  143. ['getId', 'getData', 'toArray']
  144. );
  145. $secondItemMock = $this->createPartialMock(
  146. \Magento\Framework\DataObject::class,
  147. ['getId', 'getData', 'toArray']
  148. );
  149. $requiredFields = ['required_field_one', 'required_field_two'];
  150. $arrItems = [
  151. 'totalRecords' => 1,
  152. 'items' => [
  153. 0 => 'value',
  154. ],
  155. ];
  156. $items = [
  157. 'item_id' => $firstItemMock,
  158. 0 => $secondItemMock,
  159. ];
  160. $firstItemMock->expects($this->exactly(2))->method('getId')->will($this->returnValue('item_id'));
  161. $firstItemMock
  162. ->expects($this->atLeastOnce())
  163. ->method('getData')
  164. ->with('colName')
  165. ->will($this->returnValue('first_value'));
  166. $secondItemMock
  167. ->expects($this->atLeastOnce())
  168. ->method('getData')
  169. ->with('colName')
  170. ->will($this->returnValue('second_value'));
  171. $firstItemMock
  172. ->expects($this->once())
  173. ->method('toArray')
  174. ->with($requiredFields)
  175. ->will($this->returnValue('value'));
  176. /** add items and set them values */
  177. $this->_model->addItem($firstItemMock);
  178. $this->assertEquals($arrItems, $this->_model->toArray($requiredFields));
  179. $this->_model->addItem($secondItemMock);
  180. $this->_model->setDataToAll('column', 'value');
  181. /** get items by column name */
  182. $this->assertEquals(['first_value', 'second_value'], $this->_model->getColumnValues('colName'));
  183. $this->assertEquals([$secondItemMock], $this->_model->getItemsByColumnValue('colName', 'second_value'));
  184. $this->assertEquals($firstItemMock, $this->_model->getItemByColumnValue('colName', 'second_value'));
  185. $this->assertEquals([], $this->_model->getItemsByColumnValue('colName', 'non_existing_value'));
  186. $this->assertEquals(null, $this->_model->getItemByColumnValue('colName', 'non_existing_value'));
  187. /** get items */
  188. $this->assertEquals(['item_id', 0], $this->_model->getAllIds());
  189. $this->assertEquals($firstItemMock, $this->_model->getFirstItem());
  190. $this->assertEquals($secondItemMock, $this->_model->getLastItem());
  191. $this->assertEquals($items, $this->_model->getItems('item_id'));
  192. /** remove existing items */
  193. $this->assertNull($this->_model->getItemById('not_existing_item_id'));
  194. $this->_model->removeItemByKey('item_id');
  195. $this->assertEquals([$secondItemMock], $this->_model->getItems());
  196. $this->_model->removeAllItems();
  197. $this->assertEquals([], $this->_model->getItems());
  198. }
  199. /**
  200. * Test for method eachCallsMethodOnEachItemWithNoArgs.
  201. *
  202. * @return void
  203. */
  204. public function testEachCallsMethodOnEachItemWithNoArgs()
  205. {
  206. for ($i = 0; $i < 3; $i++) {
  207. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
  208. $item->expects($this->once())->method('testCallback')->with();
  209. $this->_model->addItem($item);
  210. }
  211. $this->_model->each('testCallback');
  212. }
  213. /**
  214. * Test for method eachCallsMethodOnEachItemWithArgs.
  215. *
  216. * @return void
  217. */
  218. public function testEachCallsMethodOnEachItemWithArgs()
  219. {
  220. for ($i = 0; $i < 3; $i++) {
  221. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
  222. $item->expects($this->once())->method('testCallback')->with('a', 'b', 'c');
  223. $this->_model->addItem($item);
  224. }
  225. $this->_model->each('testCallback', ['a', 'b', 'c']);
  226. }
  227. /**
  228. * Test for method callsClosureWithEachItemAndNoArgs.
  229. *
  230. * @return void
  231. */
  232. public function testCallsClosureWithEachItemAndNoArgs()
  233. {
  234. for ($i = 0; $i < 3; $i++) {
  235. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
  236. $item->expects($this->once())->method('testCallback')->with();
  237. $this->_model->addItem($item);
  238. }
  239. $this->_model->each(function ($item) {
  240. $item->testCallback();
  241. });
  242. }
  243. /**
  244. * Test for method callsClosureWithEachItemAndArgs.
  245. *
  246. * @return void
  247. */
  248. public function testCallsClosureWithEachItemAndArgs()
  249. {
  250. for ($i = 0; $i < 3; $i++) {
  251. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
  252. $item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
  253. $this->_model->addItem($item);
  254. }
  255. $this->_model->each(function ($item, ...$args) {
  256. $item->testItemCallback(...$args);
  257. }, ['a', 'b', 'c']);
  258. }
  259. /**
  260. * Test for method callsCallableArrayWithEachItemNoArgs.
  261. *
  262. * @return void
  263. */
  264. public function testCallsCallableArrayWithEachItemNoArgs()
  265. {
  266. $mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
  267. ->setMethods(['testObjCallback'])
  268. ->getMock();
  269. $mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
  270. $item->testItemCallback(...$args);
  271. });
  272. for ($i = 0; $i < 3; $i++) {
  273. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
  274. $item->expects($this->once())->method('testItemCallback')->with();
  275. $this->_model->addItem($item);
  276. }
  277. $this->_model->each([$mockCallbackObject, 'testObjCallback']);
  278. }
  279. /**
  280. * Test for method callsCallableArrayWithEachItemAndArgs.
  281. *
  282. * @return void
  283. */
  284. public function testCallsCallableArrayWithEachItemAndArgs()
  285. {
  286. $mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
  287. ->setMethods(['testObjCallback'])
  288. ->getMock();
  289. $mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
  290. $item->testItemCallback(...$args);
  291. });
  292. for ($i = 0; $i < 3; $i++) {
  293. $item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
  294. $item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
  295. $this->_model->addItem($item);
  296. }
  297. $callback = [$mockCallbackObject, 'testObjCallback'];
  298. $this->_model->each($callback, ['a', 'b', 'c']);
  299. }
  300. }