DoctrineTestHelper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Test;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use Doctrine\ORM\Configuration;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  15. use Doctrine\ORM\Mapping\Driver\XmlDriver;
  16. use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
  17. use Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator;
  18. use PHPUnit\Framework\TestCase;
  19. /**
  20. * Provides utility functions needed in tests.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. class DoctrineTestHelper
  25. {
  26. /**
  27. * Returns an entity manager for testing.
  28. *
  29. * @return EntityManager
  30. */
  31. public static function createTestEntityManager(Configuration $config = null)
  32. {
  33. if (!\extension_loaded('pdo_sqlite')) {
  34. TestCase::markTestSkipped('Extension pdo_sqlite is required.');
  35. }
  36. if (null === $config) {
  37. $config = self::createTestConfiguration();
  38. }
  39. $params = [
  40. 'driver' => 'pdo_sqlite',
  41. 'memory' => true,
  42. ];
  43. return EntityManager::create($params, $config);
  44. }
  45. /**
  46. * @return Configuration
  47. */
  48. public static function createTestConfiguration()
  49. {
  50. $config = new Configuration();
  51. $config->setEntityNamespaces(['SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures']);
  52. $config->setAutoGenerateProxyClasses(true);
  53. $config->setProxyDir(sys_get_temp_dir());
  54. $config->setProxyNamespace('SymfonyTests\Doctrine');
  55. $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
  56. return $config;
  57. }
  58. /**
  59. * @return Configuration
  60. */
  61. public static function createTestConfigurationWithXmlLoader()
  62. {
  63. $config = static::createTestConfiguration();
  64. $driverChain = new MappingDriverChain();
  65. $driverChain->addDriver(
  66. new XmlDriver(
  67. new SymfonyFileLocator(
  68. [__DIR__.'/../Tests/Resources/orm' => 'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'], '.orm.xml'
  69. )
  70. ),
  71. 'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'
  72. );
  73. $config->setMetadataDriverImpl($driverChain);
  74. return $config;
  75. }
  76. /**
  77. * This class cannot be instantiated.
  78. */
  79. private function __construct()
  80. {
  81. }
  82. }