SchemaDiffTest.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\DBAL\Schema\Schema,
  5. Doctrine\DBAL\Schema\Table,
  6. Doctrine\DBAL\Schema\Column,
  7. Doctrine\DBAL\Schema\Index,
  8. Doctrine\DBAL\Schema\Sequence,
  9. Doctrine\DBAL\Schema\SchemaDiff,
  10. Doctrine\DBAL\Schema\TableDiff,
  11. Doctrine\DBAL\Schema\Comparator,
  12. Doctrine\DBAL\Types\Type;
  13. class SchemaDiffTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testSchemaDiffToSql()
  16. {
  17. $diff = $this->createSchemaDiff();
  18. $platform = $this->createPlatform(true);
  19. $sql = $diff->toSql($platform);
  20. $expected = array('drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table');
  21. $this->assertEquals($expected, $sql);
  22. }
  23. public function testSchemaDiffToSaveSql()
  24. {
  25. $diff = $this->createSchemaDiff();
  26. $platform = $this->createPlatform(false);
  27. $sql = $diff->toSaveSql($platform);
  28. $expected = array('alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table');
  29. $this->assertEquals($expected, $sql);
  30. }
  31. public function createPlatform($unsafe = false)
  32. {
  33. $platform = $this->getMock('Doctrine\Tests\DBAL\Mocks\MockPlatform');
  34. if ($unsafe) {
  35. $platform->expects($this->exactly(1))
  36. ->method('getDropSequenceSql')
  37. ->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
  38. ->will($this->returnValue('drop_seq'));
  39. }
  40. $platform->expects($this->exactly(1))
  41. ->method('getAlterSequenceSql')
  42. ->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
  43. ->will($this->returnValue('alter_seq'));
  44. $platform->expects($this->exactly(1))
  45. ->method('getCreateSequenceSql')
  46. ->with($this->isInstanceOf('Doctrine\DBAL\Schema\Sequence'))
  47. ->will($this->returnValue('create_seq'));
  48. if ($unsafe) {
  49. $platform->expects($this->exactly(1))
  50. ->method('getDropTableSql')
  51. ->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
  52. ->will($this->returnValue('drop_table'));
  53. }
  54. $platform->expects($this->exactly(1))
  55. ->method('getCreateTableSql')
  56. ->with($this->isInstanceof('Doctrine\DBAL\Schema\Table'))
  57. ->will($this->returnValue(array('create_table')));
  58. $platform->expects($this->exactly(1))
  59. ->method('getCreateForeignKeySQL')
  60. ->with($this->isInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint'))
  61. ->will($this->returnValue('create_foreign_key'));
  62. $platform->expects($this->exactly(1))
  63. ->method('getAlterTableSql')
  64. ->with($this->isInstanceOf('Doctrine\DBAL\Schema\TableDiff'))
  65. ->will($this->returnValue(array('alter_table')));
  66. if ($unsafe) {
  67. $platform->expects($this->exactly(1))
  68. ->method('getDropForeignKeySql')
  69. ->with($this->isInstanceof('Doctrine\DBAL\Schema\ForeignKeyConstraint'), $this->equalTo('local_table'))
  70. ->will($this->returnValue('drop_orphan_fk'));
  71. }
  72. $platform->expects($this->exactly(1))
  73. ->method('supportsSequences')
  74. ->will($this->returnValue(true));
  75. $platform->expects($this->exactly(2))
  76. ->method('supportsForeignKeyConstraints')
  77. ->will($this->returnValue(true));
  78. return $platform;
  79. }
  80. public function createSchemaDiff()
  81. {
  82. $diff = new SchemaDiff();
  83. $diff->changedSequences['foo_seq'] = new Sequence('foo_seq');
  84. $diff->newSequences['bar_seq'] = new Sequence('bar_seq');
  85. $diff->removedSequences['baz_seq'] = new Sequence('baz_seq');
  86. $diff->newTables['foo_table'] = new Table('foo_table');
  87. $diff->removedTables['bar_table'] = new Table('bar_table');
  88. $diff->changedTables['baz_table'] = new TableDiff('baz_table');
  89. $diff->newTables['foo_table']->addColumn('foreign_id', 'integer');
  90. $diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', array('foreign_id'), array('id'));
  91. $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('id'), 'foreign_table', array('id'));
  92. $fk->setLocalTable(new Table('local_table'));
  93. $diff->orphanedForeignKeys[] = $fk;
  94. return $diff;
  95. }
  96. }