SubscriptionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. use \Magento\Framework\Mview\View\Subscription;
  8. class SubscriptionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * Mysql PDO DB adapter mock
  12. *
  13. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\Pdo\Mysql
  14. */
  15. protected $connectionMock;
  16. /** @var \Magento\Framework\Mview\View\Subscription */
  17. protected $model;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResourceConnection */
  19. protected $resourceMock;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Ddl\TriggerFactory */
  21. protected $triggerFactoryMock;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\View\CollectionInterface */
  23. protected $viewCollectionMock;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Mview\ViewInterface */
  25. protected $viewMock;
  26. /** @var string */
  27. private $tableName;
  28. protected function setUp()
  29. {
  30. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  31. $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  32. $this->connectionMock->expects($this->any())
  33. ->method('quoteIdentifier')
  34. ->will($this->returnArgument(0));
  35. $this->resourceMock->expects($this->atLeastOnce())
  36. ->method('getConnection')
  37. ->willReturn($this->connectionMock);
  38. $this->triggerFactoryMock = $this->createMock(\Magento\Framework\DB\Ddl\TriggerFactory::class);
  39. $this->viewCollectionMock = $this->getMockForAbstractClass(
  40. \Magento\Framework\Mview\View\CollectionInterface::class,
  41. [],
  42. '',
  43. false,
  44. false,
  45. true,
  46. []
  47. );
  48. $this->viewMock = $this->getMockForAbstractClass(
  49. \Magento\Framework\Mview\ViewInterface::class,
  50. [],
  51. '',
  52. false,
  53. false,
  54. true,
  55. []
  56. );
  57. $this->resourceMock->expects($this->any())
  58. ->method('getTableName')
  59. ->will($this->returnArgument(0));
  60. $this->model = new Subscription(
  61. $this->resourceMock,
  62. $this->triggerFactoryMock,
  63. $this->viewCollectionMock,
  64. $this->viewMock,
  65. $this->tableName,
  66. 'columnName'
  67. );
  68. }
  69. public function testGetView()
  70. {
  71. $this->assertEquals($this->viewMock, $this->model->getView());
  72. }
  73. public function testGetTableName()
  74. {
  75. $this->assertEquals($this->tableName, $this->model->getTableName());
  76. }
  77. public function testGetColumnName()
  78. {
  79. $this->assertEquals('columnName', $this->model->getColumnName());
  80. }
  81. /**
  82. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  83. */
  84. public function testCreate()
  85. {
  86. $triggerName = 'trigger_name';
  87. $this->resourceMock->expects($this->atLeastOnce())->method('getTriggerName')->willReturn($triggerName);
  88. $triggerMock = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Trigger::class)
  89. ->setMethods(['setName', 'getName', 'setTime', 'setEvent', 'setTable', 'addStatement'])
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $triggerMock->expects($this->exactly(3))
  93. ->method('setName')
  94. ->with($triggerName)
  95. ->will($this->returnSelf());
  96. $triggerMock->expects($this->exactly(3))
  97. ->method('getName')
  98. ->will($this->returnValue('triggerName'));
  99. $triggerMock->expects($this->exactly(3))
  100. ->method('setTime')
  101. ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
  102. ->will($this->returnSelf());
  103. $triggerMock->expects($this->exactly(3))
  104. ->method('setEvent')
  105. ->will($this->returnSelf());
  106. $triggerMock->expects($this->exactly(3))
  107. ->method('setTable')
  108. ->with($this->tableName)
  109. ->will($this->returnSelf());
  110. $triggerMock->expects($this->at(4))
  111. ->method('addStatement')
  112. ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
  113. ->will($this->returnSelf());
  114. $triggerMock->expects($this->at(5))
  115. ->method('addStatement')
  116. ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
  117. ->will($this->returnSelf());
  118. $triggerMock->expects($this->at(11))
  119. ->method('addStatement')
  120. ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (NEW.columnName);")
  121. ->will($this->returnSelf());
  122. $triggerMock->expects($this->at(12))
  123. ->method('addStatement')
  124. ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (NEW.columnName);")
  125. ->will($this->returnSelf());
  126. $triggerMock->expects($this->at(18))
  127. ->method('addStatement')
  128. ->with("INSERT IGNORE INTO test_view_cl (entity_id) VALUES (OLD.columnName);")
  129. ->will($this->returnSelf());
  130. $triggerMock->expects($this->at(19))
  131. ->method('addStatement')
  132. ->with("INSERT IGNORE INTO other_test_view_cl (entity_id) VALUES (OLD.columnName);")
  133. ->will($this->returnSelf());
  134. $changelogMock = $this->getMockForAbstractClass(
  135. \Magento\Framework\Mview\View\ChangelogInterface::class,
  136. [],
  137. '',
  138. false,
  139. false,
  140. true,
  141. []
  142. );
  143. $changelogMock->expects($this->exactly(3))
  144. ->method('getName')
  145. ->will($this->returnValue('test_view_cl'));
  146. $changelogMock->expects($this->exactly(3))
  147. ->method('getColumnName')
  148. ->will($this->returnValue('entity_id'));
  149. $this->viewMock->expects($this->exactly(3))
  150. ->method('getChangelog')
  151. ->will($this->returnValue($changelogMock));
  152. $this->triggerFactoryMock->expects($this->exactly(3))
  153. ->method('create')
  154. ->will($this->returnValue($triggerMock));
  155. $otherChangelogMock = $this->getMockForAbstractClass(
  156. \Magento\Framework\Mview\View\ChangelogInterface::class,
  157. [],
  158. '',
  159. false,
  160. false,
  161. true,
  162. []
  163. );
  164. $otherChangelogMock->expects($this->exactly(3))
  165. ->method('getName')
  166. ->will($this->returnValue('other_test_view_cl'));
  167. $otherChangelogMock->expects($this->exactly(3))
  168. ->method('getColumnName')
  169. ->will($this->returnValue('entity_id'));
  170. $otherViewMock = $this->getMockForAbstractClass(
  171. \Magento\Framework\Mview\ViewInterface::class,
  172. [],
  173. '',
  174. false,
  175. false,
  176. true,
  177. []
  178. );
  179. $otherViewMock->expects($this->exactly(1))
  180. ->method('getId')
  181. ->will($this->returnValue('other_id'));
  182. $otherViewMock->expects($this->exactly(1))
  183. ->method('getSubscriptions')
  184. ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
  185. $otherViewMock->expects($this->exactly(3))
  186. ->method('getChangelog')
  187. ->will($this->returnValue($otherChangelogMock));
  188. $this->viewMock->expects($this->exactly(3))
  189. ->method('getId')
  190. ->will($this->returnValue('this_id'));
  191. $this->viewMock->expects($this->never())
  192. ->method('getSubscriptions');
  193. $this->viewCollectionMock->expects($this->exactly(1))
  194. ->method('getViewsByStateMode')
  195. ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
  196. ->will($this->returnValue([$this->viewMock, $otherViewMock]));
  197. $this->connectionMock->expects($this->exactly(3))
  198. ->method('dropTrigger')
  199. ->with('triggerName')
  200. ->will($this->returnValue(true));
  201. $this->connectionMock->expects($this->exactly(3))
  202. ->method('createTrigger')
  203. ->with($triggerMock);
  204. $this->model->create();
  205. }
  206. public function testRemove()
  207. {
  208. $triggerMock = $this->createMock(\Magento\Framework\DB\Ddl\Trigger::class);
  209. $triggerMock->expects($this->exactly(3))
  210. ->method('setName')
  211. ->will($this->returnSelf());
  212. $triggerMock->expects($this->exactly(3))
  213. ->method('getName')
  214. ->will($this->returnValue('triggerName'));
  215. $triggerMock->expects($this->exactly(3))
  216. ->method('setTime')
  217. ->with(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)
  218. ->will($this->returnSelf());
  219. $triggerMock->expects($this->exactly(3))
  220. ->method('setEvent')
  221. ->will($this->returnSelf());
  222. $triggerMock->expects($this->exactly(3))
  223. ->method('setTable')
  224. ->with($this->tableName)
  225. ->will($this->returnSelf());
  226. $triggerMock->expects($this->exactly(3))
  227. ->method('addStatement')
  228. ->will($this->returnSelf());
  229. $this->triggerFactoryMock->expects($this->exactly(3))
  230. ->method('create')
  231. ->will($this->returnValue($triggerMock));
  232. $otherChangelogMock = $this->getMockForAbstractClass(
  233. \Magento\Framework\Mview\View\ChangelogInterface::class,
  234. [],
  235. '',
  236. false,
  237. false,
  238. true,
  239. []
  240. );
  241. $otherChangelogMock->expects($this->exactly(3))
  242. ->method('getName')
  243. ->will($this->returnValue('other_test_view_cl'));
  244. $otherChangelogMock->expects($this->exactly(3))
  245. ->method('getColumnName')
  246. ->will($this->returnValue('entity_id'));
  247. $otherViewMock = $this->getMockForAbstractClass(
  248. \Magento\Framework\Mview\ViewInterface::class,
  249. [],
  250. '',
  251. false,
  252. false,
  253. true,
  254. []
  255. );
  256. $otherViewMock->expects($this->exactly(1))
  257. ->method('getId')
  258. ->will($this->returnValue('other_id'));
  259. $otherViewMock->expects($this->exactly(1))
  260. ->method('getSubscriptions')
  261. ->will($this->returnValue([['name' => $this->tableName], ['name' => 'otherTableName']]));
  262. $otherViewMock->expects($this->exactly(3))
  263. ->method('getChangelog')
  264. ->will($this->returnValue($otherChangelogMock));
  265. $this->viewMock->expects($this->exactly(3))
  266. ->method('getId')
  267. ->will($this->returnValue('this_id'));
  268. $this->viewMock->expects($this->never())
  269. ->method('getSubscriptions');
  270. $this->viewCollectionMock->expects($this->exactly(1))
  271. ->method('getViewsByStateMode')
  272. ->with(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)
  273. ->will($this->returnValue([$this->viewMock, $otherViewMock]));
  274. $this->connectionMock->expects($this->exactly(3))
  275. ->method('dropTrigger')
  276. ->with('triggerName')
  277. ->will($this->returnValue(true));
  278. $triggerMock->expects($this->exactly(3))
  279. ->method('getStatements')
  280. ->will($this->returnValue(true));
  281. $this->connectionMock->expects($this->exactly(3))
  282. ->method('createTrigger')
  283. ->with($triggerMock);
  284. $this->model->remove();
  285. }
  286. }