OrmTestCase.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Doctrine\Tests;
  3. use Doctrine\Common\Cache\ArrayCache;
  4. use Doctrine\Common\Annotations\AnnotationRegistry;
  5. use Doctrine\Common\Annotations\SimpleAnnotationReader;
  6. /**
  7. * Base testcase class for all ORM testcases.
  8. */
  9. abstract class OrmTestCase extends DoctrineTestCase
  10. {
  11. /** The metadata cache that is shared between all ORM tests (except functional tests). */
  12. private static $_metadataCacheImpl = null;
  13. /** The query cache that is shared between all ORM tests (except functional tests). */
  14. private static $_queryCacheImpl = null;
  15. /**
  16. * @param array $paths
  17. * @return \Doctrine\Common\Annotations\AnnotationReader
  18. */
  19. protected function createAnnotationDriver($paths = array(), $alias = null)
  20. {
  21. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  22. // Register the ORM Annotations in the AnnotationRegistry
  23. AnnotationRegistry::registerFile(__DIR__ . '/../../../lib/Doctrine/ORM//Mapping/Driver/DoctrineAnnotations.php');
  24. $reader = new SimpleAnnotationReader();
  25. $reader->addNamespace('Doctrine\ORM\Mapping');
  26. $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
  27. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
  28. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  29. $reader->setIgnoreNotImportedAnnotations(true);
  30. $reader->setEnableParsePhpImports(false);
  31. if ($alias) {
  32. $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
  33. } else {
  34. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  35. }
  36. $reader = new \Doctrine\Common\Annotations\CachedReader(
  37. new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
  38. );
  39. } else {
  40. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  41. if ($alias) {
  42. $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
  43. } else {
  44. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  45. }
  46. }
  47. return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array)$paths);
  48. }
  49. /**
  50. * Creates an EntityManager for testing purposes.
  51. *
  52. * NOTE: The created EntityManager will have its dependant DBAL parts completely
  53. * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
  54. * be configured in the tests to simulate the DBAL behavior that is desired
  55. * for a particular test,
  56. *
  57. * @return Doctrine\ORM\EntityManager
  58. */
  59. protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
  60. {
  61. $config = new \Doctrine\ORM\Configuration();
  62. if($withSharedMetadata) {
  63. $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
  64. } else {
  65. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  66. }
  67. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  68. $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
  69. $config->setProxyDir(__DIR__ . '/Proxies');
  70. $config->setProxyNamespace('Doctrine\Tests\Proxies');
  71. $eventManager = new \Doctrine\Common\EventManager();
  72. if ($conn === null) {
  73. $conn = array(
  74. 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
  75. 'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
  76. 'user' => 'john',
  77. 'password' => 'wayne'
  78. );
  79. }
  80. if (is_array($conn)) {
  81. $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
  82. }
  83. return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
  84. }
  85. private static function getSharedMetadataCacheImpl()
  86. {
  87. if (self::$_metadataCacheImpl === null) {
  88. self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
  89. }
  90. return self::$_metadataCacheImpl;
  91. }
  92. private static function getSharedQueryCacheImpl()
  93. {
  94. if (self::$_queryCacheImpl === null) {
  95. self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
  96. }
  97. return self::$_queryCacheImpl;
  98. }
  99. }