OrmTestCase.php 4.6 KB

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