OrmTestCase.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Common\Annotations\AnnotationReader
  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. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
  24. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  25. $reader->setIgnoreNotImportedAnnotations(true);
  26. $reader->setEnableParsePhpImports(false);
  27. if ($alias) {
  28. $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
  29. } else {
  30. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  31. }
  32. $reader = new \Doctrine\Common\Annotations\CachedReader(
  33. new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
  34. );
  35. } else {
  36. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  37. if ($alias) {
  38. $reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
  39. } else {
  40. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  41. }
  42. }
  43. return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array)$paths);
  44. }
  45. /**
  46. * Creates an EntityManager for testing purposes.
  47. *
  48. * NOTE: The created EntityManager will have its dependant DBAL parts completely
  49. * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
  50. * be configured in the tests to simulate the DBAL behavior that is desired
  51. * for a particular test,
  52. *
  53. * @return Doctrine\ORM\EntityManager
  54. */
  55. protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
  56. {
  57. $config = new \Doctrine\ORM\Configuration();
  58. if($withSharedMetadata) {
  59. $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
  60. } else {
  61. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  62. }
  63. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  64. $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
  65. $config->setProxyDir(__DIR__ . '/Proxies');
  66. $config->setProxyNamespace('Doctrine\Tests\Proxies');
  67. $eventManager = new \Doctrine\Common\EventManager();
  68. if ($conn === null) {
  69. $conn = array(
  70. 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
  71. 'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
  72. 'user' => 'john',
  73. 'password' => 'wayne'
  74. );
  75. }
  76. if (is_array($conn)) {
  77. $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
  78. }
  79. return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
  80. }
  81. private static function getSharedMetadataCacheImpl()
  82. {
  83. if (self::$_metadataCacheImpl === null) {
  84. self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
  85. }
  86. return self::$_metadataCacheImpl;
  87. }
  88. private static function getSharedQueryCacheImpl()
  89. {
  90. if (self::$_queryCacheImpl === null) {
  91. self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
  92. }
  93. return self::$_queryCacheImpl;
  94. }
  95. }