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