CustomDriverTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
  3. use Mapping\Fixture\Unmapped\Timestampable;
  4. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  5. /**
  6. * These are mapping tests for tree extension
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class CustomDriverTest 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(new CustomDriver);
  20. $conn = array(
  21. 'driver' => 'pdo_sqlite',
  22. 'memory' => true,
  23. );
  24. $evm = new \Doctrine\Common\EventManager();
  25. $this->timestampable = new \Gedmo\Timestampable\TimestampableListener();
  26. $this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
  27. $evm->addEventSubscriber($this->timestampable);
  28. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  29. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  30. $schemaTool->dropSchema(array());
  31. $schemaTool->createSchema(array(
  32. $this->em->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable'),
  33. ));
  34. }
  35. /**
  36. * @test
  37. */
  38. public function shouldWork()
  39. {
  40. // driver falls back to annotation driver
  41. $conf = $this->timestampable->getConfiguration(
  42. $this->em,
  43. 'Mapping\Fixture\Unmapped\Timestampable'
  44. );
  45. $this->assertTrue(isset($conf['create']));
  46. $test = new Timestampable;
  47. $this->em->persist($test);
  48. $this->em->flush();
  49. $id = $this->em
  50. ->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable')
  51. ->getReflectionProperty('id')
  52. ->getValue($test)
  53. ;
  54. $this->assertFalse(empty($id));
  55. }
  56. }
  57. class CustomDriver implements MappingDriver
  58. {
  59. public function getAllClassNames()
  60. {
  61. return array('Mapping\Fixture\Unmapped\Timestampable');
  62. }
  63. public function loadMetadataForClass($className, ClassMetadata $metadata)
  64. {
  65. if ($className === 'Mapping\Fixture\Unmapped\Timestampable') {
  66. $id = array();
  67. $id['fieldName'] = 'id';
  68. $id['type'] = 'integer';
  69. $id['nullable'] = false;
  70. $id['columnName'] = 'id';
  71. $id['id'] = true;
  72. $metadata->setIdGeneratorType(
  73. constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_AUTO')
  74. );
  75. $metadata->mapField($id);
  76. $created = array();
  77. $created['fieldName'] = 'created';
  78. $created['type'] = 'datetime';
  79. $created['nullable'] = false;
  80. $created['columnName'] = 'created';
  81. $metadata->mapField($created);
  82. }
  83. }
  84. public function isTransient($className)
  85. {
  86. return !in_array($className, $this->getAllClassNames());
  87. }
  88. }