forked from doctrine/DoctrineMongoDBBundle
/
ContainerRepositoryFactoryTest.php
191 lines (154 loc) · 6.69 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
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\MongoDBBundle\Tests\Repository;
use Doctrine\Bundle\MongoDBBundle\Repository\ContainerRepositoryFactory;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface;
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\UnitOfWork;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use RuntimeException;
use stdClass;
use function sprintf;
use function sys_get_temp_dir;
class ContainerRepositoryFactoryTest extends TestCase
{
public function testGetRepositoryReturnsService()
{
$dm = $this->createDocumentManager([CoolDocument::class => 'my_repo']);
$repo = new StubRepository($dm, $dm->getUnitOfWork(), new ClassMetadata(CoolDocument::class));
$container = $this->createContainer(['my_repo' => $repo]);
$factory = new ContainerRepositoryFactory($container);
$this->assertSame($repo, $factory->getRepository($dm, CoolDocument::class));
}
public function testGetRepositoryReturnsDocumentRepository()
{
$container = $this->createContainer([]);
$dm = $this->createDocumentManager([BoringDocument::class => null]);
$factory = new ContainerRepositoryFactory($container);
$actualRepo = $factory->getRepository($dm, BoringDocument::class);
$this->assertInstanceOf(DocumentRepository::class, $actualRepo);
// test the same instance is returned
$this->assertSame($actualRepo, $factory->getRepository($dm, BoringDocument::class));
}
public function testCustomRepositoryIsReturned()
{
$container = $this->createContainer([]);
$dm = $this->createDocumentManager([
CustomNormalRepoDocument::class => StubRepository::class,
]);
$factory = new ContainerRepositoryFactory($container);
$actualRepo = $factory->getRepository($dm, CustomNormalRepoDocument::class);
$this->assertInstanceOf(StubRepository::class, $actualRepo);
// test the same instance is returned
$this->assertSame($actualRepo, $factory->getRepository($dm, CustomNormalRepoDocument::class));
}
public function testServiceRepositoriesMustExtendDocumentRepository()
{
$repo = new stdClass();
$container = $this->createContainer(['my_repo' => $repo]);
$dm = $this->createDocumentManager([CoolDocument::class => 'my_repo']);
$factory = new ContainerRepositoryFactory($container);
$this->expectExceptionMessage(
'The service "my_repo" must extend DocumentRepository (or a base class, like ServiceDocumentRepository).'
);
$this->expectException(RuntimeException::class);
$factory->getRepository($dm, CoolDocument::class);
}
public function testRepositoryMatchesServiceInterfaceButServiceNotFound()
{
$container = $this->createContainer([]);
$dm = $this->createDocumentManager([
CoolDocument::class => StubServiceRepository::class,
]);
$factory = new ContainerRepositoryFactory($container);
$this->expectExceptionMessage(sprintf(
'The "%s" document repository implements "%s", but its service could not be found.'
. ' Make sure the service exists and is tagged with "doctrine_mongodb.odm.repository_service".',
StubServiceRepository::class,
ServiceDocumentRepositoryInterface::class
));
$this->expectException(RuntimeException::class);
$factory->getRepository($dm, CoolDocument::class);
}
public function testCustomRepositoryIsNotAValidClass()
{
$container = $this->createContainer([]);
$dm = $this->createDocumentManager([CoolDocument::class => 'not_a_real_class']);
$factory = new ContainerRepositoryFactory($container);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf(
'The "%s" document 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_mongodb.odm.repository_service".',
CoolDocument::class
));
$factory->getRepository($dm, CoolDocument::class);
}
/**
* @return MockObject|ContainerInterface
*/
private function createContainer(array $services)
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->any())
->method('has')
->willReturnCallback(static function ($id) use ($services) {
return isset($services[$id]);
});
$container->expects($this->any())
->method('get')
->willReturnCallback(static function ($id) use ($services) {
return $services[$id];
});
return $container;
}
/**
* @return MockObject|DocumentManager
*/
private function createDocumentManager(array $documentRepositoryClasses)
{
$classMetadatas = [];
foreach ($documentRepositoryClasses as $documentClass => $documentRepositoryClass) {
$metadata = new ClassMetadata($documentClass);
$metadata->customRepositoryClassName = $documentRepositoryClass;
$classMetadatas[$documentClass] = $metadata;
}
$dm = $this->getMockBuilder(DocumentManager::class)->disableOriginalConstructor()->getMock();
$dm->expects($this->any())
->method('getClassMetadata')
->willReturnCallback(static function ($class) use ($classMetadatas) {
return $classMetadatas[$class];
});
$evm = $this->createMock(EventManager::class);
$uow = new UnitOfWork($dm, $evm, new HydratorFactory($dm, $evm, sys_get_temp_dir(), sys_get_temp_dir(), Configuration::AUTOGENERATE_EVAL));
$dm->expects($this->any())
->method('getUnitOfWork')
->willReturn($uow);
$dm->expects($this->any())
->method('getConfiguration')
->willReturn(new Configuration());
return $dm;
}
}
class StubRepository extends DocumentRepository
{
}
class StubServiceRepository extends DocumentRepository implements ServiceDocumentRepositoryInterface
{
}
class CoolDocument
{
}
class BoringDocument
{
}
class CustomNormalRepoDocument
{
}