IndexerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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;
  7. use Magento\Framework\Indexer\StateInterface;
  8. class IndexerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Indexer\Model\Indexer|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $model;
  14. /**
  15. * @var \Magento\Framework\Indexer\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $configMock;
  18. /**
  19. * @var \Magento\Framework\Indexer\ActionFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $actionFactoryMock;
  22. /**
  23. * @var \Magento\Framework\Mview\ViewInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $viewMock;
  26. /**
  27. * @var \Magento\Indexer\Model\Indexer\StateFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $stateFactoryMock;
  30. /**
  31. * @var \Magento\Indexer\Model\Indexer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $indexFactoryMock;
  34. protected function setUp()
  35. {
  36. $this->configMock = $this->getMockForAbstractClass(
  37. \Magento\Framework\Indexer\ConfigInterface::class,
  38. [],
  39. '',
  40. false,
  41. false,
  42. true,
  43. ['getIndexer']
  44. );
  45. $this->actionFactoryMock = $this->createPartialMock(
  46. \Magento\Framework\Indexer\ActionFactory::class,
  47. ['create']
  48. );
  49. $this->viewMock = $this->getMockForAbstractClass(
  50. \Magento\Framework\Mview\ViewInterface::class,
  51. [],
  52. '',
  53. false,
  54. false,
  55. true,
  56. ['load', 'isEnabled', 'getUpdated', 'getStatus', '__wakeup', 'getId', 'suspend', 'resume']
  57. );
  58. $this->stateFactoryMock = $this->createPartialMock(
  59. \Magento\Indexer\Model\Indexer\StateFactory::class,
  60. ['create']
  61. );
  62. $this->indexFactoryMock = $this->createPartialMock(
  63. \Magento\Indexer\Model\Indexer\CollectionFactory::class,
  64. ['create']
  65. );
  66. $structureFactory = $this->getMockBuilder(\Magento\Framework\Indexer\StructureFactory::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods(['create'])
  69. ->getMock();
  70. /** @var \Magento\Framework\Indexer\StructureFactory $structureFactory */
  71. $this->model = new \Magento\Indexer\Model\Indexer(
  72. $this->configMock,
  73. $this->actionFactoryMock,
  74. $structureFactory,
  75. $this->viewMock,
  76. $this->stateFactoryMock,
  77. $this->indexFactoryMock
  78. );
  79. }
  80. /**
  81. * @expectedException \InvalidArgumentException
  82. * @expectedExceptionMessage indexer_id indexer does not exist.
  83. */
  84. public function testLoadWithException()
  85. {
  86. $indexId = 'indexer_id';
  87. $this->configMock->expects(
  88. $this->once()
  89. )->method(
  90. 'getIndexer'
  91. )->with(
  92. $indexId
  93. )->will(
  94. $this->returnValue($this->getIndexerData())
  95. );
  96. $this->model->load($indexId);
  97. }
  98. public function testGetView()
  99. {
  100. $indexId = 'indexer_internal_name';
  101. $this->viewMock->expects($this->once())->method('load')->with('view_test')->will($this->returnSelf());
  102. $this->loadIndexer($indexId);
  103. $this->assertEquals($this->viewMock, $this->model->getView());
  104. }
  105. public function testGetState()
  106. {
  107. $indexId = 'indexer_internal_name';
  108. $stateMock = $this->createPartialMock(
  109. \Magento\Indexer\Model\Indexer\State::class,
  110. ['loadByIndexer', 'getId', '__wakeup']
  111. );
  112. $stateMock->expects($this->once())->method('loadByIndexer')->with($indexId)->will($this->returnSelf());
  113. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  114. $this->loadIndexer($indexId);
  115. $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
  116. }
  117. /**
  118. * @param bool $getViewIsEnabled
  119. * @param string $getViewGetUpdated
  120. * @param string $getStateGetUpdated
  121. * @dataProvider getLatestUpdatedDataProvider
  122. */
  123. public function testGetLatestUpdated($getViewIsEnabled, $getViewGetUpdated, $getStateGetUpdated)
  124. {
  125. $indexId = 'indexer_internal_name';
  126. $this->loadIndexer($indexId);
  127. $this->viewMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  128. $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($getViewIsEnabled));
  129. $this->viewMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getViewGetUpdated));
  130. $stateMock = $this->createPartialMock(
  131. \Magento\Indexer\Model\Indexer\State::class,
  132. ['load', 'getId', 'setIndexerId', '__wakeup', 'getUpdated']
  133. );
  134. $stateMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getStateGetUpdated));
  135. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  136. if ($getViewIsEnabled && $getViewGetUpdated) {
  137. if (!$getStateGetUpdated) {
  138. $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
  139. } else {
  140. if ($getViewGetUpdated == $getStateGetUpdated) {
  141. $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
  142. } else {
  143. $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
  144. }
  145. }
  146. } else {
  147. $this->assertEquals($getStateGetUpdated, $this->model->getLatestUpdated());
  148. }
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function getLatestUpdatedDataProvider()
  154. {
  155. return [
  156. [false, '06-Jan-1944', '06-Jan-1944'],
  157. [false, '', '06-Jan-1944'],
  158. [false, '06-Jan-1944', ''],
  159. [false, '', ''],
  160. [true, '06-Jan-1944', '06-Jan-1944'],
  161. [true, '', '06-Jan-1944'],
  162. [true, '06-Jan-1944', ''],
  163. [true, '', ''],
  164. [true, '06-Jan-1944', '05-Jan-1944']
  165. ];
  166. }
  167. public function testReindexAll()
  168. {
  169. $indexId = 'indexer_internal_name';
  170. $this->loadIndexer($indexId);
  171. $stateMock = $this->createPartialMock(
  172. \Magento\Indexer\Model\Indexer\State::class,
  173. ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
  174. );
  175. $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
  176. $stateMock->expects($this->never())->method('setIndexerId');
  177. $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  178. $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
  179. $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
  180. $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
  181. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  182. $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
  183. $this->viewMock->expects($this->once())->method('suspend');
  184. $this->viewMock->expects($this->once())->method('resume');
  185. $actionMock = $this->createPartialMock(
  186. \Magento\Framework\Indexer\ActionInterface::class,
  187. ['executeFull', 'executeList', 'executeRow']
  188. );
  189. $this->actionFactoryMock->expects(
  190. $this->once()
  191. )->method(
  192. 'create'
  193. )->with(
  194. 'Some\Class\Name'
  195. )->will(
  196. $this->returnValue($actionMock)
  197. );
  198. $this->model->reindexAll();
  199. }
  200. /**
  201. * @expectedException \Exception
  202. * @expectedExceptionMessage Test exception
  203. */
  204. public function testReindexAllWithException()
  205. {
  206. $indexId = 'indexer_internal_name';
  207. $this->loadIndexer($indexId);
  208. $stateMock = $this->createPartialMock(
  209. \Magento\Indexer\Model\Indexer\State::class,
  210. ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
  211. );
  212. $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
  213. $stateMock->expects($this->never())->method('setIndexerId');
  214. $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  215. $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
  216. $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
  217. $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
  218. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  219. $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
  220. $this->viewMock->expects($this->never())->method('suspend');
  221. $this->viewMock->expects($this->once())->method('resume');
  222. $actionMock = $this->createPartialMock(
  223. \Magento\Framework\Indexer\ActionInterface::class,
  224. ['executeFull', 'executeList', 'executeRow']
  225. );
  226. $actionMock->expects($this->once())->method('executeFull')->will(
  227. $this->returnCallback(
  228. function () {
  229. throw new \Exception('Test exception');
  230. }
  231. )
  232. );
  233. $this->actionFactoryMock->expects(
  234. $this->once()
  235. )->method(
  236. 'create'
  237. )->with(
  238. 'Some\Class\Name'
  239. )->will(
  240. $this->returnValue($actionMock)
  241. );
  242. $this->model->reindexAll();
  243. }
  244. /**
  245. * @expectedException \Error
  246. * @expectedExceptionMessage Test Engine Error
  247. */
  248. public function testReindexAllWithError()
  249. {
  250. $indexId = 'indexer_internal_name';
  251. $this->loadIndexer($indexId);
  252. $stateMock = $this->createPartialMock(
  253. \Magento\Indexer\Model\Indexer\State::class,
  254. ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
  255. );
  256. $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
  257. $stateMock->expects($this->never())->method('setIndexerId');
  258. $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  259. $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
  260. $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
  261. $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
  262. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  263. $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
  264. $this->viewMock->expects($this->never())->method('suspend');
  265. $this->viewMock->expects($this->once())->method('resume');
  266. $actionMock = $this->createPartialMock(
  267. \Magento\Framework\Indexer\ActionInterface::class,
  268. ['executeFull', 'executeList', 'executeRow']
  269. );
  270. $actionMock->expects($this->once())->method('executeFull')->will(
  271. $this->returnCallback(
  272. function () {
  273. throw new \Error('Test Engine Error');
  274. }
  275. )
  276. );
  277. $this->actionFactoryMock->expects(
  278. $this->once()
  279. )->method(
  280. 'create'
  281. )->with(
  282. 'Some\Class\Name'
  283. )->will(
  284. $this->returnValue($actionMock)
  285. );
  286. $this->model->reindexAll();
  287. }
  288. /**
  289. * @return array
  290. */
  291. protected function getIndexerData()
  292. {
  293. return [
  294. 'indexer_id' => 'indexer_internal_name',
  295. 'view_id' => 'view_test',
  296. 'action_class' => 'Some\Class\Name',
  297. 'title' => 'Indexer public name',
  298. 'description' => 'Indexer public description'
  299. ];
  300. }
  301. /**
  302. * @param $indexId
  303. */
  304. protected function loadIndexer($indexId)
  305. {
  306. $this->configMock->expects(
  307. $this->once()
  308. )->method(
  309. 'getIndexer'
  310. )->with(
  311. $indexId
  312. )->will(
  313. $this->returnValue($this->getIndexerData())
  314. );
  315. $this->model->load($indexId);
  316. }
  317. public function testGetTitle()
  318. {
  319. $result = 'Test Result';
  320. $this->model->setTitle($result);
  321. $this->assertEquals($result, $this->model->getTitle());
  322. }
  323. public function testGetDescription()
  324. {
  325. $result = 'Test Result';
  326. $this->model->setDescription($result);
  327. $this->assertEquals($result, $this->model->getDescription());
  328. }
  329. public function testSetState()
  330. {
  331. $stateMock = $this->createPartialMock(
  332. \Magento\Indexer\Model\Indexer\State::class,
  333. ['loadByIndexer', 'getId', '__wakeup']
  334. );
  335. $this->model->setState($stateMock);
  336. $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
  337. }
  338. public function testIsScheduled()
  339. {
  340. $result = true;
  341. $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
  342. $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($result));
  343. $this->assertEquals($result, $this->model->isScheduled());
  344. }
  345. /**
  346. * @param bool $scheduled
  347. * @param string $method
  348. * @dataProvider setScheduledDataProvider
  349. */
  350. public function testSetScheduled($scheduled, $method)
  351. {
  352. $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
  353. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  354. $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
  355. $this->viewMock->expects($this->once())->method($method)->will($this->returnValue(true));
  356. $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
  357. $this->model->setScheduled($scheduled);
  358. }
  359. /**
  360. * @return array
  361. */
  362. public function setScheduledDataProvider()
  363. {
  364. return [
  365. [true, 'subscribe'],
  366. [false, 'unsubscribe']
  367. ];
  368. }
  369. public function testGetStatus()
  370. {
  371. $status = StateInterface::STATUS_WORKING;
  372. $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
  373. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  374. $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
  375. $this->assertEquals($status, $this->model->getStatus());
  376. }
  377. /**
  378. * @param string $method
  379. * @param string $status
  380. * @dataProvider statusDataProvider
  381. */
  382. public function testStatus($method, $status)
  383. {
  384. $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
  385. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  386. $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
  387. $this->assertEquals(true, $this->model->$method());
  388. }
  389. /**
  390. * @return array
  391. */
  392. public function statusDataProvider()
  393. {
  394. return [
  395. ['isValid', StateInterface::STATUS_VALID],
  396. ['isInvalid', StateInterface::STATUS_INVALID],
  397. ['isWorking', StateInterface::STATUS_WORKING]
  398. ];
  399. }
  400. public function testInvalidate()
  401. {
  402. $stateMock = $this->createPartialMock(
  403. \Magento\Indexer\Model\Indexer\State::class,
  404. ['load', 'setStatus', 'save']
  405. );
  406. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  407. $stateMock->expects($this->once())->method('setStatus')->with(StateInterface::STATUS_INVALID)->will(
  408. $this->returnSelf()
  409. );
  410. $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
  411. $this->model->invalidate();
  412. }
  413. public function testReindexRow()
  414. {
  415. $id = 1;
  416. $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
  417. $actionMock = $this->createPartialMock(
  418. \Magento\Framework\Indexer\ActionInterface::class,
  419. ['executeFull', 'executeList', 'executeRow']
  420. );
  421. $this->actionFactoryMock->expects(
  422. $this->once()
  423. )->method(
  424. 'create'
  425. )->will(
  426. $this->returnValue($actionMock)
  427. );
  428. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  429. $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
  430. $actionMock->expects($this->once())->method('executeRow')->with($id)->will($this->returnSelf());
  431. $this->model->reindexRow($id);
  432. }
  433. public function testReindexList()
  434. {
  435. $ids = [1];
  436. $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
  437. $actionMock = $this->createPartialMock(
  438. \Magento\Framework\Indexer\ActionInterface::class,
  439. ['executeFull', 'executeList', 'executeRow']
  440. );
  441. $this->actionFactoryMock->expects(
  442. $this->once()
  443. )->method(
  444. 'create'
  445. )->will(
  446. $this->returnValue($actionMock)
  447. );
  448. $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
  449. $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
  450. $actionMock->expects($this->once())->method('executeList')->with($ids)->will($this->returnSelf());
  451. $this->model->reindexList($ids);
  452. }
  453. }