PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/internal/Magento/Framework/Mview/Test/Unit/View/ChangelogTest.php

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