ConfigurationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Doctrine\DBAL\Migrations\Tests;
  3. use Doctrine\DBAL\Migrations\Configuration\Configuration;
  4. use Doctrine\DBAL\Schema\Schema;
  5. class ConfigurationTest extends MigrationTestCase
  6. {
  7. public function testGetConnection()
  8. {
  9. $conn = $this->getSqliteConnection();
  10. $config = new Configuration($conn);
  11. $this->assertSame($conn, $config->getConnection());
  12. }
  13. public function testValidateMigrationsNamespaceRequired()
  14. {
  15. $config = new Configuration($this->getSqliteConnection());
  16. $this->setExpectedException("Doctrine\DBAL\Migrations\MigrationException", "Migrations namespace must be configured in order to use Doctrine migrations.");
  17. $config->validate();
  18. }
  19. public function testValidateMigrationsDirectoryRequired()
  20. {
  21. $config = new Configuration($this->getSqliteConnection());
  22. $config->setMigrationsNamespace("DoctrineMigrations\\");
  23. $this->setExpectedException("Doctrine\DBAL\Migrations\MigrationException", "Migrations directory must be configured in order to use Doctrine migrations.");
  24. $config->validate();
  25. }
  26. public function testValidateMigrations()
  27. {
  28. $config = new Configuration($this->getSqliteConnection());
  29. $config->setMigrationsNamespace("DoctrineMigrations\\");
  30. $config->setMigrationsDirectory(sys_get_temp_dir());
  31. $config->validate();
  32. }
  33. public function testSetGetName()
  34. {
  35. $config = new Configuration($this->getSqliteConnection());
  36. $config->setName('Test');
  37. $this->assertEquals('Test', $config->getName());
  38. }
  39. public function testMigrationsTable()
  40. {
  41. $config = new Configuration($this->getSqliteConnection());
  42. $this->assertEquals("doctrine_migration_versions", $config->getMigrationsTableName());
  43. }
  44. public function testEmptyProjectDefaults()
  45. {
  46. $config = $this->getSqliteConfiguration();
  47. $this->assertEquals(0, $config->getCurrentVersion(), "current version 0");
  48. $this->assertEquals(0, $config->getLatestVersion(), "latest version 0");
  49. $this->assertEquals(0, $config->getNumberOfAvailableMigrations(), "number of available migrations 0");
  50. $this->assertEquals(0, $config->getNumberOfExecutedMigrations(), "number of executed migrations 0");
  51. $this->assertEquals(array(), $config->getMigrations());
  52. }
  53. public function testGetUnknownVersion()
  54. {
  55. $config = $this->getSqliteConfiguration();
  56. $this->setExpectedException('Doctrine\DBAL\Migrations\MigrationException', 'Could not find migration version 1234');
  57. $config->getVersion(1234);
  58. }
  59. public function testRegisterMigration()
  60. {
  61. $config = $this->getSqliteConfiguration();
  62. $config->registerMigration(1234, 'Doctrine\DBAL\Migrations\Tests\ConfigMigration');
  63. $this->assertEquals(1, count($config->getMigrations()), "One Migration registered.");
  64. $this->assertTrue($config->hasVersion(1234));
  65. $version = $config->getVersion(1234);
  66. $this->assertInstanceOf('Doctrine\DBAL\Migrations\Version', $version);
  67. $this->assertEquals(1234, $version->getVersion());
  68. $this->assertFalse($version->isMigrated());
  69. }
  70. public function testRegisterMigrations()
  71. {
  72. $config = $this->getSqliteConfiguration();
  73. $config->registerMigrations(array(
  74. 1234 => 'Doctrine\DBAL\Migrations\Tests\ConfigMigration',
  75. 1235 => 'Doctrine\DBAL\Migrations\Tests\Config2Migration',
  76. ));
  77. $this->assertEquals(2, count($config->getMigrations()), "Two Migration registered.");
  78. $version = $config->getVersion(1234);
  79. $this->assertInstanceOf('Doctrine\DBAL\Migrations\Version', $version);
  80. $version = $config->getVersion(1235);
  81. $this->assertInstanceOf('Doctrine\DBAL\Migrations\Version', $version);
  82. }
  83. public function testRegisterDuplicateVersion()
  84. {
  85. $config = $this->getSqliteConfiguration();
  86. $config->registerMigration(1234, 'Doctrine\DBAL\Migrations\Tests\ConfigMigration');
  87. $this->setExpectedException('Doctrine\DBAL\Migrations\MigrationException', 'Migration version 1234 already registered with class Doctrine\DBAL\Migrations\Version');
  88. $config->registerMigration(1234, 'Doctrine\DBAL\Migrations\Tests\ConfigMigration');
  89. }
  90. public function testGetAvailableVersions()
  91. {
  92. $config = $this->getSqliteConfiguration();
  93. $config->registerMigration(1234, 'Doctrine\DBAL\Migrations\Tests\ConfigMigration');
  94. $this->assertEquals(array(1234), $config->getAvailableVersions());
  95. }
  96. }
  97. class ConfigMigration extends \Doctrine\DBAL\Migrations\AbstractMigration
  98. {
  99. public function down(Schema $schema)
  100. {
  101. }
  102. public function up(Schema $schema)
  103. {
  104. }
  105. }
  106. class Config2Migration extends \Doctrine\DBAL\Migrations\AbstractMigration
  107. {
  108. public function down(Schema $schema)
  109. {
  110. }
  111. public function up(Schema $schema)
  112. {
  113. }
  114. }