DatabaseDriverTest.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  5. Doctrine\Common\Util\Inflector;
  6. class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
  7. {
  8. /**
  9. * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
  10. */
  11. protected $_sm = null;
  12. public function setUp()
  13. {
  14. $this->useModelSet('cms');
  15. parent::setUp();
  16. $this->_sm = $this->_em->getConnection()->getSchemaManager();
  17. }
  18. public function testLoadMetadataFromDatabase()
  19. {
  20. if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
  21. $this->markTestSkipped('Platform does not support foreign keys.');
  22. }
  23. $table = new \Doctrine\DBAL\Schema\Table("dbdriver_foo");
  24. $table->addColumn('id', 'integer');
  25. $table->setPrimaryKey(array('id'));
  26. $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 200));
  27. $this->_sm->dropAndCreateTable($table);
  28. $metadatas = $this->extractClassMetadata(array("DbdriverFoo"));
  29. $this->assertArrayHasKey('DbdriverFoo', $metadatas);
  30. $metadata = $metadatas['DbdriverFoo'];
  31. $this->assertArrayHasKey('id', $metadata->fieldMappings);
  32. $this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']);
  33. $this->assertEquals('id', strtolower($metadata->fieldMappings['id']['columnName']));
  34. $this->assertEquals('integer', (string)$metadata->fieldMappings['id']['type']);
  35. $this->assertArrayHasKey('bar', $metadata->fieldMappings);
  36. $this->assertEquals('bar', $metadata->fieldMappings['bar']['fieldName']);
  37. $this->assertEquals('bar', strtolower($metadata->fieldMappings['bar']['columnName']));
  38. $this->assertEquals('string', (string)$metadata->fieldMappings['bar']['type']);
  39. $this->assertEquals(200, $metadata->fieldMappings['bar']['length']);
  40. $this->assertTrue($metadata->fieldMappings['bar']['nullable']);
  41. }
  42. public function testLoadMetadataWithForeignKeyFromDatabase()
  43. {
  44. if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
  45. $this->markTestSkipped('Platform does not support foreign keys.');
  46. }
  47. $tableB = new \Doctrine\DBAL\Schema\Table("dbdriver_bar");
  48. $tableB->addColumn('id', 'integer');
  49. $tableB->setPrimaryKey(array('id'));
  50. $this->_sm->dropAndCreateTable($tableB);
  51. $tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz");
  52. $tableA->addColumn('id', 'integer');
  53. $tableA->setPrimaryKey(array('id'));
  54. $tableA->addColumn('bar_id', 'integer');
  55. $tableA->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id'));
  56. $this->_sm->dropAndCreateTable($tableA);
  57. $metadatas = $this->extractClassMetadata(array("DbdriverBar", "DbdriverBaz"));
  58. $this->assertArrayHasKey('DbdriverBaz', $metadatas);
  59. $bazMetadata = $metadatas['DbdriverBaz'];
  60. $this->assertArrayNotHasKey('barId', $bazMetadata->fieldMappings, "The foreign Key field should not be inflected as 'barId' field, its an association.");
  61. $this->assertArrayHasKey('id', $bazMetadata->fieldMappings);
  62. $bazMetadata->associationMappings = \array_change_key_case($bazMetadata->associationMappings, \CASE_LOWER);
  63. $this->assertArrayHasKey('bar', $bazMetadata->associationMappings);
  64. $this->assertEquals(ClassMetadataInfo::MANY_TO_ONE, $bazMetadata->associationMappings['bar']['type']);
  65. }
  66. public function testDetectManyToManyTables()
  67. {
  68. if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
  69. $this->markTestSkipped('Platform does not support foreign keys.');
  70. }
  71. $metadatas = $this->extractClassMetadata(array("CmsUsers", "CmsGroups"));
  72. $this->assertArrayHasKey('CmsUsers', $metadatas, 'CmsUsers entity was not detected.');
  73. $this->assertArrayHasKey('CmsGroups', $metadatas, 'CmsGroups entity was not detected.');
  74. $this->assertEquals(1, count($metadatas['CmsUsers']->associationMappings));
  75. $this->assertArrayHasKey('group', $metadatas['CmsUsers']->associationMappings);
  76. $this->assertEquals(1, count($metadatas['CmsGroups']->associationMappings));
  77. $this->assertArrayHasKey('user', $metadatas['CmsGroups']->associationMappings);
  78. }
  79. public function testIgnoreManyToManyTableWithoutFurtherForeignKeyDetails()
  80. {
  81. $tableB = new \Doctrine\DBAL\Schema\Table("dbdriver_bar");
  82. $tableB->addColumn('id', 'integer');
  83. $tableB->setPrimaryKey(array('id'));
  84. $tableA = new \Doctrine\DBAL\Schema\Table("dbdriver_baz");
  85. $tableA->addColumn('id', 'integer');
  86. $tableA->setPrimaryKey(array('id'));
  87. $tableMany = new \Doctrine\DBAL\Schema\Table("dbdriver_bar_baz");
  88. $tableMany->addColumn('bar_id', 'integer');
  89. $tableMany->addColumn('baz_id', 'integer');
  90. $tableMany->addForeignKeyConstraint('dbdriver_bar', array('bar_id'), array('id'));
  91. $metadatas = $this->convertToClassMetadata(array($tableA, $tableB), array($tableMany));
  92. $this->assertEquals(0, count($metadatas['DbdriverBaz']->associationMappings), "no association mappings should be detected.");
  93. }
  94. protected function convertToClassMetadata(array $entityTables, array $manyTables = array())
  95. {
  96. $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm);
  97. $driver->setTables($entityTables, $manyTables);
  98. $metadatas = array();
  99. foreach ($driver->getAllClassNames() AS $className) {
  100. $class = new ClassMetadataInfo($className);
  101. $driver->loadMetadataForClass($className, $class);
  102. $metadatas[$className] = $class;
  103. }
  104. return $metadatas;
  105. }
  106. /**
  107. * @param string $className
  108. * @return ClassMetadata
  109. */
  110. protected function extractClassMetadata(array $classNames)
  111. {
  112. $classNames = array_map('strtolower', $classNames);
  113. $metadatas = array();
  114. $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm);
  115. foreach ($driver->getAllClassNames() as $className) {
  116. if (!in_array(strtolower($className), $classNames)) {
  117. continue;
  118. }
  119. $class = new ClassMetadataInfo($className);
  120. $driver->loadMetadataForClass($className, $class);
  121. $metadatas[$className] = $class;
  122. }
  123. if (count($metadatas) != count($classNames)) {
  124. $this->fail("Have not found all classes matching the names '" . implode(", ", $classNames) . "' only tables " . implode(", ", array_keys($metadatas)));
  125. }
  126. return $metadatas;
  127. }
  128. }