TriggerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Test\Unit\Ddl;
  7. class TriggerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\DB\Ddl\Trigger
  11. */
  12. protected $_object;
  13. protected function setUp()
  14. {
  15. $this->_object = new \Magento\Framework\DB\Ddl\Trigger();
  16. }
  17. /**
  18. * Test getListOfEvents() method
  19. */
  20. public function testGetListOfEvents()
  21. {
  22. $actualEventTypes = \Magento\Framework\DB\Ddl\Trigger::getListOfEvents();
  23. $this->assertInternalType('array', $actualEventTypes);
  24. $this->assertCount(3, $actualEventTypes);
  25. $this->assertTrue(in_array(\Magento\Framework\DB\Ddl\Trigger::EVENT_INSERT, $actualEventTypes));
  26. $this->assertTrue(in_array(\Magento\Framework\DB\Ddl\Trigger::EVENT_UPDATE, $actualEventTypes));
  27. $this->assertTrue(in_array(\Magento\Framework\DB\Ddl\Trigger::EVENT_DELETE, $actualEventTypes));
  28. }
  29. /**
  30. * Test getListOfTimes() method
  31. */
  32. public function testGetListOfTimes()
  33. {
  34. $actualTimeTypes = \Magento\Framework\DB\Ddl\Trigger::getListOfTimes();
  35. $this->assertInternalType('array', $actualTimeTypes);
  36. $this->assertCount(2, $actualTimeTypes);
  37. $this->assertTrue(in_array(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER, $actualTimeTypes));
  38. $this->assertTrue(in_array(\Magento\Framework\DB\Ddl\Trigger::TIME_BEFORE, $actualTimeTypes));
  39. }
  40. /**
  41. * Test case for getName() after setName()
  42. */
  43. public function testGetNameWithSetName()
  44. {
  45. $triggerName = 'TEST_TRIGGER_NAME' . mt_rand(100, 999);
  46. $this->_object->setName($triggerName);
  47. $this->assertEquals(strtolower($triggerName), $this->_object->getName());
  48. }
  49. /**
  50. * Test case for setName() with exception
  51. *
  52. * @expectedException \InvalidArgumentException
  53. * @expectedExceptionMessage Trigger name should be a string
  54. */
  55. public function testSetNameWithException()
  56. {
  57. $triggerName = new \stdClass();
  58. //non string
  59. $this->_object->setName($triggerName);
  60. }
  61. /**
  62. * Test case for setTable() with exception
  63. *
  64. * @expectedException \InvalidArgumentException
  65. * @expectedExceptionMessage Trigger table name should be a string
  66. */
  67. public function testSetTableWithException()
  68. {
  69. $tableName = new \stdClass();
  70. //non string
  71. $this->_object->setTable($tableName);
  72. }
  73. /**
  74. * Test for table name setter
  75. */
  76. public function testSetTableName()
  77. {
  78. $names = ['PREFIX_table', 'prefix_table'];
  79. foreach ($names as $name) {
  80. $this->_object->setTable($name);
  81. $this->assertEquals($name, $this->_object->getTable());
  82. }
  83. }
  84. /**
  85. * Test case for getName()
  86. *
  87. * @expectedException \Zend_Db_Exception
  88. * @expectedExceptionMessage Trigger name is not defined
  89. */
  90. public function testGetNameWithException()
  91. {
  92. $tableName = 'TEST_TABLE_NAME_' . mt_rand(100, 999);
  93. $event = \Magento\Framework\DB\Ddl\Trigger::EVENT_INSERT;
  94. $this->_object->setTable($tableName)->setTime(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)->setEvent($event);
  95. $this->_object->getName();
  96. }
  97. /**
  98. * Test case for getTime() with Exception
  99. *
  100. * @expectedException \Zend_Db_Exception
  101. * @expectedExceptionMessage Trigger time is not defined
  102. */
  103. public function testGetTimeWithException()
  104. {
  105. $tableName = 'TEST_TABLE_NAME_' . mt_rand(100, 999);
  106. $event = \Magento\Framework\DB\Ddl\Trigger::EVENT_INSERT;
  107. $this->_object->setTable($tableName)->setEvent($event);
  108. $this->_object->getTime();
  109. }
  110. /**
  111. * Test case for getTable()
  112. *
  113. * @expectedException \Zend_Db_Exception
  114. * @expectedExceptionMessage Trigger table name is not defined
  115. */
  116. public function testGetTableWithException()
  117. {
  118. $event = \Magento\Framework\DB\Ddl\Trigger::EVENT_INSERT;
  119. $this->_object->setTime(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER)->setEvent($event);
  120. $this->_object->getTable();
  121. }
  122. /**
  123. * Test case for getEvent() with Exception
  124. *
  125. * @expectedException \Zend_Db_Exception
  126. * @expectedExceptionMessage Trigger event is not defined
  127. */
  128. public function testGetEventWithException()
  129. {
  130. $tableName = 'TEST_TABLE_NAME_' . mt_rand(100, 999);
  131. $this->_object->setTable($tableName)->setTime(\Magento\Framework\DB\Ddl\Trigger::TIME_AFTER);
  132. $this->_object->getEvent();
  133. }
  134. /**
  135. * @expectedException InvalidArgumentException
  136. * @expectedExceptionMessage Trigger unsupported event type
  137. */
  138. public function testWrongEventTypeException()
  139. {
  140. $this->_object->setEvent('UNSUPORT EVENT TYPE');
  141. }
  142. /**
  143. * @expectedException InvalidArgumentException
  144. * @expectedExceptionMessage Trigger unsupported time type
  145. */
  146. public function testWrongTimeTypeException()
  147. {
  148. $this->_object->setTime('UNSUPORT TIME TYPE');
  149. }
  150. /**
  151. * Test case for setTable() with exception
  152. *
  153. * @expectedException \InvalidArgumentException
  154. * @expectedExceptionMessage Trigger statement should be a string
  155. */
  156. public function testAddStatementWithException()
  157. {
  158. $statement = new \stdClass();
  159. //non string
  160. $this->_object->addStatement($statement);
  161. }
  162. /**
  163. * Data provider for setBody function
  164. */
  165. public function getStatementsDataProvider()
  166. {
  167. return [['SQL', ['SQL;']], ['SQL;', ['SQL;']]];
  168. }
  169. /**
  170. * Test addStatement and getStatements for trigger
  171. *
  172. * @dataProvider getStatementsDataProvider
  173. */
  174. public function testAddStatement($param, $expected)
  175. {
  176. $this->_object->addStatement($param);
  177. $this->assertEquals($expected, $this->_object->getStatements());
  178. }
  179. /**
  180. * Test getStatements method
  181. */
  182. public function testGetStatements()
  183. {
  184. $this->assertEquals([], $this->_object->getStatements());
  185. }
  186. }