CustomDriverTest.php 3.1 KB

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