ForcedMetadataTest.php 3.3 KB

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