Skip to content

[DoctrineBridge] Remove DoctrineTestHelper and TestRepositoryFactory #42925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.0
---

* Remove `DoctrineTestHelper` and `TestRepositoryFactory`

5.3
---

Expand Down
102 changes: 0 additions & 102 deletions src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php

This file was deleted.

73 changes: 0 additions & 73 deletions src/Symfony/Bridge/Doctrine/Test/TestRepositoryFactory.php

This file was deleted.

65 changes: 63 additions & 2 deletions src/Symfony/Bridge/Doctrine/Tests/DoctrineTestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,74 @@

namespace Symfony\Bridge\Doctrine\Tests;

use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper as TestDoctrineTestHelper;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator;
use PHPUnit\Framework\TestCase;

/**
* Provides utility functions needed in tests.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class DoctrineTestHelper extends TestDoctrineTestHelper
final class DoctrineTestHelper
{
/**
* Returns an entity manager for testing.
*/
public static function createTestEntityManager(Configuration $config = null): EntityManager
{
if (!\extension_loaded('pdo_sqlite')) {
TestCase::markTestSkipped('Extension pdo_sqlite is required.');
}

$params = [
'driver' => 'pdo_sqlite',
'memory' => true,
];

return EntityManager::create($params, $config ?? self::createTestConfiguration());
}

public static function createTestConfiguration(): Configuration
{
$config = new Configuration();
$config->setEntityNamespaces(['SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures']);
$config->setAutoGenerateProxyClasses(true);
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));

return $config;
}

public static function createTestConfigurationWithXmlLoader(): Configuration
{
$config = self::createTestConfiguration();

$driverChain = new MappingDriverChain();
$driverChain->addDriver(
new XmlDriver(
new SymfonyFileLocator(
[__DIR__.'/../Tests/Resources/orm' => 'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'], '.orm.xml'
)
),
'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'
);

$config->setMetadataDriverImpl($driverChain);

return $config;
}

/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}
40 changes: 38 additions & 2 deletions src/Symfony/Bridge/Doctrine/Tests/TestRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,47 @@

namespace Symfony\Bridge\Doctrine\Tests;

use Symfony\Bridge\Doctrine\Test\TestRepositoryFactory as TestTestRepositoryFactory;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\Persistence\ObjectRepository;

/**
* @author Andreas Braun <alcaeus@alcaeus.org>
*/
final class TestRepositoryFactory extends TestTestRepositoryFactory
final class TestRepositoryFactory implements RepositoryFactory
{
/**
* @var array<string, ObjectRepository>
*/
private array $repositoryList = [];

/**
* {@inheritdoc}
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
{
$repositoryHash = $this->getRepositoryHash($entityManager, $entityName);

return $this->repositoryList[$repositoryHash] ??= $this->createRepository($entityManager, $entityName);
}

public function setRepository(EntityManagerInterface $entityManager, string $entityName, ObjectRepository $repository): void
{
$repositoryHash = $this->getRepositoryHash($entityManager, $entityName);

$this->repositoryList[$repositoryHash] = $repository;
}

private function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository
{
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();

return new $repositoryClassName($entityManager, $metadata);
}

private function getRepositoryHash(EntityManagerInterface $entityManager, string $entityName): string
{
return $entityManager->getClassMetadata($entityName)->getName().spl_object_hash($entityManager);
}
}