ForcedMetadataTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. use Doctrine\ORM\Mapping\ClassMetadata;
  3. use Mapping\Fixture\Unmapped\Timestampable;
  4. use Doctrine\ORM\Version;
  5. /**
  6. * These are mapping tests for tree extension
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @package Gedmo.Mapping
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class ForcedMetadataTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function setUp()
  16. {
  17. $config = new \Doctrine\ORM\Configuration();
  18. $config->setProxyDir(TESTS_TEMP_DIR);
  19. $config->setProxyNamespace('Gedmo\Mapping\Proxy');
  20. $config->setMetadataDriverImpl(
  21. new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($_ENV['annotation_reader'])
  22. );
  23. $conn = array(
  24. 'driver' => 'pdo_sqlite',
  25. 'memory' => true,
  26. );
  27. $evm = new \Doctrine\Common\EventManager();
  28. $this->timestampable = new \Gedmo\Timestampable\TimestampableListener();
  29. $this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
  30. $evm->addEventSubscriber($this->timestampable);
  31. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  32. }
  33. private function prepare()
  34. {
  35. $cmf = $this->em->getMetadataFactory();
  36. $metadata = new ClassMetadata('Mapping\Fixture\Unmapped\Timestampable');
  37. $id = array();
  38. $id['fieldName'] = 'id';
  39. $id['type'] = 'integer';
  40. $id['nullable'] = false;
  41. $id['columnName'] = 'id';
  42. $id['id'] = true;
  43. $metadata->mapField($id);
  44. $created = array();
  45. $created['fieldName'] = 'created';
  46. $created['type'] = 'datetime';
  47. $created['nullable'] = false;
  48. $created['columnName'] = 'created';
  49. $metadata->mapField($created);
  50. $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
  51. $metadata->setIdGenerator(new \Doctrine\ORM\Id\IdentityGenerator(null));
  52. $metadata->setPrimaryTable(array('name' => 'temp_test'));
  53. $cmf->setMetadataFor('Mapping\Fixture\Unmapped\Timestampable', $metadata);
  54. // trigger loadClassMetadata event
  55. $evm = $this->em->getEventManager();
  56. $eventArgs = new \Doctrine\ORM\Event\LoadClassMetadataEventArgs($metadata, $this->em);
  57. $evm->dispatchEvent(\Doctrine\ORM\Events::loadClassMetadata, $eventArgs);
  58. if (Version::compare('2.3.0') >= 0) {
  59. $metadata->wakeupReflection($cmf->getReflectionService());
  60. }
  61. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  62. $schemaTool->dropSchema(array());
  63. $schemaTool->createSchema(array(
  64. $this->em->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable'),
  65. ));
  66. }
  67. /**
  68. * @test
  69. */
  70. public function shouldWork()
  71. {
  72. $this->prepare();
  73. $meta = $this->em->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable');
  74. // driver falls back to annotation driver
  75. $conf = $this->timestampable->getConfiguration(
  76. $this->em,
  77. 'Mapping\Fixture\Unmapped\Timestampable'
  78. );
  79. $this->assertTrue(isset($conf['create']));
  80. $test = new Timestampable;
  81. $this->em->persist($test);
  82. $this->em->flush();
  83. $id = $this->em
  84. ->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable')
  85. ->getReflectionProperty('id')
  86. ->getValue($test)
  87. ;
  88. $this->assertFalse(empty($id));
  89. }
  90. }