/
ContainerRepositoryFactoryTest.php
200 lines (161 loc) · 7.5 KB
/
ContainerRepositoryFactoryTest.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Doctrine\Bundle\DoctrineBundle\Tests\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
class ContainerRepositoryFactoryTest extends TestCase
{
public function testGetRepositoryReturnsService()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
$repo = new StubRepository($em, new ClassMetadata(''));
$container = $this->createContainer(['my_repo' => $repo]);
$factory = new ContainerRepositoryFactory($container);
$this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity'));
}
public function testGetRepositoryReturnsEntityRepository()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$container = $this->createContainer([]);
$em = $this->createEntityManager(['Foo\BoringEntity' => null]);
$factory = new ContainerRepositoryFactory($container);
$actualRepo = $factory->getRepository($em, 'Foo\BoringEntity');
$this->assertInstanceOf(EntityRepository::class, $actualRepo);
// test the same instance is returned
$this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity'));
}
public function testCustomRepositoryIsReturned()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$container = $this->createContainer([]);
$em = $this->createEntityManager([
'Foo\CustomNormalRepoEntity' => StubRepository::class,
]);
$factory = new ContainerRepositoryFactory($container);
$actualRepo = $factory->getRepository($em, 'Foo\CustomNormalRepoEntity');
$this->assertInstanceOf(StubRepository::class, $actualRepo);
// test the same instance is returned
$this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\CustomNormalRepoEntity'));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).
*/
public function testServiceRepositoriesMustExtendObjectRepository()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$repo = new \stdClass();
$container = $this->createContainer(['my_repo' => $repo]);
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
$factory = new ContainerRepositoryFactory($container);
$factory->getRepository($em, 'Foo\CoolEntity');
}
public function testServiceRepositoriesCanNotExtendsEntityRepository()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$repo = new StubCustomRepository();
$container = $this->createContainer(['my_repo' => $repo]);
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
$factory = new ContainerRepositoryFactory($container);
$factory->getRepository($em, 'Foo\CoolEntity');
$actualRepo = $factory->getRepository($em, 'Foo\CoolEntity');
$this->assertInstanceOf(StubCustomRepository::class, $actualRepo);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The "Doctrine\Bundle\DoctrineBundle\Tests\Repository\StubServiceRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service".
*/
public function testRepositoryMatchesServiceInterfaceButServiceNotFound()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}
$container = $this->createContainer([]);
$em = $this->createEntityManager([
'Foo\CoolEntity' => StubServiceRepository::class,
]);
$factory = new ContainerRepositoryFactory($container);
$factory->getRepository($em, 'Foo\CoolEntity');
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The "Foo\CoolEntity" entity has a repositoryClass set to "not_a_real_class", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service".
*/
public function testCustomRepositoryIsNotAValidClass()
{
if (interface_exists(ContainerInterface::class)) {
$container = $this->createContainer([]);
} else {
// Symfony 3.2 and lower support
$container = null;
}
$em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']);
$factory = new ContainerRepositoryFactory($container);
$factory->getRepository($em, 'Foo\CoolEntity');
}
private function createContainer(array $services)
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->any())
->method('has')
->willReturnCallback(function ($id) use ($services) {
return isset($services[$id]);
});
$container->expects($this->any())
->method('get')
->willReturnCallback(function ($id) use ($services) {
return $services[$id];
});
return $container;
}
private function createEntityManager(array $entityRepositoryClasses)
{
$classMetadatas = [];
foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
$metadata = new ClassMetadata($entityClass);
$metadata->customRepositoryClassName = $entityRepositoryClass;
$classMetadatas[$entityClass] = $metadata;
}
$em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
$em->expects($this->any())
->method('getClassMetadata')
->willReturnCallback(function ($class) use ($classMetadatas) {
return $classMetadatas[$class];
});
$em->expects($this->any())
->method('getConfiguration')
->willReturn(new Configuration());
return $em;
}
}
class StubRepository extends EntityRepository
{
}
class StubServiceRepository extends EntityRepository implements ServiceEntityRepositoryInterface
{
}
class StubCustomRepository implements ObjectRepository
{
public function find($id) {}
public function findAll() {}
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) {}
public function findOneBy(array $criteria) {}
public function getClassName() {}
}