ViewTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Mview\Test\Unit;
  7. use \Magento\Framework\Mview\View;
  8. class ViewTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Mview\View
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ConfigInterface
  16. */
  17. protected $configMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ActionFactory
  20. */
  21. protected $actionFactoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Indexer\Model\Mview\View\State
  24. */
  25. protected $stateMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\Changelog
  28. */
  29. protected $changelogMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\SubscriptionFactory
  32. */
  33. protected $subscriptionFactoryMock;
  34. protected function setUp()
  35. {
  36. $this->configMock = $this->getMockForAbstractClass(
  37. \Magento\Framework\Mview\ConfigInterface::class,
  38. [],
  39. '',
  40. false,
  41. false,
  42. true,
  43. ['getView']
  44. );
  45. $this->actionFactoryMock = $this->createPartialMock(\Magento\Framework\Mview\ActionFactory::class, ['get']);
  46. $this->stateMock = $this->createPartialMock(\Magento\Indexer\Model\Mview\View\State::class, ['getViewId',
  47. 'loadByView',
  48. 'getVersionId',
  49. 'setVersionId',
  50. 'getUpdated',
  51. 'getStatus',
  52. 'setStatus',
  53. 'getMode',
  54. 'setMode',
  55. 'save',
  56. '__wakeup',
  57. ]);
  58. $this->changelogMock = $this->createPartialMock(
  59. \Magento\Framework\Mview\View\Changelog::class,
  60. ['getViewId', 'setViewId', 'create', 'drop', 'getVersion', 'getList', 'clear']
  61. );
  62. $this->subscriptionFactoryMock = $this->createPartialMock(
  63. \Magento\Framework\Mview\View\SubscriptionFactory::class,
  64. ['create']
  65. );
  66. $this->model = new View(
  67. $this->configMock,
  68. $this->actionFactoryMock,
  69. $this->stateMock,
  70. $this->changelogMock,
  71. $this->subscriptionFactoryMock
  72. );
  73. }
  74. public function testGetActionClass()
  75. {
  76. $this->model->setData('action_class', 'actionClass');
  77. $this->assertEquals('actionClass', $this->model->getActionClass());
  78. }
  79. public function testGetGroup()
  80. {
  81. $this->model->setData('group', 'some_group');
  82. $this->assertEquals('some_group', $this->model->getGroup());
  83. }
  84. public function testGetSubscriptions()
  85. {
  86. $this->model->setData('subscriptions', ['subscription']);
  87. $this->assertEquals(['subscription'], $this->model->getSubscriptions());
  88. }
  89. public function testLoad()
  90. {
  91. $viewId = 'view_test';
  92. $this->configMock->expects(
  93. $this->once()
  94. )->method(
  95. 'getView'
  96. )->with(
  97. $viewId
  98. )->will(
  99. $this->returnValue($this->getViewData())
  100. );
  101. $this->assertInstanceOf(\Magento\Framework\Mview\View::class, $this->model->load($viewId));
  102. }
  103. /**
  104. * @expectedException \InvalidArgumentException
  105. * @expectedExceptionMessage view_id view does not exist.
  106. */
  107. public function testLoadWithException()
  108. {
  109. $viewId = 'view_id';
  110. $this->configMock->expects(
  111. $this->once()
  112. )->method(
  113. 'getView'
  114. )->with(
  115. $viewId
  116. )->will(
  117. $this->returnValue($this->getViewData())
  118. );
  119. $this->model->load($viewId);
  120. }
  121. public function testSubscribe()
  122. {
  123. $this->stateMock->expects($this->once())
  124. ->method('getMode')
  125. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
  126. $this->stateMock->expects($this->once())
  127. ->method('setMode')
  128. ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
  129. ->will($this->returnSelf());
  130. $this->changelogMock->expects($this->once())
  131. ->method('create');
  132. $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['create']);
  133. $subscriptionMock->expects($this->exactly(1))->method('create');
  134. $this->subscriptionFactoryMock->expects(
  135. $this->exactly(1)
  136. )->method(
  137. 'create'
  138. )->will(
  139. $this->returnValue($subscriptionMock)
  140. );
  141. $this->loadView();
  142. $this->model->subscribe();
  143. }
  144. public function testSubscribeEnabled()
  145. {
  146. $this->stateMock->expects($this->once())
  147. ->method('getMode')
  148. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
  149. $this->stateMock->expects($this->never())
  150. ->method('setMode');
  151. $this->changelogMock->expects($this->never())
  152. ->method('create');
  153. $this->subscriptionFactoryMock->expects($this->never())
  154. ->method('create');
  155. $this->loadView();
  156. $this->model->subscribe();
  157. }
  158. /**
  159. * @expectedException \Exception
  160. */
  161. public function testSubscribeWithException()
  162. {
  163. $this->stateMock->expects($this->once())
  164. ->method('getMode')
  165. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
  166. $this->changelogMock->expects($this->once())
  167. ->method('create')
  168. ->will($this->returnCallback(
  169. function () {
  170. throw new \Exception();
  171. }
  172. ));
  173. $this->loadView();
  174. $this->model->subscribe();
  175. }
  176. public function testUnsubscribe()
  177. {
  178. $this->stateMock->expects($this->once())
  179. ->method('getMode')
  180. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
  181. $this->stateMock->expects($this->once())
  182. ->method('setMode')
  183. ->with(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED)
  184. ->will($this->returnSelf());
  185. $this->changelogMock->expects($this->never())
  186. ->method('drop');
  187. $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
  188. $subscriptionMock->expects($this->exactly(1))->method('remove');
  189. $this->subscriptionFactoryMock->expects(
  190. $this->exactly(1)
  191. )->method(
  192. 'create'
  193. )->will(
  194. $this->returnValue($subscriptionMock)
  195. );
  196. $this->loadView();
  197. $this->model->unsubscribe();
  198. }
  199. public function testUnsubscribeDisabled()
  200. {
  201. $this->stateMock->expects($this->once())
  202. ->method('getMode')
  203. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
  204. $this->stateMock->expects($this->never())
  205. ->method('setVersionId');
  206. $this->stateMock->expects($this->never())
  207. ->method('setMode');
  208. $this->changelogMock->expects($this->never())
  209. ->method('drop');
  210. $this->subscriptionFactoryMock->expects($this->never())
  211. ->method('create');
  212. $this->loadView();
  213. $this->model->unsubscribe();
  214. }
  215. /**
  216. * @expectedException \Exception
  217. */
  218. public function testUnsubscribeWithException()
  219. {
  220. $this->stateMock->expects($this->once())
  221. ->method('getMode')
  222. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
  223. $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
  224. $subscriptionMock->expects($this->exactly(1))
  225. ->method('remove')
  226. ->will($this->returnCallback(
  227. function () {
  228. throw new \Exception();
  229. }
  230. ));
  231. $this->subscriptionFactoryMock->expects($this->exactly(1))
  232. ->method('create')
  233. ->will($this->returnValue($subscriptionMock));
  234. $this->loadView();
  235. $this->model->unsubscribe();
  236. }
  237. public function testUpdate()
  238. {
  239. $currentVersionId = 3;
  240. $lastVersionId = 1;
  241. $listId = [2, 3];
  242. $this->stateMock->expects($this->any())
  243. ->method('getViewId')
  244. ->will($this->returnValue(1));
  245. $this->stateMock->expects($this->once())
  246. ->method('getVersionId')
  247. ->will($this->returnValue($lastVersionId));
  248. $this->stateMock->expects($this->once())
  249. ->method('setVersionId')
  250. ->will($this->returnSelf());
  251. $this->stateMock->expects($this->exactly(2))
  252. ->method('getStatus')
  253. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
  254. $this->stateMock->expects($this->exactly(2))
  255. ->method('setStatus')
  256. ->will($this->returnSelf());
  257. $this->stateMock->expects($this->exactly(2))
  258. ->method('save')
  259. ->will($this->returnSelf());
  260. $this->changelogMock->expects(
  261. $this->once()
  262. )->method(
  263. 'getVersion'
  264. )->will(
  265. $this->returnValue($currentVersionId)
  266. );
  267. $this->changelogMock->expects(
  268. $this->once()
  269. )->method(
  270. 'getList'
  271. )->with(
  272. $lastVersionId,
  273. $currentVersionId
  274. )->will(
  275. $this->returnValue($listId)
  276. );
  277. $actionMock = $this->createMock(\Magento\Framework\Mview\ActionInterface::class);
  278. $actionMock->expects($this->once())->method('execute')->with($listId)->will($this->returnSelf());
  279. $this->actionFactoryMock->expects(
  280. $this->once()
  281. )->method(
  282. 'get'
  283. )->with(
  284. 'Some\Class\Name'
  285. )->will(
  286. $this->returnValue($actionMock)
  287. );
  288. $this->loadView();
  289. $this->model->update();
  290. }
  291. /**
  292. * @expectedException \Exception
  293. * @expectedExceptionMessage Test exception
  294. */
  295. public function testUpdateWithException()
  296. {
  297. $currentVersionId = 3;
  298. $lastVersionId = 1;
  299. $listId = [2, 3];
  300. $this->stateMock->expects($this->any())
  301. ->method('getViewId')
  302. ->will($this->returnValue(1));
  303. $this->stateMock->expects($this->once())
  304. ->method('getVersionId')
  305. ->will($this->returnValue($lastVersionId));
  306. $this->stateMock->expects($this->never())
  307. ->method('setVersionId');
  308. $this->stateMock->expects($this->exactly(2))
  309. ->method('getStatus')
  310. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
  311. $this->stateMock->expects($this->exactly(2))
  312. ->method('setStatus')
  313. ->will($this->returnSelf());
  314. $this->stateMock->expects($this->exactly(2))
  315. ->method('save')
  316. ->will($this->returnSelf());
  317. $this->changelogMock->expects(
  318. $this->once()
  319. )->method(
  320. 'getVersion'
  321. )->will(
  322. $this->returnValue($currentVersionId)
  323. );
  324. $this->changelogMock->expects(
  325. $this->once()
  326. )->method(
  327. 'getList'
  328. )->with(
  329. $lastVersionId,
  330. $currentVersionId
  331. )->will(
  332. $this->returnValue($listId)
  333. );
  334. $actionMock = $this->createPartialMock(\Magento\Framework\Mview\ActionInterface::class, ['execute']);
  335. $actionMock->expects($this->once())->method('execute')->with($listId)->will(
  336. $this->returnCallback(
  337. function () {
  338. throw new \Exception('Test exception');
  339. }
  340. )
  341. );
  342. $this->actionFactoryMock->expects(
  343. $this->once()
  344. )->method(
  345. 'get'
  346. )->with(
  347. 'Some\Class\Name'
  348. )->will(
  349. $this->returnValue($actionMock)
  350. );
  351. $this->loadView();
  352. $this->model->update();
  353. }
  354. public function testSuspend()
  355. {
  356. $this->stateMock->expects($this->once())
  357. ->method('getMode')
  358. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
  359. $this->stateMock->expects($this->once())
  360. ->method('setVersionId')
  361. ->with(11)
  362. ->will($this->returnSelf());
  363. $this->stateMock->expects($this->once())
  364. ->method('setStatus')
  365. ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED)
  366. ->will($this->returnSelf());
  367. $this->stateMock->expects($this->once())
  368. ->method('save')
  369. ->will($this->returnSelf());
  370. $this->changelogMock->expects($this->once())
  371. ->method('getVersion')
  372. ->will($this->returnValue(11));
  373. $this->loadView();
  374. $this->model->suspend();
  375. }
  376. public function testSuspendDisabled()
  377. {
  378. $this->stateMock->expects($this->once())
  379. ->method('getMode')
  380. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
  381. $this->stateMock->expects($this->never())
  382. ->method('setVersionId');
  383. $this->stateMock->expects($this->never())
  384. ->method('setStatus');
  385. $this->stateMock->expects($this->never())
  386. ->method('save');
  387. $this->changelogMock->expects($this->never())
  388. ->method('getVersion');
  389. $this->loadView();
  390. $this->model->suspend();
  391. }
  392. public function testResume()
  393. {
  394. $this->stateMock->expects($this->once())
  395. ->method('getStatus')
  396. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED));
  397. $this->stateMock->expects($this->once())
  398. ->method('setStatus')
  399. ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE)
  400. ->will($this->returnSelf());
  401. $this->stateMock->expects($this->once())
  402. ->method('save')
  403. ->will($this->returnSelf());
  404. $this->loadView();
  405. $this->model->resume();
  406. }
  407. /**
  408. * @param string $status
  409. * @dataProvider dataProviderResumeNotSuspended
  410. */
  411. public function testResumeNotSuspended($status)
  412. {
  413. $this->stateMock->expects($this->once())
  414. ->method('getStatus')
  415. ->will($this->returnValue($status));
  416. $this->stateMock->expects($this->never())
  417. ->method('setStatus');
  418. $this->stateMock->expects($this->never())
  419. ->method('save');
  420. $this->loadView();
  421. $this->model->resume();
  422. }
  423. /**
  424. * @return array
  425. */
  426. public function dataProviderResumeNotSuspended()
  427. {
  428. return [
  429. [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE],
  430. [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING],
  431. ];
  432. }
  433. public function testClearChangelog()
  434. {
  435. $this->stateMock->expects($this->once())
  436. ->method('getMode')
  437. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
  438. $this->stateMock->expects($this->once())
  439. ->method('getVersionId')
  440. ->will($this->returnValue(11));
  441. $this->changelogMock->expects($this->once())
  442. ->method('clear')
  443. ->with(11)
  444. ->will($this->returnValue(true));
  445. $this->loadView();
  446. $this->model->clearChangelog();
  447. }
  448. public function testClearChangelogDisabled()
  449. {
  450. $this->stateMock->expects($this->once())
  451. ->method('getMode')
  452. ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
  453. $this->stateMock->expects($this->never())
  454. ->method('getVersionId');
  455. $this->changelogMock->expects($this->never())
  456. ->method('clear');
  457. $this->loadView();
  458. $this->model->clearChangelog();
  459. }
  460. public function testSetState()
  461. {
  462. $this->model->setState($this->stateMock);
  463. $this->assertEquals($this->stateMock, $this->model->getState());
  464. }
  465. /**
  466. * @param string $mode
  467. * @param bool $result
  468. * @dataProvider dataProviderIsEnabled
  469. */
  470. public function testIsEnabled($mode, $result)
  471. {
  472. $this->stateMock->expects($this->once())
  473. ->method('getMode')
  474. ->will($this->returnValue($mode));
  475. $this->assertEquals($result, $this->model->isEnabled());
  476. }
  477. /**
  478. * @return array
  479. */
  480. public function dataProviderIsEnabled()
  481. {
  482. return [
  483. [\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED, true],
  484. [\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED, false],
  485. ];
  486. }
  487. /**
  488. * @param string $status
  489. * @param bool $result
  490. * @dataProvider dataProviderIsIdle
  491. */
  492. public function testIsIdle($status, $result)
  493. {
  494. $this->stateMock->expects($this->once())
  495. ->method('getStatus')
  496. ->will($this->returnValue($status));
  497. $this->assertEquals($result, $this->model->isIdle());
  498. }
  499. /**
  500. * @return array
  501. */
  502. public function dataProviderIsIdle()
  503. {
  504. return [
  505. [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, true],
  506. [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
  507. [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
  508. ];
  509. }
  510. /**
  511. * @param string $status
  512. * @param bool $result
  513. * @dataProvider dataProviderIsWorking
  514. */
  515. public function testIsWorking($status, $result)
  516. {
  517. $this->stateMock->expects($this->once())
  518. ->method('getStatus')
  519. ->will($this->returnValue($status));
  520. $this->assertEquals($result, $this->model->isWorking());
  521. }
  522. /**
  523. * @return array
  524. */
  525. public function dataProviderIsWorking()
  526. {
  527. return [
  528. [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
  529. [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, true],
  530. [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
  531. ];
  532. }
  533. /**
  534. * @param string $status
  535. * @param bool $result
  536. * @dataProvider dataProviderIsSuspended
  537. */
  538. public function testIsSuspended($status, $result)
  539. {
  540. $this->stateMock->expects($this->once())
  541. ->method('getStatus')
  542. ->will($this->returnValue($status));
  543. $this->assertEquals($result, $this->model->isSuspended());
  544. }
  545. /**
  546. * @return array
  547. */
  548. public function dataProviderIsSuspended()
  549. {
  550. return [
  551. [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
  552. [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
  553. [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, true],
  554. ];
  555. }
  556. public function testGetUpdated()
  557. {
  558. $this->stateMock->expects($this->once())
  559. ->method('getUpdated')
  560. ->will($this->returnValue('some datetime'));
  561. $this->assertEquals('some datetime', $this->model->getUpdated());
  562. }
  563. protected function loadView()
  564. {
  565. $viewId = 'view_test';
  566. $this->configMock->expects(
  567. $this->once()
  568. )->method(
  569. 'getView'
  570. )->with(
  571. $viewId
  572. )->will(
  573. $this->returnValue($this->getViewData())
  574. );
  575. $this->model->load($viewId);
  576. }
  577. /**
  578. * @return array
  579. */
  580. protected function getViewData()
  581. {
  582. return [
  583. 'view_id' => 'view_test',
  584. 'action_class' => 'Some\Class\Name',
  585. 'group' => 'some_group',
  586. 'subscriptions' => ['some_entity' => ['name' => 'some_entity', 'column' => 'entity_id']]
  587. ];
  588. }
  589. }