SchemaTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\Sequence;
  7. class SchemaTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testAddTable()
  10. {
  11. $tableName = "foo";
  12. $table = new Table($tableName);
  13. $schema = new Schema(array($table));
  14. $this->assertTrue($schema->hasTable($tableName));
  15. $tables = $schema->getTables();
  16. $this->assertTrue( isset($tables[$tableName]) );
  17. $this->assertSame($table, $tables[$tableName]);
  18. $this->assertSame($table, $schema->getTable($tableName));
  19. $this->assertTrue($schema->hasTable($tableName));
  20. }
  21. public function testTableMatchingCaseInsenstive()
  22. {
  23. $table = new Table("Foo");
  24. $schema = new Schema(array($table));
  25. $this->assertTrue($schema->hasTable("foo"));
  26. $this->assertTrue($schema->hasTable("FOO"));
  27. $this->assertSame($table, $schema->getTable('FOO'));
  28. $this->assertSame($table, $schema->getTable('foo'));
  29. $this->assertSame($table, $schema->getTable('Foo'));
  30. }
  31. public function testGetUnknownTableThrowsException()
  32. {
  33. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  34. $schema = new Schema();
  35. $schema->getTable("unknown");
  36. }
  37. public function testCreateTableTwiceThrowsException()
  38. {
  39. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  40. $tableName = "foo";
  41. $table = new Table($tableName);
  42. $tables = array($table, $table);
  43. $schema = new Schema($tables);
  44. }
  45. public function testRenameTable()
  46. {
  47. $tableName = "foo";
  48. $table = new Table($tableName);
  49. $schema = new Schema(array($table));
  50. $this->assertTrue($schema->hasTable("foo"));
  51. $schema->renameTable("foo", "bar");
  52. $this->assertFalse($schema->hasTable("foo"));
  53. $this->assertTrue($schema->hasTable("bar"));
  54. $this->assertSame($table, $schema->getTable("bar"));
  55. }
  56. public function testDropTable()
  57. {
  58. $tableName = "foo";
  59. $table = new Table($tableName);
  60. $schema = new Schema(array($table));
  61. $this->assertTrue($schema->hasTable("foo"));
  62. $schema->dropTable("foo");
  63. $this->assertFalse($schema->hasTable("foo"));
  64. }
  65. public function testCreateTable()
  66. {
  67. $schema = new Schema();
  68. $this->assertFalse($schema->hasTable("foo"));
  69. $table = $schema->createTable("foo");
  70. $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
  71. $this->assertEquals("foo", $table->getName());
  72. $this->assertTrue($schema->hasTable("foo"));
  73. }
  74. public function testAddSequences()
  75. {
  76. $sequence = new Sequence("a_seq", 1, 1);
  77. $schema = new Schema(array(), array($sequence));
  78. $this->assertTrue($schema->hasSequence("a_seq"));
  79. $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
  80. $sequences = $schema->getSequences();
  81. $this->assertArrayHasKey('a_seq', $sequences);
  82. }
  83. public function testSequenceAccessCaseInsensitive()
  84. {
  85. $sequence = new Sequence("a_Seq");
  86. $schema = new Schema(array(), array($sequence));
  87. $this->assertTrue($schema->hasSequence('a_seq'));
  88. $this->assertTrue($schema->hasSequence('a_Seq'));
  89. $this->assertTrue($schema->hasSequence('A_SEQ'));
  90. $this->assertEquals($sequence, $schema->getSequence('a_seq'));
  91. $this->assertEquals($sequence, $schema->getSequence('a_Seq'));
  92. $this->assertEquals($sequence, $schema->getSequence('A_SEQ'));
  93. }
  94. public function testGetUnknownSequenceThrowsException()
  95. {
  96. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  97. $schema = new Schema();
  98. $schema->getSequence("unknown");
  99. }
  100. public function testCreateSequence()
  101. {
  102. $schema = new Schema();
  103. $sequence = $schema->createSequence('a_seq', 10, 20);
  104. $this->assertEquals('a_seq', $sequence->getName());
  105. $this->assertEquals(10, $sequence->getAllocationSize());
  106. $this->assertEquals(20, $sequence->getInitialValue());
  107. $this->assertTrue($schema->hasSequence("a_seq"));
  108. $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
  109. $sequences = $schema->getSequences();
  110. $this->assertArrayHasKey('a_seq', $sequences);
  111. }
  112. public function testDropSequence()
  113. {
  114. $sequence = new Sequence("a_seq", 1, 1);
  115. $schema = new Schema(array(), array($sequence));
  116. $schema->dropSequence("a_seq");
  117. $this->assertFalse($schema->hasSequence("a_seq"));
  118. }
  119. public function testAddSequenceTwiceThrowsException()
  120. {
  121. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  122. $sequence = new Sequence("a_seq", 1, 1);
  123. $schema = new Schema(array(), array($sequence, $sequence));
  124. }
  125. public function testConfigMaxIdentifierLength()
  126. {
  127. $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
  128. $schemaConfig->setMaxIdentifierLength(5);
  129. $schema = new Schema(array(), array(), $schemaConfig);
  130. $table = $schema->createTable("smalltable");
  131. $table->addColumn('long_id', 'integer');
  132. $table->addIndex(array('long_id'));
  133. $index = current($table->getIndexes());
  134. $this->assertEquals(5, strlen($index->getName()));
  135. }
  136. public function testDeepClone()
  137. {
  138. $schema = new Schema();
  139. $sequence = $schema->createSequence('baz');
  140. $tableA = $schema->createTable('foo');
  141. $tableA->addColumn('id', 'integer');
  142. $tableB = $schema->createTable('bar');
  143. $tableB->addColumn('id', 'integer');
  144. $tableB->addColumn('foo_id', 'integer');
  145. $tableB->addForeignKeyConstraint($tableA, array('foo_id'), array('id'));
  146. $schemaNew = clone $schema;
  147. $this->assertNotSame($sequence, $schemaNew->getSequence('baz'));
  148. $this->assertNotSame($tableA, $schemaNew->getTable('foo'));
  149. $this->assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
  150. $this->assertNotSame($tableB, $schemaNew->getTable('bar'));
  151. $this->assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
  152. $fk = $schemaNew->getTable('bar')->getForeignKeys();
  153. $fk = current($fk);
  154. $this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
  155. }
  156. }