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