/
ServiceRepositoryTest.php
105 lines (93 loc) · 5.03 KB
/
ServiceRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Doctrine\Bundle\DoctrineBundle\Tests;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomClassRepoEntity;
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomServiceRepoEntity;
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
class ServiceRepositoryTest extends TestCase
{
/**
* https://github.com/doctrine/orm/pull/7953 needed, otherwise ORM classes we define services for trigger deprecations
*
* @group legacy
*/
public function testRepositoryServiceWiring() : void
{
$container = new ContainerBuilder(new ParameterBag([
'kernel.name' => 'app',
'kernel.debug' => false,
'kernel.bundles' => ['RepositoryServiceBundle' => RepositoryServiceBundle::class],
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.environment' => 'test',
'kernel.root_dir' => __DIR__ . '/../../../../', // src dir
'kernel.project_dir' => __DIR__ . '/../../../../', // src dir
'kernel.bundles_metadata' => [],
'kernel.charset' => 'UTF-8',
'kernel.container_class' => ContainerBuilder::class,
'kernel.secret' => 'test',
'container.build_id' => uniqid(),
'env(base64:default::SYMFONY_DECRYPTION_SECRET)' => 'foo',
]));
$container->set('annotation_reader', new AnnotationReader());
$extension = new FrameworkExtension();
$container->registerExtension($extension);
$extension->load(['framework' => []], $container);
$extension = new DoctrineExtension();
$container->registerExtension($extension);
$extension->load([
[
'dbal' => [
'driver' => 'pdo_sqlite',
'charset' => 'UTF8',
],
'orm' => [
'mappings' => [
'RepositoryServiceBundle' => [
'type' => 'annotation',
'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity',
'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity',
],
],
],
],
], $container);
$def = $container->register(TestCustomServiceRepoRepository::class, TestCustomServiceRepoRepository::class)
->setPublic(false);
// create a public alias so we can use it below for testing
$container->setAlias('test_alias__' . TestCustomServiceRepoRepository::class, new Alias(TestCustomServiceRepoRepository::class, true));
$def->setAutowired(true);
$def->setAutoconfigured(true);
$container->addCompilerPass(new ServiceRepositoryCompilerPass());
$container->compile();
$em = $container->get('doctrine.orm.default_entity_manager');
// traditional custom class repository
$customClassRepo = $em->getRepository(TestCustomClassRepoEntity::class);
$this->assertInstanceOf(TestCustomClassRepoRepository::class, $customClassRepo);
// a smoke test, trying some methods
$this->assertSame(TestCustomClassRepoEntity::class, $customClassRepo->getClassName());
$this->assertInstanceOf(QueryBuilder::class, $customClassRepo->createQueryBuilder('tc'));
// generic EntityRepository
$genericRepository = $em->getRepository(TestDefaultRepoEntity::class);
$this->assertInstanceOf(EntityRepository::class, $genericRepository);
$this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class));
// a smoke test, trying one of the methods
$this->assertSame(TestDefaultRepoEntity::class, $genericRepository->getClassName());
// custom service repository
$customServiceRepo = $em->getRepository(TestCustomServiceRepoEntity::class);
$this->assertSame($customServiceRepo, $container->get('test_alias__' . TestCustomServiceRepoRepository::class));
// a smoke test, trying some methods
$this->assertSame(TestCustomServiceRepoEntity::class, $customServiceRepo->getClassName());
$this->assertInstanceOf(QueryBuilder::class, $customServiceRepo->createQueryBuilder('tc'));
}
}