123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Mview\Test\Unit\View;
- class ChangelogTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\Mview\View\Changelog
- */
- protected $model;
- /**
- * Mysql PDO DB adapter mock
- *
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\Pdo\Mysql
- */
- protected $connectionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection
- */
- protected $resourceMock;
- protected function setUp()
- {
- $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
- $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
- $this->mockGetConnection($this->connectionMock);
- $this->model = new \Magento\Framework\Mview\View\Changelog($this->resourceMock);
- }
- public function testInstanceOf()
- {
- $resourceMock =
- $this->createMock(\Magento\Framework\App\ResourceConnection::class);
- $resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue(true));
- $model = new \Magento\Framework\Mview\View\Changelog($resourceMock);
- $this->assertInstanceOf(\Magento\Framework\Mview\View\ChangelogInterface::class, $model);
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage The write connection to the database isn't available. Please try again later.
- */
- public function testCheckConnectionException()
- {
- $resourceMock =
- $this->createMock(\Magento\Framework\App\ResourceConnection::class);
- $resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue(null));
- $model = new \Magento\Framework\Mview\View\Changelog($resourceMock);
- $model->setViewId('ViewIdTest');
- $this->assertNull($model);
- }
- public function testGetName()
- {
- $this->model->setViewId('ViewIdTest');
- $this->assertEquals(
- 'ViewIdTest' . '_' . \Magento\Framework\Mview\View\Changelog::NAME_SUFFIX,
- $this->model->getName()
- );
- }
- public function testGetViewId()
- {
- $this->model->setViewId('ViewIdTest');
- $this->assertEquals('ViewIdTest', $this->model->getViewId());
- }
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage View's identifier is not set
- */
- public function testGetNameWithException()
- {
- $this->model->getName();
- }
- public function testGetColumnName()
- {
- $this->assertEquals(\Magento\Framework\Mview\View\Changelog::COLUMN_NAME, $this->model->getColumnName());
- }
- public function testGetVersion()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, true);
- $this->mockGetTableName();
- $this->connectionMock->expects($this->once())
- ->method('fetchRow')
- ->will($this->returnValue(['Auto_increment' => 11]));
- $this->model->setViewId('viewIdtest');
- $this->assertEquals(10, $this->model->getVersion());
- }
- public function testGetVersionWithExceptionNoAutoincrement()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, true);
- $this->mockGetTableName();
- $this->connectionMock->expects($this->once())
- ->method('fetchRow')
- ->will($this->returnValue([]));
- $this->expectException('Exception');
- $this->expectExceptionMessage("Table status for `{$changelogTableName}` is incorrect. Can`t fetch version id.");
- $this->model->setViewId('viewIdtest');
- $this->model->getVersion();
- }
- public function testGetVersionWithExceptionNoTable()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, false);
- $this->mockGetTableName();
- $this->expectException('Exception');
- $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
- $this->model->setViewId('viewIdtest');
- $this->model->getVersion();
- }
- public function testDrop()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, false);
- $this->mockGetTableName();
- $this->expectException('Exception');
- $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
- $this->model->setViewId('viewIdtest');
- $this->model->drop();
- }
- public function testDropWithException()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, true);
- $this->mockGetTableName();
- $this->connectionMock->expects($this->once())
- ->method('dropTable')
- ->with($changelogTableName)
- ->will($this->returnValue(true));
- $this->model->setViewId('viewIdtest');
- $this->model->drop();
- }
- public function testCreate()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, false);
- $this->mockGetTableName();
- $tableMock = $this->createMock(\Magento\Framework\DB\Ddl\Table::class);
- $tableMock->expects($this->exactly(2))
- ->method('addColumn')
- ->will($this->returnSelf());
- $this->connectionMock->expects($this->once())
- ->method('newTable')
- ->with($changelogTableName)
- ->will($this->returnValue($tableMock));
- $this->connectionMock->expects($this->once())
- ->method('createTable')
- ->with($tableMock);
- $this->model->setViewId('viewIdtest');
- $this->model->create();
- }
- public function testCreateWithExistingTable()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, true);
- $this->mockGetTableName();
- $this->connectionMock->expects($this->never())->method('createTable');
- $this->model->setViewId('viewIdtest');
- $this->model->create();
- }
- public function testGetList()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, true);
- $this->mockGetTableName();
- $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
- $selectMock->expects($this->once())
- ->method('distinct')
- ->with(true)
- ->will($this->returnSelf());
- $selectMock->expects($this->once())
- ->method('from')
- ->with($changelogTableName, ['entity_id'])
- ->will($this->returnSelf());
- $selectMock->expects($this->exactly(2))
- ->method('where')
- ->will($this->returnSelf());
- $this->connectionMock->expects($this->once())
- ->method('select')
- ->will($this->returnValue($selectMock));
- $this->connectionMock->expects($this->once())
- ->method('fetchCol')
- ->with($selectMock)
- ->will($this->returnValue(['some_data']));
- $this->model->setViewId('viewIdtest');
- $this->assertEquals(['some_data'], $this->model->getList(1, 2));
- }
- public function testGetListWithException()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, false);
- $this->mockGetTableName();
- $this->expectException('Exception');
- $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
- $this->model->setViewId('viewIdtest');
- $this->model->getList(mt_rand(1, 200), mt_rand(201, 400));
- }
- public function testClearWithException()
- {
- $changelogTableName = 'viewIdtest_cl';
- $this->mockIsTableExists($changelogTableName, false);
- $this->mockGetTableName();
- $this->expectException('Exception');
- $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
- $this->model->setViewId('viewIdtest');
- $this->model->clear(mt_rand(1, 200));
- }
- /**
- * @param $connection
- */
- protected function mockGetConnection($connection)
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
- }
- protected function mockGetTableName()
- {
- $this->resourceMock->expects($this->once())->method('getTableName')->will($this->returnArgument(0));
- }
- /**
- * @param $changelogTableName
- * @param $result
- */
- protected function mockIsTableExists($changelogTableName, $result)
- {
- $this->connectionMock->expects(
- $this->once()
- )->method(
- 'isTableExists'
- )->with(
- $this->equalTo($changelogTableName)
- )->will(
- $this->returnValue($result)
- );
- }
- }
|