/tests/MigrationManagerHelperTest.php

https://github.com/eugenix/dbmigrator · PHP · 188 lines · 133 code · 25 blank · 30 comment · 2 complexity · 799557c75e3006e508a2826dac72470b MD5 · raw file

  1. <?php
  2. require_once '../Helpers/MigrationManagerHelper.php';
  3. /**
  4. * Test class for MigrationManagerHelper.
  5. * Generated by PHPUnit on 2011-05-24 at 17:08:26.
  6. */
  7. class MigrationManagerHelperTest extends PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var MigrationManagerHelper
  11. */
  12. protected $object;
  13. protected $dbName = "testmigration";
  14. /**
  15. * Sets up the fixture, for example, opens a network connection.
  16. * This method is called before a test is executed.
  17. */
  18. protected function setUp()
  19. {
  20. $this->object = new MigrationManagerHelper('localhost', 'root', 'toor', $this->dbName);
  21. }
  22. /**
  23. * Tears down the fixture, for example, closes a network connection.
  24. * This method is called after a test is executed.
  25. */
  26. protected function tearDown()
  27. {
  28. if ($this->object != null)
  29. $this->object->__destruct();
  30. }
  31. /**
  32. * @todo Implement testCreateTable().
  33. */
  34. public function testCreateTable()
  35. {
  36. $this->object->createTable();
  37. $res = $this->object->executeQuery("
  38. SELECT COUNT(*) AS count
  39. FROM information_schema.tables
  40. WHERE table_schema = '{$this->dbName}'
  41. AND table_name = '__migration'
  42. ");
  43. $this->assertEquals(1, mysql_result($res, 0), "Table migration must be exist");
  44. }
  45. /**
  46. * @todo Implement testMakeDBEmpty().
  47. */
  48. public function testMakeDBEmpty()
  49. {
  50. $this->object->makeDBEmpty();
  51. $res = $this->object->executeQuery("
  52. SELECT COUNT(*) AS count
  53. FROM information_schema.tables
  54. WHERE table_schema = '{$this->dbName}'
  55. ");
  56. $this->assertEquals(0, mysql_result($res, 0), "No tables");
  57. $res = $this->object->executeQuery("
  58. SELECT COUNT(*) AS COUNT
  59. FROM information_schema.triggers
  60. WHERE trigger_schema = '{$this->dbName}'
  61. ");
  62. $this->assertEquals(0, mysql_result($res, 0), "No triggers");
  63. $res = $this->object->executeQuery("
  64. SELECT COUNT(*) AS COUNT
  65. FROM information_schema.routines
  66. WHERE routine_schema = '{$this->dbName}'
  67. ");
  68. $this->assertEquals(0, mysql_result($res, 0), "No procedures and functions");
  69. }
  70. /**
  71. * @todo Implement testCheckDBError().
  72. */
  73. public function testCheckDBError()
  74. {
  75. try
  76. {
  77. $this->object->executeQuery("INVALID SQL");
  78. $this->fail("Must be exception");
  79. }
  80. catch (Exception $e)
  81. {
  82. $this->assertTrue(true, "Mysql exeptions");
  83. }
  84. }
  85. public function testCreateDump()
  86. {
  87. MigrationManagerHelper::cleanMigrDir('data/storage/0');
  88. $this->object->createDump('data/storage/0');
  89. $this->assertTrue(is_writable('data/storage/0/data.sql'));
  90. $this->assertTrue(is_writable('data/storage/0/procedures.sql'));
  91. $this->assertTrue(is_writable('data/storage/0/scheme.sql'));
  92. $this->assertTrue(is_writable('data/storage/0/triggers.sql'));
  93. }
  94. /**
  95. * @todo Implement testImportFiles().
  96. */
  97. public function testImportFiles()
  98. {
  99. $this->object->makeDBEmpty();
  100. $this->object->createTable();
  101. $this->object->importFiles('data/deltas/0',
  102. array('scheme.sql', 'data.sql', 'procedures.sql', 'triggers.sql'));
  103. $res = $this->object->executeQuery("
  104. SELECT COUNT(*) AS count
  105. FROM information_schema.tables
  106. WHERE table_schema = '{$this->dbName}'
  107. ");
  108. $this->assertEquals(24, mysql_result($res, 0), "24 tables");
  109. $res = $this->object->executeQuery("
  110. SELECT COUNT(*) AS COUNT
  111. FROM information_schema.triggers
  112. WHERE trigger_schema = '{$this->dbName}'
  113. ");
  114. $this->assertEquals(6, mysql_result($res, 0), "6 triggers");
  115. $res = $this->object->executeQuery("
  116. SELECT COUNT(*) AS COUNT
  117. FROM information_schema.routines
  118. WHERE routine_schema = '{$this->dbName}'
  119. ");
  120. $this->assertEquals(6, mysql_result($res, 0), "6 procedures");
  121. $this->object->makeDBEmpty();
  122. }
  123. /**
  124. * @todo Implement testCheckDir().
  125. */
  126. public function testCheckDir()
  127. {
  128. try
  129. {
  130. $this->object->checkDir('data/storage', 'test_migration');
  131. $this->assertTrue(true);
  132. }
  133. catch (Exception $e)
  134. {
  135. $this->fail('Thats ok');
  136. }
  137. }
  138. public function testCreateDeltaByBinaryLog()
  139. {
  140. $this->markTestSkipped();
  141. }
  142. public function testFilterQueries()
  143. {
  144. $queries = array(
  145. "/*!40019 SET @@session.max_insert_delayed_threads=0*/;",
  146. "use testmigration",
  147. "SET TIMESTAMP=1307451576",
  148. "SET @@session.pseudo_thread_id=999999999",
  149. "SET @@session.sql_mode=0",
  150. "/*!\C utf8 */",
  151. "BEGIN",
  152. "INSERT INTO migration (number, createTime, comment)
  153. VALUES (0, unix_timestamp(NOW()), 'Init migration')",
  154. "COMMIT",
  155. "DELIMITER ;",
  156. "ALTER TABLE `testmigration`.`actor` CHANGE `first_name` `first_name` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL",
  157. "ROLLBACK /* added by mysqlbinlog */;"
  158. );
  159. $queries = $this->object->filterQueries($queries);
  160. $this->assertEquals(6, count($queries));
  161. }
  162. }
  163. ?>