/framework/Db/test/Horde/Db/Migration/MigratorTest.php

https://github.com/ewandor/horde · PHP · 217 lines · 158 code · 37 blank · 22 comment · 0 complexity · 0c3367e3aa75e4bde67f042cb69e6241 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2007 Maintainable Software, LLC
  4. * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  5. *
  6. * @author Mike Naberezny <mike@maintainable.com>
  7. * @author Derek DeVries <derek@maintainable.com>
  8. * @author Chuck Hagenbuch <chuck@horde.org>
  9. * @license http://www.horde.org/licenses/bsd
  10. * @category Horde
  11. * @package Db
  12. * @subpackage UnitTests
  13. */
  14. /**
  15. * @author Mike Naberezny <mike@maintainable.com>
  16. * @author Derek DeVries <derek@maintainable.com>
  17. * @author Chuck Hagenbuch <chuck@horde.org>
  18. * @license http://www.horde.org/licenses/bsd
  19. * @group horde_db
  20. * @category Horde
  21. * @package Db
  22. * @subpackage UnitTests
  23. */
  24. class Horde_Db_Migration_MigratorTest extends PHPUnit_Framework_TestCase
  25. {
  26. public function setUp()
  27. {
  28. try {
  29. $this->_conn = new Horde_Db_Adapter_Pdo_Sqlite(array(
  30. 'dbname' => ':memory:',
  31. ));
  32. } catch (Horde_Db_Exception $e) {
  33. $this->markTestSkipped('The sqlite adapter is not available');
  34. }
  35. $table = $this->_conn->createTable('users');
  36. $table->column('company_id', 'integer', array('limit' => 11));
  37. $table->column('name', 'string', array('limit' => 255, 'default' => ''));
  38. $table->column('first_name', 'string', array('limit' => 40, 'default' => ''));
  39. $table->column('approved', 'boolean', array('default' => true));
  40. $table->column('type', 'string', array('limit' => 255, 'default' => ''));
  41. $table->column('created_at', 'datetime', array('default' => '0000-00-00 00:00:00'));
  42. $table->column('created_on', 'date', array('default' => '0000-00-00'));
  43. $table->column('updated_at', 'datetime', array('default' => '0000-00-00 00:00:00'));
  44. $table->column('updated_on', 'date', array('default' => '0000-00-00'));
  45. $table->end();
  46. }
  47. public function testInitializeSchemaInformation()
  48. {
  49. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  50. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  51. $sql = "SELECT version FROM schema_info";
  52. $this->assertEquals(0, $this->_conn->selectValue($sql));
  53. }
  54. public function testMigrator()
  55. {
  56. $columns = $this->_columnNames('users');
  57. $this->assertFalse(in_array('last_name', $columns));
  58. $e = null;
  59. try {
  60. $this->_conn->selectValues("SELECT * FROM reminders");
  61. } catch (Exception $e) {}
  62. $this->assertInstanceOf('Horde_Db_Exception', $e);
  63. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  64. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  65. $migrator->up();
  66. $this->assertEquals(3, $migrator->getCurrentVersion());
  67. $columns = $this->_columnNames('users');
  68. $this->assertTrue(in_array('last_name', $columns));
  69. $this->_conn->insert("INSERT INTO reminders (content, remind_at) VALUES ('hello world', '2005-01-01 02:22:23')");
  70. $reminder = (object)$this->_conn->selectOne('SELECT * FROM reminders');
  71. $this->assertEquals('hello world', $reminder->content);
  72. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  73. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  74. $migrator->down();
  75. $this->assertEquals(0, $migrator->getCurrentVersion());
  76. $columns = $this->_columnNames('users');
  77. $this->assertFalse(in_array('last_name', $columns));
  78. $e = null;
  79. try {
  80. $this->_conn->selectValues("SELECT * FROM reminders");
  81. } catch (Exception $e) {}
  82. $this->assertInstanceOf('Horde_Db_Exception', $e);
  83. }
  84. public function testOneUp()
  85. {
  86. $e = null;
  87. try {
  88. $this->_conn->selectValues("SELECT * FROM reminders");
  89. } catch (Exception $e) {}
  90. $this->assertInstanceOf('Horde_Db_Exception', $e);
  91. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  92. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  93. $migrator->up(1);
  94. $this->assertEquals(1, $migrator->getCurrentVersion());
  95. $columns = $this->_columnNames('users');
  96. $this->assertTrue(in_array('last_name', $columns));
  97. $e = null;
  98. try {
  99. $this->_conn->selectValues("SELECT * FROM reminders");
  100. } catch (Exception $e) {}
  101. $this->assertInstanceOf('Horde_Db_Exception', $e);
  102. $migrator->up(2);
  103. $this->assertEquals(2, $migrator->getCurrentVersion());
  104. $this->_conn->insert("INSERT INTO reminders (content, remind_at) VALUES ('hello world', '2005-01-01 02:22:23')");
  105. $reminder = (object)$this->_conn->selectOne('SELECT * FROM reminders');
  106. $this->assertEquals('hello world', $reminder->content);
  107. }
  108. public function testOneDown()
  109. {
  110. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  111. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  112. $migrator->up();
  113. $migrator->down(1);
  114. $columns = $this->_columnNames('users');
  115. $this->assertTrue(in_array('last_name', $columns));
  116. }
  117. public function testOneUpOneDown()
  118. {
  119. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  120. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  121. $migrator->up(1);
  122. $migrator->down(0);
  123. $columns = $this->_columnNames('users');
  124. $this->assertFalse(in_array('last_name', $columns));
  125. }
  126. public function testMigratorGoingDownDueToVersionTarget()
  127. {
  128. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
  129. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  130. $migrator->up(1);
  131. $migrator->down(0);
  132. $columns = $this->_columnNames('users');
  133. $this->assertFalse(in_array('last_name', $columns));
  134. $e = null;
  135. try {
  136. $this->_conn->selectValues("SELECT * FROM reminders");
  137. } catch (Exception $e) {}
  138. $this->assertInstanceOf('Horde_Db_Exception', $e);
  139. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  140. $migrator->up();
  141. $columns = $this->_columnNames('users');
  142. $this->assertTrue(in_array('last_name', $columns));
  143. $this->_conn->insert("INSERT INTO reminders (content, remind_at) VALUES ('hello world', '2005-01-01 02:22:23')");
  144. $reminder = (object)$this->_conn->selectOne('SELECT * FROM reminders');
  145. $this->assertEquals('hello world', $reminder->content);
  146. }
  147. public function testWithDuplicates()
  148. {
  149. try {
  150. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations_with_duplicate/';
  151. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  152. $migrator->up();
  153. } catch (Exception $e) { return; }
  154. $this->fail('Expected exception wasn\'t raised');
  155. }
  156. public function testWithMissingVersionNumbers()
  157. {
  158. $dir = dirname(dirname(__FILE__)).'/fixtures/migrations_with_missing_versions/';
  159. $migrator = new Horde_Db_Migration_Migrator($this->_conn, null, array('migrationsPath' => $dir));
  160. $migrator->migrate(500);
  161. $this->assertEquals(4, $migrator->getCurrentVersion());
  162. $migrator->migrate(2);
  163. $this->assertEquals(2, $migrator->getCurrentVersion());
  164. $e = null;
  165. try {
  166. $this->_conn->selectValues("SELECT * FROM reminders");
  167. } catch (Exception $e) {}
  168. $this->assertInstanceOf('Horde_Db_Exception', $e);
  169. $columns = $this->_columnNames('users');
  170. $this->assertTrue(in_array('last_name', $columns));
  171. }
  172. protected function _columnNames($tableName)
  173. {
  174. $columns = array();
  175. foreach ($this->_conn->columns($tableName) as $c) {
  176. $columns[] = $c->getName();
  177. }
  178. return $columns;
  179. }
  180. }