123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Mview\Test\Unit;
- use \Magento\Framework\Mview\View;
- class ViewTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\Mview\View
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ConfigInterface
- */
- protected $configMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ActionFactory
- */
- protected $actionFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Indexer\Model\Mview\View\State
- */
- protected $stateMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\Changelog
- */
- protected $changelogMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\SubscriptionFactory
- */
- protected $subscriptionFactoryMock;
- protected function setUp()
- {
- $this->configMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\ConfigInterface::class,
- [],
- '',
- false,
- false,
- true,
- ['getView']
- );
- $this->actionFactoryMock = $this->createPartialMock(\Magento\Framework\Mview\ActionFactory::class, ['get']);
- $this->stateMock = $this->createPartialMock(\Magento\Indexer\Model\Mview\View\State::class, ['getViewId',
- 'loadByView',
- 'getVersionId',
- 'setVersionId',
- 'getUpdated',
- 'getStatus',
- 'setStatus',
- 'getMode',
- 'setMode',
- 'save',
- '__wakeup',
- ]);
- $this->changelogMock = $this->createPartialMock(
- \Magento\Framework\Mview\View\Changelog::class,
- ['getViewId', 'setViewId', 'create', 'drop', 'getVersion', 'getList', 'clear']
- );
- $this->subscriptionFactoryMock = $this->createPartialMock(
- \Magento\Framework\Mview\View\SubscriptionFactory::class,
- ['create']
- );
- $this->model = new View(
- $this->configMock,
- $this->actionFactoryMock,
- $this->stateMock,
- $this->changelogMock,
- $this->subscriptionFactoryMock
- );
- }
- public function testGetActionClass()
- {
- $this->model->setData('action_class', 'actionClass');
- $this->assertEquals('actionClass', $this->model->getActionClass());
- }
- public function testGetGroup()
- {
- $this->model->setData('group', 'some_group');
- $this->assertEquals('some_group', $this->model->getGroup());
- }
- public function testGetSubscriptions()
- {
- $this->model->setData('subscriptions', ['subscription']);
- $this->assertEquals(['subscription'], $this->model->getSubscriptions());
- }
- public function testLoad()
- {
- $viewId = 'view_test';
- $this->configMock->expects(
- $this->once()
- )->method(
- 'getView'
- )->with(
- $viewId
- )->will(
- $this->returnValue($this->getViewData())
- );
- $this->assertInstanceOf(\Magento\Framework\Mview\View::class, $this->model->load($viewId));
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage view_id view does not exist.
- */
- public function testLoadWithException()
- {
- $viewId = 'view_id';
- $this->configMock->expects(
- $this->once()
- )->method(
- 'getView'
- )->with(
- $viewId
- )->will(
- $this->returnValue($this->getViewData())
- );
- $this->model->load($viewId);
- }
- public function testSubscribe()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
- $this->stateMock->expects($this->once())
- ->method('setMode')
- ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
- ->will($this->returnSelf());
- $this->changelogMock->expects($this->once())
- ->method('create');
- $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['create']);
- $subscriptionMock->expects($this->exactly(1))->method('create');
- $this->subscriptionFactoryMock->expects(
- $this->exactly(1)
- )->method(
- 'create'
- )->will(
- $this->returnValue($subscriptionMock)
- );
- $this->loadView();
- $this->model->subscribe();
- }
- public function testSubscribeEnabled()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
- $this->stateMock->expects($this->never())
- ->method('setMode');
- $this->changelogMock->expects($this->never())
- ->method('create');
- $this->subscriptionFactoryMock->expects($this->never())
- ->method('create');
- $this->loadView();
- $this->model->subscribe();
- }
- /**
- * @expectedException \Exception
- */
- public function testSubscribeWithException()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
- $this->changelogMock->expects($this->once())
- ->method('create')
- ->will($this->returnCallback(
- function () {
- throw new \Exception();
- }
- ));
- $this->loadView();
- $this->model->subscribe();
- }
- public function testUnsubscribe()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
- $this->stateMock->expects($this->once())
- ->method('setMode')
- ->with(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED)
- ->will($this->returnSelf());
- $this->changelogMock->expects($this->never())
- ->method('drop');
- $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
- $subscriptionMock->expects($this->exactly(1))->method('remove');
- $this->subscriptionFactoryMock->expects(
- $this->exactly(1)
- )->method(
- 'create'
- )->will(
- $this->returnValue($subscriptionMock)
- );
- $this->loadView();
- $this->model->unsubscribe();
- }
- public function testUnsubscribeDisabled()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
- $this->stateMock->expects($this->never())
- ->method('setVersionId');
- $this->stateMock->expects($this->never())
- ->method('setMode');
- $this->changelogMock->expects($this->never())
- ->method('drop');
- $this->subscriptionFactoryMock->expects($this->never())
- ->method('create');
- $this->loadView();
- $this->model->unsubscribe();
- }
- /**
- * @expectedException \Exception
- */
- public function testUnsubscribeWithException()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
- $subscriptionMock = $this->createPartialMock(\Magento\Framework\Mview\View\Subscription::class, ['remove']);
- $subscriptionMock->expects($this->exactly(1))
- ->method('remove')
- ->will($this->returnCallback(
- function () {
- throw new \Exception();
- }
- ));
- $this->subscriptionFactoryMock->expects($this->exactly(1))
- ->method('create')
- ->will($this->returnValue($subscriptionMock));
- $this->loadView();
- $this->model->unsubscribe();
- }
- public function testUpdate()
- {
- $currentVersionId = 3;
- $lastVersionId = 1;
- $listId = [2, 3];
- $this->stateMock->expects($this->any())
- ->method('getViewId')
- ->will($this->returnValue(1));
- $this->stateMock->expects($this->once())
- ->method('getVersionId')
- ->will($this->returnValue($lastVersionId));
- $this->stateMock->expects($this->once())
- ->method('setVersionId')
- ->will($this->returnSelf());
- $this->stateMock->expects($this->exactly(2))
- ->method('getStatus')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
- $this->stateMock->expects($this->exactly(2))
- ->method('setStatus')
- ->will($this->returnSelf());
- $this->stateMock->expects($this->exactly(2))
- ->method('save')
- ->will($this->returnSelf());
- $this->changelogMock->expects(
- $this->once()
- )->method(
- 'getVersion'
- )->will(
- $this->returnValue($currentVersionId)
- );
- $this->changelogMock->expects(
- $this->once()
- )->method(
- 'getList'
- )->with(
- $lastVersionId,
- $currentVersionId
- )->will(
- $this->returnValue($listId)
- );
- $actionMock = $this->createMock(\Magento\Framework\Mview\ActionInterface::class);
- $actionMock->expects($this->once())->method('execute')->with($listId)->will($this->returnSelf());
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'get'
- )->with(
- 'Some\Class\Name'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->loadView();
- $this->model->update();
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage Test exception
- */
- public function testUpdateWithException()
- {
- $currentVersionId = 3;
- $lastVersionId = 1;
- $listId = [2, 3];
- $this->stateMock->expects($this->any())
- ->method('getViewId')
- ->will($this->returnValue(1));
- $this->stateMock->expects($this->once())
- ->method('getVersionId')
- ->will($this->returnValue($lastVersionId));
- $this->stateMock->expects($this->never())
- ->method('setVersionId');
- $this->stateMock->expects($this->exactly(2))
- ->method('getStatus')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
- $this->stateMock->expects($this->exactly(2))
- ->method('setStatus')
- ->will($this->returnSelf());
- $this->stateMock->expects($this->exactly(2))
- ->method('save')
- ->will($this->returnSelf());
- $this->changelogMock->expects(
- $this->once()
- )->method(
- 'getVersion'
- )->will(
- $this->returnValue($currentVersionId)
- );
- $this->changelogMock->expects(
- $this->once()
- )->method(
- 'getList'
- )->with(
- $lastVersionId,
- $currentVersionId
- )->will(
- $this->returnValue($listId)
- );
- $actionMock = $this->createPartialMock(\Magento\Framework\Mview\ActionInterface::class, ['execute']);
- $actionMock->expects($this->once())->method('execute')->with($listId)->will(
- $this->returnCallback(
- function () {
- throw new \Exception('Test exception');
- }
- )
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'get'
- )->with(
- 'Some\Class\Name'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->loadView();
- $this->model->update();
- }
- public function testSuspend()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
- $this->stateMock->expects($this->once())
- ->method('setVersionId')
- ->with(11)
- ->will($this->returnSelf());
- $this->stateMock->expects($this->once())
- ->method('setStatus')
- ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED)
- ->will($this->returnSelf());
- $this->stateMock->expects($this->once())
- ->method('save')
- ->will($this->returnSelf());
- $this->changelogMock->expects($this->once())
- ->method('getVersion')
- ->will($this->returnValue(11));
- $this->loadView();
- $this->model->suspend();
- }
- public function testSuspendDisabled()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
- $this->stateMock->expects($this->never())
- ->method('setVersionId');
- $this->stateMock->expects($this->never())
- ->method('setStatus');
- $this->stateMock->expects($this->never())
- ->method('save');
- $this->changelogMock->expects($this->never())
- ->method('getVersion');
- $this->loadView();
- $this->model->suspend();
- }
- public function testResume()
- {
- $this->stateMock->expects($this->once())
- ->method('getStatus')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED));
- $this->stateMock->expects($this->once())
- ->method('setStatus')
- ->with(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE)
- ->will($this->returnSelf());
- $this->stateMock->expects($this->once())
- ->method('save')
- ->will($this->returnSelf());
- $this->loadView();
- $this->model->resume();
- }
- /**
- * @param string $status
- * @dataProvider dataProviderResumeNotSuspended
- */
- public function testResumeNotSuspended($status)
- {
- $this->stateMock->expects($this->once())
- ->method('getStatus')
- ->will($this->returnValue($status));
- $this->stateMock->expects($this->never())
- ->method('setStatus');
- $this->stateMock->expects($this->never())
- ->method('save');
- $this->loadView();
- $this->model->resume();
- }
- /**
- * @return array
- */
- public function dataProviderResumeNotSuspended()
- {
- return [
- [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING],
- ];
- }
- public function testClearChangelog()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
- $this->stateMock->expects($this->once())
- ->method('getVersionId')
- ->will($this->returnValue(11));
- $this->changelogMock->expects($this->once())
- ->method('clear')
- ->with(11)
- ->will($this->returnValue(true));
- $this->loadView();
- $this->model->clearChangelog();
- }
- public function testClearChangelogDisabled()
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED));
- $this->stateMock->expects($this->never())
- ->method('getVersionId');
- $this->changelogMock->expects($this->never())
- ->method('clear');
- $this->loadView();
- $this->model->clearChangelog();
- }
- public function testSetState()
- {
- $this->model->setState($this->stateMock);
- $this->assertEquals($this->stateMock, $this->model->getState());
- }
- /**
- * @param string $mode
- * @param bool $result
- * @dataProvider dataProviderIsEnabled
- */
- public function testIsEnabled($mode, $result)
- {
- $this->stateMock->expects($this->once())
- ->method('getMode')
- ->will($this->returnValue($mode));
- $this->assertEquals($result, $this->model->isEnabled());
- }
- /**
- * @return array
- */
- public function dataProviderIsEnabled()
- {
- return [
- [\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED, true],
- [\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED, false],
- ];
- }
- /**
- * @param string $status
- * @param bool $result
- * @dataProvider dataProviderIsIdle
- */
- public function testIsIdle($status, $result)
- {
- $this->stateMock->expects($this->once())
- ->method('getStatus')
- ->will($this->returnValue($status));
- $this->assertEquals($result, $this->model->isIdle());
- }
- /**
- * @return array
- */
- public function dataProviderIsIdle()
- {
- return [
- [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, true],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
- ];
- }
- /**
- * @param string $status
- * @param bool $result
- * @dataProvider dataProviderIsWorking
- */
- public function testIsWorking($status, $result)
- {
- $this->stateMock->expects($this->once())
- ->method('getStatus')
- ->will($this->returnValue($status));
- $this->assertEquals($result, $this->model->isWorking());
- }
- /**
- * @return array
- */
- public function dataProviderIsWorking()
- {
- return [
- [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, true],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, false],
- ];
- }
- /**
- * @param string $status
- * @param bool $result
- * @dataProvider dataProviderIsSuspended
- */
- public function testIsSuspended($status, $result)
- {
- $this->stateMock->expects($this->once())
- ->method('getStatus')
- ->will($this->returnValue($status));
- $this->assertEquals($result, $this->model->isSuspended());
- }
- /**
- * @return array
- */
- public function dataProviderIsSuspended()
- {
- return [
- [\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE, false],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_WORKING, false],
- [\Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED, true],
- ];
- }
- public function testGetUpdated()
- {
- $this->stateMock->expects($this->once())
- ->method('getUpdated')
- ->will($this->returnValue('some datetime'));
- $this->assertEquals('some datetime', $this->model->getUpdated());
- }
- protected function loadView()
- {
- $viewId = 'view_test';
- $this->configMock->expects(
- $this->once()
- )->method(
- 'getView'
- )->with(
- $viewId
- )->will(
- $this->returnValue($this->getViewData())
- );
- $this->model->load($viewId);
- }
- /**
- * @return array
- */
- protected function getViewData()
- {
- return [
- 'view_id' => 'view_test',
- 'action_class' => 'Some\Class\Name',
- 'group' => 'some_group',
- 'subscriptions' => ['some_entity' => ['name' => 'some_entity', 'column' => 'entity_id']]
- ];
- }
- }
|