SchemaToolTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Doctrine\Tests\ORM\Tools;
  3. use Doctrine\ORM\Tools\SchemaTool;
  4. use Doctrine\ORM\Tools\ToolEvents;
  5. use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
  6. use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
  9. {
  10. public function testAddUniqueIndexForUniqueFieldAnnocation()
  11. {
  12. $em = $this->_getTestEntityManager();
  13. $schemaTool = new SchemaTool($em);
  14. $classes = array(
  15. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
  16. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'),
  17. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'),
  18. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'),
  19. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'),
  20. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
  21. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
  22. );
  23. $schema = $schemaTool->getSchemaFromMetadata($classes);
  24. $this->assertTrue($schema->hasTable('cms_users'), "Table cms_users should exist.");
  25. $this->assertTrue($schema->getTable('cms_users')->columnsAreIndexed(array('username')), "username column should be indexed.");
  26. }
  27. /**
  28. * @group DDC-200
  29. */
  30. public function testPassColumnDefinitionToJoinColumn()
  31. {
  32. $customColumnDef = "MEDIUMINT(6) UNSIGNED NOT NULL";
  33. $em = $this->_getTestEntityManager();
  34. $schemaTool = new SchemaTool($em);
  35. $avatar = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumAvatar');
  36. $avatar->fieldMappings['id']['columnDefinition'] = $customColumnDef;
  37. $user = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumUser');
  38. $classes = array($avatar, $user);
  39. $schema = $schemaTool->getSchemaFromMetadata($classes);
  40. $this->assertTrue($schema->hasTable('forum_users'));
  41. $table = $schema->getTable("forum_users");
  42. $this->assertTrue($table->hasColumn('avatar_id'));
  43. $this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
  44. }
  45. /**
  46. * @group DDC-283
  47. */
  48. public function testPostGenerateEvents()
  49. {
  50. $listener = new GenerateSchemaEventListener();
  51. $em = $this->_getTestEntityManager();
  52. $em->getEventManager()->addEventListener(
  53. array(ToolEvents::postGenerateSchemaTable, ToolEvents::postGenerateSchema), $listener
  54. );
  55. $schemaTool = new SchemaTool($em);
  56. $classes = array(
  57. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
  58. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'),
  59. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'),
  60. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'),
  61. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'),
  62. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
  63. $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
  64. );
  65. $schema = $schemaTool->getSchemaFromMetadata($classes);
  66. $this->assertEquals(count($classes), $listener->tableCalls);
  67. $this->assertTrue($listener->schemaCalled);
  68. }
  69. }
  70. class GenerateSchemaEventListener
  71. {
  72. public $tableCalls = 0;
  73. public $schemaCalled = false;
  74. public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
  75. {
  76. $this->tableCalls++;
  77. }
  78. public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs)
  79. {
  80. $this->schemaCalled = true;
  81. }
  82. }