AbstractCollectionTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity\Collection;
  7. /**
  8. * AbstractCollection test
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class AbstractCollectionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var AbstractCollectionStub|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $model;
  18. /**
  19. * @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $coreEntityFactoryMock;
  22. /**
  23. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $loggerMock;
  26. /**
  27. * @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $fetchStrategyMock;
  30. /**
  31. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $eventManagerMock;
  34. /**
  35. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $configMock;
  38. /**
  39. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $coreResourceMock;
  42. /**
  43. * @var \Magento\Eav\Model\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $entityFactoryMock;
  46. /**
  47. * @var \Magento\Eav\Model\ResourceModel\Helper|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $resourceHelperMock;
  50. /**
  51. * @var \Magento\Framework\Validator\UniversalFactory|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $validatorFactoryMock;
  54. /**
  55. * @var \Magento\Framework\DB\Statement\Pdo\Mysql|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $statementMock;
  58. protected function setUp()
  59. {
  60. $this->coreEntityFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
  61. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  62. $this->fetchStrategyMock = $this->createMock(
  63. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface::class
  64. );
  65. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  66. $this->configMock = $this->createMock(\Magento\Eav\Model\Config::class);
  67. $this->coreResourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  68. $this->resourceHelperMock = $this->createMock(\Magento\Eav\Model\ResourceModel\Helper::class);
  69. $this->validatorFactoryMock = $this->createMock(\Magento\Framework\Validator\UniversalFactory::class);
  70. $this->entityFactoryMock = $this->createMock(\Magento\Eav\Model\EntityFactory::class);
  71. /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
  72. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  73. $this->statementMock = $this->createPartialMock(\Magento\Framework\DB\Statement\Pdo\Mysql::class, ['fetch']);
  74. /** @var $selectMock \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
  75. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  76. $this->coreEntityFactoryMock->expects(
  77. $this->any()
  78. )->method(
  79. 'create'
  80. )->will(
  81. $this->returnCallback([$this, 'getMagentoObject'])
  82. );
  83. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  84. $connectionMock->expects($this->any())->method('query')->willReturn($this->statementMock);
  85. $this->coreResourceMock->expects(
  86. $this->any()
  87. )->method(
  88. 'getConnection'
  89. )->will(
  90. $this->returnValue($connectionMock)
  91. );
  92. $entityMock = $this->createMock(\Magento\Eav\Model\Entity\AbstractEntity::class);
  93. $entityMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
  94. $entityMock->expects($this->any())->method('getDefaultAttributes')->will($this->returnValue([]));
  95. $this->validatorFactoryMock->expects(
  96. $this->any()
  97. )->method(
  98. 'create'
  99. )->with(
  100. 'test_entity_model' // see \Magento\Eav\Test\Unit\Model\Entity\Collection\AbstractCollectionStub
  101. )->will(
  102. $this->returnValue($entityMock)
  103. );
  104. $this->model = new AbstractCollectionStub(
  105. $this->coreEntityFactoryMock,
  106. $this->loggerMock,
  107. $this->fetchStrategyMock,
  108. $this->eventManagerMock,
  109. $this->configMock,
  110. $this->coreResourceMock,
  111. $this->entityFactoryMock,
  112. $this->resourceHelperMock,
  113. $this->validatorFactoryMock,
  114. null
  115. );
  116. }
  117. public function tearDown()
  118. {
  119. $this->model = null;
  120. }
  121. /**
  122. * Test method \Magento\Eav\Model\Entity\Collection\AbstractCollection::load
  123. */
  124. public function testLoad()
  125. {
  126. $this->fetchStrategyMock
  127. ->expects($this->once())
  128. ->method('fetchAll')
  129. ->will($this->returnValue([['id' => 1, 'data_changes' => true], ['id' => 2]]));
  130. foreach ($this->model->getItems() as $item) {
  131. $this->assertFalse($item->getDataChanges());
  132. }
  133. }
  134. /**
  135. * @dataProvider getItemsDataProvider
  136. */
  137. public function testClear($values, $count)
  138. {
  139. $this->fetchStrategyMock->expects($this->once())->method('fetchAll')->will($this->returnValue($values));
  140. $testId = array_pop($values)['id'];
  141. $this->assertCount($count, $this->model->getItems());
  142. $this->assertNotNull($this->model->getItemById($testId));
  143. $this->model->clear();
  144. $this->assertNull($this->model->getItemById($testId));
  145. }
  146. /**
  147. * @dataProvider getItemsDataProvider
  148. */
  149. public function testRemoveAllItems($values, $count)
  150. {
  151. $this->fetchStrategyMock->expects($this->once())->method('fetchAll')->will($this->returnValue($values));
  152. $testId = array_pop($values)['id'];
  153. $this->assertCount($count, $this->model->getItems());
  154. $this->assertNotNull($this->model->getItemById($testId));
  155. $this->model->removeAllItems();
  156. $this->assertNull($this->model->getItemById($testId));
  157. }
  158. /**
  159. * @dataProvider getItemsDataProvider
  160. */
  161. public function testRemoveItemByKey($values, $count)
  162. {
  163. $this->fetchStrategyMock->expects($this->once())->method('fetchAll')->will($this->returnValue($values));
  164. $testId = array_pop($values)['id'];
  165. $this->assertCount($count, $this->model->getItems());
  166. $this->assertNotNull($this->model->getItemById($testId));
  167. $this->model->removeItemByKey($testId);
  168. $this->assertCount($count - 1, $this->model->getItems());
  169. $this->assertNull($this->model->getItemById($testId));
  170. }
  171. /**
  172. * @return array
  173. */
  174. public function getItemsDataProvider()
  175. {
  176. return [
  177. ['values' => [['id' => 1]], 'count' => 1],
  178. ['values' => [['id' => 1], ['id' => 2]], 'count' => 2],
  179. ['values' => [['id' => 2], ['id' => 3]], 'count' => 2]
  180. ];
  181. }
  182. /**
  183. * @return \Magento\Framework\DataObject
  184. */
  185. public function getMagentoObject()
  186. {
  187. return new \Magento\Framework\DataObject();
  188. }
  189. }