123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Indexer\Test\Unit\Model;
- use Magento\Framework\Indexer\StateInterface;
- class IndexerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Indexer\Model\Indexer|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $model;
- /**
- * @var \Magento\Framework\Indexer\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $configMock;
- /**
- * @var \Magento\Framework\Indexer\ActionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $actionFactoryMock;
- /**
- * @var \Magento\Framework\Mview\ViewInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $viewMock;
- /**
- * @var \Magento\Indexer\Model\Indexer\StateFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $stateFactoryMock;
- /**
- * @var \Magento\Indexer\Model\Indexer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $indexFactoryMock;
- protected function setUp()
- {
- $this->configMock = $this->getMockForAbstractClass(
- \Magento\Framework\Indexer\ConfigInterface::class,
- [],
- '',
- false,
- false,
- true,
- ['getIndexer']
- );
- $this->actionFactoryMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionFactory::class,
- ['create']
- );
- $this->viewMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\ViewInterface::class,
- [],
- '',
- false,
- false,
- true,
- ['load', 'isEnabled', 'getUpdated', 'getStatus', '__wakeup', 'getId', 'suspend', 'resume']
- );
- $this->stateFactoryMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\StateFactory::class,
- ['create']
- );
- $this->indexFactoryMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\CollectionFactory::class,
- ['create']
- );
- $structureFactory = $this->getMockBuilder(\Magento\Framework\Indexer\StructureFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- /** @var \Magento\Framework\Indexer\StructureFactory $structureFactory */
- $this->model = new \Magento\Indexer\Model\Indexer(
- $this->configMock,
- $this->actionFactoryMock,
- $structureFactory,
- $this->viewMock,
- $this->stateFactoryMock,
- $this->indexFactoryMock
- );
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage indexer_id indexer does not exist.
- */
- public function testLoadWithException()
- {
- $indexId = 'indexer_id';
- $this->configMock->expects(
- $this->once()
- )->method(
- 'getIndexer'
- )->with(
- $indexId
- )->will(
- $this->returnValue($this->getIndexerData())
- );
- $this->model->load($indexId);
- }
- public function testGetView()
- {
- $indexId = 'indexer_internal_name';
- $this->viewMock->expects($this->once())->method('load')->with('view_test')->will($this->returnSelf());
- $this->loadIndexer($indexId);
- $this->assertEquals($this->viewMock, $this->model->getView());
- }
- public function testGetState()
- {
- $indexId = 'indexer_internal_name';
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['loadByIndexer', 'getId', '__wakeup']
- );
- $stateMock->expects($this->once())->method('loadByIndexer')->with($indexId)->will($this->returnSelf());
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $this->loadIndexer($indexId);
- $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
- }
- /**
- * @param bool $getViewIsEnabled
- * @param string $getViewGetUpdated
- * @param string $getStateGetUpdated
- * @dataProvider getLatestUpdatedDataProvider
- */
- public function testGetLatestUpdated($getViewIsEnabled, $getViewGetUpdated, $getStateGetUpdated)
- {
- $indexId = 'indexer_internal_name';
- $this->loadIndexer($indexId);
- $this->viewMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($getViewIsEnabled));
- $this->viewMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getViewGetUpdated));
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['load', 'getId', 'setIndexerId', '__wakeup', 'getUpdated']
- );
- $stateMock->expects($this->any())->method('getUpdated')->will($this->returnValue($getStateGetUpdated));
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- if ($getViewIsEnabled && $getViewGetUpdated) {
- if (!$getStateGetUpdated) {
- $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
- } else {
- if ($getViewGetUpdated == $getStateGetUpdated) {
- $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
- } else {
- $this->assertEquals($getViewGetUpdated, $this->model->getLatestUpdated());
- }
- }
- } else {
- $this->assertEquals($getStateGetUpdated, $this->model->getLatestUpdated());
- }
- }
- /**
- * @return array
- */
- public function getLatestUpdatedDataProvider()
- {
- return [
- [false, '06-Jan-1944', '06-Jan-1944'],
- [false, '', '06-Jan-1944'],
- [false, '06-Jan-1944', ''],
- [false, '', ''],
- [true, '06-Jan-1944', '06-Jan-1944'],
- [true, '', '06-Jan-1944'],
- [true, '06-Jan-1944', ''],
- [true, '', ''],
- [true, '06-Jan-1944', '05-Jan-1944']
- ];
- }
- public function testReindexAll()
- {
- $indexId = 'indexer_internal_name';
- $this->loadIndexer($indexId);
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
- );
- $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
- $stateMock->expects($this->never())->method('setIndexerId');
- $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
- $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
- $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
- $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
- $this->viewMock->expects($this->once())->method('suspend');
- $this->viewMock->expects($this->once())->method('resume');
- $actionMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionInterface::class,
- ['executeFull', 'executeList', 'executeRow']
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->with(
- 'Some\Class\Name'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->model->reindexAll();
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage Test exception
- */
- public function testReindexAllWithException()
- {
- $indexId = 'indexer_internal_name';
- $this->loadIndexer($indexId);
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
- );
- $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
- $stateMock->expects($this->never())->method('setIndexerId');
- $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
- $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
- $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
- $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
- $this->viewMock->expects($this->never())->method('suspend');
- $this->viewMock->expects($this->once())->method('resume');
- $actionMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionInterface::class,
- ['executeFull', 'executeList', 'executeRow']
- );
- $actionMock->expects($this->once())->method('executeFull')->will(
- $this->returnCallback(
- function () {
- throw new \Exception('Test exception');
- }
- )
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->with(
- 'Some\Class\Name'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->model->reindexAll();
- }
- /**
- * @expectedException \Error
- * @expectedExceptionMessage Test Engine Error
- */
- public function testReindexAllWithError()
- {
- $indexId = 'indexer_internal_name';
- $this->loadIndexer($indexId);
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['load', 'getId', 'setIndexerId', '__wakeup', 'getStatus', 'setStatus', 'save']
- );
- $stateMock->expects($this->once())->method('load')->with($indexId, 'indexer_id')->will($this->returnSelf());
- $stateMock->expects($this->never())->method('setIndexerId');
- $stateMock->expects($this->once())->method('getId')->will($this->returnValue(1));
- $stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
- $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue('idle'));
- $stateMock->expects($this->exactly(2))->method('save')->will($this->returnSelf());
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
- $this->viewMock->expects($this->never())->method('suspend');
- $this->viewMock->expects($this->once())->method('resume');
- $actionMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionInterface::class,
- ['executeFull', 'executeList', 'executeRow']
- );
- $actionMock->expects($this->once())->method('executeFull')->will(
- $this->returnCallback(
- function () {
- throw new \Error('Test Engine Error');
- }
- )
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->with(
- 'Some\Class\Name'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->model->reindexAll();
- }
- /**
- * @return array
- */
- protected function getIndexerData()
- {
- return [
- 'indexer_id' => 'indexer_internal_name',
- 'view_id' => 'view_test',
- 'action_class' => 'Some\Class\Name',
- 'title' => 'Indexer public name',
- 'description' => 'Indexer public description'
- ];
- }
- /**
- * @param $indexId
- */
- protected function loadIndexer($indexId)
- {
- $this->configMock->expects(
- $this->once()
- )->method(
- 'getIndexer'
- )->with(
- $indexId
- )->will(
- $this->returnValue($this->getIndexerData())
- );
- $this->model->load($indexId);
- }
- public function testGetTitle()
- {
- $result = 'Test Result';
- $this->model->setTitle($result);
- $this->assertEquals($result, $this->model->getTitle());
- }
- public function testGetDescription()
- {
- $result = 'Test Result';
- $this->model->setDescription($result);
- $this->assertEquals($result, $this->model->getDescription());
- }
- public function testSetState()
- {
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['loadByIndexer', 'getId', '__wakeup']
- );
- $this->model->setState($stateMock);
- $this->assertInstanceOf(\Magento\Indexer\Model\Indexer\State::class, $this->model->getState());
- }
- public function testIsScheduled()
- {
- $result = true;
- $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->viewMock->expects($this->once())->method('isEnabled')->will($this->returnValue($result));
- $this->assertEquals($result, $this->model->isScheduled());
- }
- /**
- * @param bool $scheduled
- * @param string $method
- * @dataProvider setScheduledDataProvider
- */
- public function testSetScheduled($scheduled, $method)
- {
- $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $this->viewMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->viewMock->expects($this->once())->method($method)->will($this->returnValue(true));
- $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
- $this->model->setScheduled($scheduled);
- }
- /**
- * @return array
- */
- public function setScheduledDataProvider()
- {
- return [
- [true, 'subscribe'],
- [false, 'unsubscribe']
- ];
- }
- public function testGetStatus()
- {
- $status = StateInterface::STATUS_WORKING;
- $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
- $this->assertEquals($status, $this->model->getStatus());
- }
- /**
- * @param string $method
- * @param string $status
- * @dataProvider statusDataProvider
- */
- public function testStatus($method, $status)
- {
- $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'getStatus']);
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $stateMock->expects($this->once())->method('getStatus')->will($this->returnValue($status));
- $this->assertEquals(true, $this->model->$method());
- }
- /**
- * @return array
- */
- public function statusDataProvider()
- {
- return [
- ['isValid', StateInterface::STATUS_VALID],
- ['isInvalid', StateInterface::STATUS_INVALID],
- ['isWorking', StateInterface::STATUS_WORKING]
- ];
- }
- public function testInvalidate()
- {
- $stateMock = $this->createPartialMock(
- \Magento\Indexer\Model\Indexer\State::class,
- ['load', 'setStatus', 'save']
- );
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $stateMock->expects($this->once())->method('setStatus')->with(StateInterface::STATUS_INVALID)->will(
- $this->returnSelf()
- );
- $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
- $this->model->invalidate();
- }
- public function testReindexRow()
- {
- $id = 1;
- $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
- $actionMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionInterface::class,
- ['executeFull', 'executeList', 'executeRow']
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
- $actionMock->expects($this->once())->method('executeRow')->with($id)->will($this->returnSelf());
- $this->model->reindexRow($id);
- }
- public function testReindexList()
- {
- $ids = [1];
- $stateMock = $this->createPartialMock(\Magento\Indexer\Model\Indexer\State::class, ['load', 'save']);
- $actionMock = $this->createPartialMock(
- \Magento\Framework\Indexer\ActionInterface::class,
- ['executeFull', 'executeList', 'executeRow']
- );
- $this->actionFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->will(
- $this->returnValue($actionMock)
- );
- $this->stateFactoryMock->expects($this->once())->method('create')->will($this->returnValue($stateMock));
- $stateMock->expects($this->once())->method('save')->will($this->returnSelf());
- $actionMock->expects($this->once())->method('executeList')->with($ids)->will($this->returnSelf());
- $this->model->reindexList($ids);
- }
- }
|