123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Mview\Test\Unit\View;
- use \Magento\Framework\Mview\View\Subscription;
- class SubscriptionTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * Mysql PDO DB adapter mock
- *
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\Pdo\Mysql
- */
- protected $connectionMock;
- /** @var \Magento\Framework\Mview\View\Subscription */
- protected $model;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection */
- protected $resourceMock;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Ddl\TriggerFactory */
- protected $triggerFactoryMock;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\CollectionInterface */
- protected $viewCollectionMock;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ViewInterface */
- protected $viewMock;
- /** @var string */
- private $tableName;
- protected function setUp()
- {
- $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
- $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
- $this->connectionMock->expects($this->any())
- ->method('quoteIdentifier')
- ->will($this->returnArgument(0));
- $this->resourceMock->expects($this->atLeastOnce())
- ->method('getConnection')
- ->willReturn($this->connectionMock);
- $this->triggerFactoryMock = $this->createMock(\Magento\Framework\DB\Ddl\TriggerFactory::class);
- $this->viewCollectionMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\View\CollectionInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $this->viewMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\ViewInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $this->resourceMock->expects($this->any())
- ->method('getTableName')
- ->will($this->returnArgument(0));
- $this->model = new Subscription(
- $this->resourceMock,
- $this->triggerFactoryMock,
- $this->viewCollectionMock,
- $this->viewMock,
- $this->tableName,
- 'columnName'
- );
- }
- public function testGetView()
- {
- $this->assertEquals($this->viewMock, $this->model->getView());
- }
- public function testGetTableName()
- {
- $this->assertEquals($this->tableName, $this->model->getTableName());
- }
- public function testGetColumnName()
- {
- $this->assertEquals('columnName', $this->model->getColumnName());
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testCreate()
- {
- $triggerName = 'trigger_name';
- $this->resourceMock->expects($this->atLeastOnce())->method('getTriggerName')->willReturn($triggerName);
- $triggerMock = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Trigger::class)
- ->setMethods(['setName', 'getName', 'setTime', 'setEvent', 'setTable', 'addStatement'])
- ->disableOriginalConstructor()
- ->getMock();
- $triggerMock->expects($this->exactly(3))
- ->method('setName')
- ->with($triggerName)
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('getName')
- ->will($this->returnValue('triggerName'));
- $triggerMock->expects($this->exactly(3))
- ->method('setTime')
- ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('setEvent')
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('setTable')
- ->with($this->tableName)
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(4))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(5))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(11))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(12))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(18))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (OLD.columnName);")
- ->will($this->returnSelf());
- $triggerMock->expects($this->at(19))
- ->method('addStatement')
- ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (OLD.columnName);")
- ->will($this->returnSelf());
- $changelogMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\View\ChangelogInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $changelogMock->expects($this->exactly(3))
- ->method('getName')
- ->will($this->returnValue('test_view_cl'));
- $changelogMock->expects($this->exactly(3))
- ->method('getColumnName')
- ->will($this->returnValue('entity_id'));
- $this->viewMock->expects($this->exactly(3))
- ->method('getChangelog')
- ->will($this->returnValue($changelogMock));
- $this->triggerFactoryMock->expects($this->exactly(3))
- ->method('create')
- ->will($this->returnValue($triggerMock));
- $otherChangelogMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\View\ChangelogInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $otherChangelogMock->expects($this->exactly(3))
- ->method('getName')
- ->will($this->returnValue('other_test_view_cl'));
- $otherChangelogMock->expects($this->exactly(3))
- ->method('getColumnName')
- ->will($this->returnValue('entity_id'));
- $otherViewMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\ViewInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $otherViewMock->expects($this->exactly(1))
- ->method('getId')
- ->will($this->returnValue('other_id'));
- $otherViewMock->expects($this->exactly(1))
- ->method('getSubscriptions')
- ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
- $otherViewMock->expects($this->exactly(3))
- ->method('getChangelog')
- ->will($this->returnValue($otherChangelogMock));
- $this->viewMock->expects($this->exactly(3))
- ->method('getId')
- ->will($this->returnValue('this_id'));
- $this->viewMock->expects($this->never())
- ->method('getSubscriptions');
- $this->viewCollectionMock->expects($this->exactly(1))
- ->method('getViewsByStateMode')
- ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
- ->will($this->returnValue([$this->viewMock, $otherViewMock]));
- $this->connectionMock->expects($this->exactly(3))
- ->method('dropTrigger')
- ->with('triggerName')
- ->will($this->returnValue(true));
- $this->connectionMock->expects($this->exactly(3))
- ->method('createTrigger')
- ->with($triggerMock);
- $this->model->create();
- }
- public function testRemove()
- {
- $triggerMock = $this->createMock(\Magento\Framework\DB\Ddl\Trigger::class);
- $triggerMock->expects($this->exactly(3))
- ->method('setName')
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('getName')
- ->will($this->returnValue('triggerName'));
- $triggerMock->expects($this->exactly(3))
- ->method('setTime')
- ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('setEvent')
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('setTable')
- ->with($this->tableName)
- ->will($this->returnSelf());
- $triggerMock->expects($this->exactly(3))
- ->method('addStatement')
- ->will($this->returnSelf());
- $this->triggerFactoryMock->expects($this->exactly(3))
- ->method('create')
- ->will($this->returnValue($triggerMock));
- $otherChangelogMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\View\ChangelogInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $otherChangelogMock->expects($this->exactly(3))
- ->method('getName')
- ->will($this->returnValue('other_test_view_cl'));
- $otherChangelogMock->expects($this->exactly(3))
- ->method('getColumnName')
- ->will($this->returnValue('entity_id'));
- $otherViewMock = $this->getMockForAbstractClass(
- \Magento\Framework\Mview\ViewInterface::class,
- [],
- '',
- false,
- false,
- true,
- []
- );
- $otherViewMock->expects($this->exactly(1))
- ->method('getId')
- ->will($this->returnValue('other_id'));
- $otherViewMock->expects($this->exactly(1))
- ->method('getSubscriptions')
- ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
- $otherViewMock->expects($this->exactly(3))
- ->method('getChangelog')
- ->will($this->returnValue($otherChangelogMock));
- $this->viewMock->expects($this->exactly(3))
- ->method('getId')
- ->will($this->returnValue('this_id'));
- $this->viewMock->expects($this->never())
- ->method('getSubscriptions');
- $this->viewCollectionMock->expects($this->exactly(1))
- ->method('getViewsByStateMode')
- ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
- ->will($this->returnValue([$this->viewMock, $otherViewMock]));
- $this->connectionMock->expects($this->exactly(3))
- ->method('dropTrigger')
- ->with('triggerName')
- ->will($this->returnValue(true));
- $triggerMock->expects($this->exactly(3))
- ->method('getStatements')
- ->will($this->returnValue(true));
- $this->connectionMock->expects($this->exactly(3))
- ->method('createTrigger')
- ->with($triggerMock);
- $this->model->remove();
- }
- }
|