CollectionTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Test\Unit\Model\Indexer;
  7. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  8. use Magento\Framework\Indexer\ConfigInterface;
  9. use Magento\Framework\Indexer\IndexerInterface;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  11. use Magento\Indexer\Model\Indexer\Collection;
  12. use Magento\Indexer\Model\Indexer\State;
  13. use Magento\Indexer\Model\ResourceModel\Indexer\State\Collection as StateCollection;
  14. use Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory;
  15. class CollectionTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var ObjectManagerHelper
  19. */
  20. private $objectManagerHelper;
  21. /**
  22. * @var Collection
  23. */
  24. private $collection;
  25. /**
  26. * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $configMock;
  29. /**
  30. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $statesFactoryMock;
  33. /**
  34. * @var EntityFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $entityFactoryMock;
  37. /**
  38. * @return void
  39. */
  40. protected function setUp()
  41. {
  42. $this->objectManagerHelper = new ObjectManagerHelper($this);
  43. $this->configMock = $this->getMockBuilder(ConfigInterface::class)
  44. ->getMockForAbstractClass();
  45. $this->statesFactoryMock = $this->getMockBuilder(CollectionFactory::class)
  46. ->setMethods(['create'])
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->entityFactoryMock = $this->getMockBuilder(EntityFactoryInterface::class)
  50. ->getMock();
  51. $this->collection = $this->objectManagerHelper->getObject(
  52. Collection::class,
  53. [
  54. 'entityFactory' => $this->entityFactoryMock,
  55. 'config' => $this->configMock,
  56. 'statesFactory' => $this->statesFactoryMock,
  57. ]
  58. );
  59. }
  60. /**
  61. * @param array $indexersData
  62. * @param array $states
  63. * @dataProvider loadDataDataProvider
  64. */
  65. public function testLoadData(array $indexersData, array $states)
  66. {
  67. $statesCollection = $this->getMockBuilder(StateCollection::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->statesFactoryMock
  71. ->expects($this->once())
  72. ->method('create')
  73. ->willReturn($statesCollection);
  74. $statesCollection->method('getItems')
  75. ->willReturn($states);
  76. $calls = [];
  77. foreach ($indexersData as $indexerId => $indexerData) {
  78. $indexer = $this->getIndexerMock($indexerData);
  79. $state = $states[$indexerId] ?? '';
  80. $indexer
  81. ->expects($this->once())
  82. ->method('load')
  83. ->with($indexerId);
  84. $indexer
  85. ->expects($this->exactly($state ? 1: 0))
  86. ->method('setState')
  87. ->with($state);
  88. $calls[] = $indexer;
  89. }
  90. $this->configMock
  91. ->method('getIndexers')
  92. ->willReturn($indexersData);
  93. $this->entityFactoryMock
  94. ->method('create')
  95. ->willReturnOnConsecutiveCalls(...$calls);
  96. $this->assertFalse((bool)$this->collection->isLoaded());
  97. $this->assertInstanceOf(Collection::class, $this->collection->loadData());
  98. $itemIds = [];
  99. foreach ($this->collection->getItems() as $item) {
  100. $itemIds[] = $item->getId();
  101. }
  102. $this->assertEmpty(array_diff($itemIds, array_keys($indexersData)));
  103. $this->assertTrue($this->collection->isLoaded());
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function loadDataDataProvider()
  109. {
  110. return [
  111. [
  112. 'indexers' => [
  113. 'indexer_2' => [
  114. 'indexer_id' => 'indexer_2',
  115. ],
  116. 'indexer_3' => [
  117. 'indexer_id' => 'indexer_3',
  118. ],
  119. 'indexer_1' => [
  120. 'indexer_id' => 'indexer_1',
  121. ],
  122. ],
  123. 'states' => [
  124. 'indexer_2' => $this->getStateMock(['indexer_id' => 'indexer_2']),
  125. 'indexer_3' => $this->getStateMock(['indexer_id' => 'indexer_3']),
  126. ],
  127. ]
  128. ];
  129. }
  130. /**
  131. * @param array $indexersData
  132. * @dataProvider getAllIdsDataProvider
  133. */
  134. public function testGetAllIds(array $indexersData)
  135. {
  136. $statesCollection = $this->getMockBuilder(StateCollection::class)
  137. ->disableOriginalConstructor()
  138. ->getMock();
  139. $this->statesFactoryMock
  140. ->expects($this->once())
  141. ->method('create')
  142. ->willReturn($statesCollection);
  143. $statesCollection->method('getItems')
  144. ->willReturn([]);
  145. $calls = [];
  146. foreach ($indexersData as $indexerData) {
  147. $calls[] = $this->getIndexerMock($indexerData);
  148. }
  149. $this->configMock
  150. ->method('getIndexers')
  151. ->willReturn($indexersData);
  152. $this->entityFactoryMock
  153. ->method('create')
  154. ->willReturnOnConsecutiveCalls(...$calls);
  155. $this->assertEmpty(array_diff($this->collection->getAllIds(), array_keys($indexersData)));
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function getAllIdsDataProvider()
  161. {
  162. return [
  163. [
  164. 'indexers' => [
  165. 'indexer_2' => [
  166. 'indexer_id' => 'indexer_2',
  167. ],
  168. 'indexer_3' => [
  169. 'indexer_id' => 'indexer_3',
  170. ],
  171. 'indexer_1' => [
  172. 'indexer_id' => 'indexer_1',
  173. ],
  174. ],
  175. ]
  176. ];
  177. }
  178. /**
  179. * @param string $methodName
  180. * @param array $arguments
  181. * @dataProvider stubMethodsDataProvider
  182. */
  183. public function testStubMethods(string $methodName, array $arguments)
  184. {
  185. $this->statesFactoryMock
  186. ->expects($this->never())
  187. ->method('create');
  188. $collection = $this->objectManagerHelper->getObject(
  189. Collection::class,
  190. [
  191. 'entityFactory' => $this->entityFactoryMock,
  192. 'config' => $this->configMock,
  193. 'statesFactory' => $this->statesFactoryMock,
  194. '_items' => [$this->getIndexerMock()],
  195. ]
  196. );
  197. $this->assertEmpty($collection->{$methodName}(...$arguments));
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function stubMethodsDataProvider()
  203. {
  204. return [
  205. [
  206. 'getColumnValues',
  207. ['colName'],
  208. ],
  209. [
  210. 'getItemsByColumnValue',
  211. ['colName', 'value']
  212. ],
  213. [
  214. 'getItemByColumnValue',
  215. ['colName', 'value']
  216. ],
  217. [
  218. 'toXml',
  219. []
  220. ],
  221. [
  222. 'toArray',
  223. []
  224. ],
  225. [
  226. 'toOptionArray',
  227. []
  228. ],
  229. [
  230. 'toOptionHash',
  231. []
  232. ],
  233. ];
  234. }
  235. /**
  236. * @param string $methodName
  237. * @param array $arguments
  238. * @dataProvider stubMethodsWithReturnSelfDataProvider
  239. */
  240. public function testStubMethodsWithReturnSelf(string $methodName, array $arguments)
  241. {
  242. $this->statesFactoryMock
  243. ->expects($this->never())
  244. ->method('create');
  245. $collection = $this->objectManagerHelper->getObject(
  246. Collection::class,
  247. [
  248. 'entityFactory' => $this->entityFactoryMock,
  249. 'config' => $this->configMock,
  250. 'statesFactory' => $this->statesFactoryMock,
  251. '_items' => [$this->getIndexerMock()],
  252. ]
  253. );
  254. $this->assertInstanceOf(Collection::class, $collection->{$methodName}(...$arguments));
  255. }
  256. /**
  257. * @return array
  258. */
  259. public function stubMethodsWithReturnSelfDataProvider()
  260. {
  261. return [
  262. [
  263. 'setDataToAll',
  264. ['colName', 'value']
  265. ],
  266. [
  267. 'setItemObjectClass',
  268. ['notValidClassName']
  269. ],
  270. ];
  271. }
  272. /**
  273. * @return \PHPUnit_Framework_MockObject_MockObject|IndexerInterface
  274. */
  275. private function getIndexerMock(array $data = [])
  276. {
  277. /** @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface $indexer */
  278. $indexer = $this->getMockBuilder(IndexerInterface::class)
  279. ->getMockForAbstractClass();
  280. if (isset($data['indexer_id'])) {
  281. $indexer->method('getId')
  282. ->willReturn($data['indexer_id']);
  283. }
  284. return $indexer;
  285. }
  286. /**
  287. * @param array $data
  288. * @return \PHPUnit_Framework_MockObject_MockObject|State
  289. */
  290. private function getStateMock(array $data = [])
  291. {
  292. /** @var \PHPUnit_Framework_MockObject_MockObject|State $state */
  293. $state = $this->getMockBuilder(State::class)
  294. ->disableOriginalConstructor()
  295. ->getMock();
  296. if (isset($data['indexer_id'])) {
  297. $state->method('getIndexerId')
  298. ->willReturn($data['indexer_id']);
  299. }
  300. return $state;
  301. }
  302. }