ChangelogTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\View;
  7. class ChangelogTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Mview\View\Changelog
  11. */
  12. protected $model;
  13. /**
  14. * Mysql PDO DB adapter mock
  15. *
  16. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\Pdo\Mysql
  17. */
  18. protected $connectionMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection
  21. */
  22. protected $resourceMock;
  23. protected function setUp()
  24. {
  25. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  26. $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  27. $this->mockGetConnection($this->connectionMock);
  28. $this->model = new \Magento\Framework\Mview\View\Changelog($this->resourceMock);
  29. }
  30. public function testInstanceOf()
  31. {
  32. $resourceMock =
  33. $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  34. $resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue(true));
  35. $model = new \Magento\Framework\Mview\View\Changelog($resourceMock);
  36. $this->assertInstanceOf(\Magento\Framework\Mview\View\ChangelogInterface::class, $model);
  37. }
  38. /**
  39. * @expectedException \Exception
  40. * @expectedExceptionMessage The write connection to the database isn't available. Please try again later.
  41. */
  42. public function testCheckConnectionException()
  43. {
  44. $resourceMock =
  45. $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  46. $resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue(null));
  47. $model = new \Magento\Framework\Mview\View\Changelog($resourceMock);
  48. $model->setViewId('ViewIdTest');
  49. $this->assertNull($model);
  50. }
  51. public function testGetName()
  52. {
  53. $this->model->setViewId('ViewIdTest');
  54. $this->assertEquals(
  55. 'ViewIdTest' . '_' . \Magento\Framework\Mview\View\Changelog::NAME_SUFFIX,
  56. $this->model->getName()
  57. );
  58. }
  59. public function testGetViewId()
  60. {
  61. $this->model->setViewId('ViewIdTest');
  62. $this->assertEquals('ViewIdTest', $this->model->getViewId());
  63. }
  64. /**
  65. * @expectedException \Exception
  66. * @expectedExceptionMessage View's identifier is not set
  67. */
  68. public function testGetNameWithException()
  69. {
  70. $this->model->getName();
  71. }
  72. public function testGetColumnName()
  73. {
  74. $this->assertEquals(\Magento\Framework\Mview\View\Changelog::COLUMN_NAME, $this->model->getColumnName());
  75. }
  76. public function testGetVersion()
  77. {
  78. $changelogTableName = 'viewIdtest_cl';
  79. $this->mockIsTableExists($changelogTableName, true);
  80. $this->mockGetTableName();
  81. $this->connectionMock->expects($this->once())
  82. ->method('fetchRow')
  83. ->will($this->returnValue(['Auto_increment' => 11]));
  84. $this->model->setViewId('viewIdtest');
  85. $this->assertEquals(10, $this->model->getVersion());
  86. }
  87. public function testGetVersionWithExceptionNoAutoincrement()
  88. {
  89. $changelogTableName = 'viewIdtest_cl';
  90. $this->mockIsTableExists($changelogTableName, true);
  91. $this->mockGetTableName();
  92. $this->connectionMock->expects($this->once())
  93. ->method('fetchRow')
  94. ->will($this->returnValue([]));
  95. $this->expectException('Exception');
  96. $this->expectExceptionMessage("Table status for `{$changelogTableName}` is incorrect. Can`t fetch version id.");
  97. $this->model->setViewId('viewIdtest');
  98. $this->model->getVersion();
  99. }
  100. public function testGetVersionWithExceptionNoTable()
  101. {
  102. $changelogTableName = 'viewIdtest_cl';
  103. $this->mockIsTableExists($changelogTableName, false);
  104. $this->mockGetTableName();
  105. $this->expectException('Exception');
  106. $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
  107. $this->model->setViewId('viewIdtest');
  108. $this->model->getVersion();
  109. }
  110. public function testDrop()
  111. {
  112. $changelogTableName = 'viewIdtest_cl';
  113. $this->mockIsTableExists($changelogTableName, false);
  114. $this->mockGetTableName();
  115. $this->expectException('Exception');
  116. $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
  117. $this->model->setViewId('viewIdtest');
  118. $this->model->drop();
  119. }
  120. public function testDropWithException()
  121. {
  122. $changelogTableName = 'viewIdtest_cl';
  123. $this->mockIsTableExists($changelogTableName, true);
  124. $this->mockGetTableName();
  125. $this->connectionMock->expects($this->once())
  126. ->method('dropTable')
  127. ->with($changelogTableName)
  128. ->will($this->returnValue(true));
  129. $this->model->setViewId('viewIdtest');
  130. $this->model->drop();
  131. }
  132. public function testCreate()
  133. {
  134. $changelogTableName = 'viewIdtest_cl';
  135. $this->mockIsTableExists($changelogTableName, false);
  136. $this->mockGetTableName();
  137. $tableMock = $this->createMock(\Magento\Framework\DB\Ddl\Table::class);
  138. $tableMock->expects($this->exactly(2))
  139. ->method('addColumn')
  140. ->will($this->returnSelf());
  141. $this->connectionMock->expects($this->once())
  142. ->method('newTable')
  143. ->with($changelogTableName)
  144. ->will($this->returnValue($tableMock));
  145. $this->connectionMock->expects($this->once())
  146. ->method('createTable')
  147. ->with($tableMock);
  148. $this->model->setViewId('viewIdtest');
  149. $this->model->create();
  150. }
  151. public function testCreateWithExistingTable()
  152. {
  153. $changelogTableName = 'viewIdtest_cl';
  154. $this->mockIsTableExists($changelogTableName, true);
  155. $this->mockGetTableName();
  156. $this->connectionMock->expects($this->never())->method('createTable');
  157. $this->model->setViewId('viewIdtest');
  158. $this->model->create();
  159. }
  160. public function testGetList()
  161. {
  162. $changelogTableName = 'viewIdtest_cl';
  163. $this->mockIsTableExists($changelogTableName, true);
  164. $this->mockGetTableName();
  165. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  166. $selectMock->expects($this->once())
  167. ->method('distinct')
  168. ->with(true)
  169. ->will($this->returnSelf());
  170. $selectMock->expects($this->once())
  171. ->method('from')
  172. ->with($changelogTableName, ['entity_id'])
  173. ->will($this->returnSelf());
  174. $selectMock->expects($this->exactly(2))
  175. ->method('where')
  176. ->will($this->returnSelf());
  177. $this->connectionMock->expects($this->once())
  178. ->method('select')
  179. ->will($this->returnValue($selectMock));
  180. $this->connectionMock->expects($this->once())
  181. ->method('fetchCol')
  182. ->with($selectMock)
  183. ->will($this->returnValue(['some_data']));
  184. $this->model->setViewId('viewIdtest');
  185. $this->assertEquals(['some_data'], $this->model->getList(1, 2));
  186. }
  187. public function testGetListWithException()
  188. {
  189. $changelogTableName = 'viewIdtest_cl';
  190. $this->mockIsTableExists($changelogTableName, false);
  191. $this->mockGetTableName();
  192. $this->expectException('Exception');
  193. $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
  194. $this->model->setViewId('viewIdtest');
  195. $this->model->getList(mt_rand(1, 200), mt_rand(201, 400));
  196. }
  197. public function testClearWithException()
  198. {
  199. $changelogTableName = 'viewIdtest_cl';
  200. $this->mockIsTableExists($changelogTableName, false);
  201. $this->mockGetTableName();
  202. $this->expectException('Exception');
  203. $this->expectExceptionMessage("Table {$changelogTableName} does not exist");
  204. $this->model->setViewId('viewIdtest');
  205. $this->model->clear(mt_rand(1, 200));
  206. }
  207. /**
  208. * @param $connection
  209. */
  210. protected function mockGetConnection($connection)
  211. {
  212. $this->resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
  213. }
  214. protected function mockGetTableName()
  215. {
  216. $this->resourceMock->expects($this->once())->method('getTableName')->will($this->returnArgument(0));
  217. }
  218. /**
  219. * @param $changelogTableName
  220. * @param $result
  221. */
  222. protected function mockIsTableExists($changelogTableName, $result)
  223. {
  224. $this->connectionMock->expects(
  225. $this->once()
  226. )->method(
  227. 'isTableExists'
  228. )->with(
  229. $this->equalTo($changelogTableName)
  230. )->will(
  231. $this->returnValue($result)
  232. );
  233. }
  234. }