-
-
Notifications
You must be signed in to change notification settings - Fork 447
/
ContainerRepositoryFactoryTest.php
162 lines (131 loc) · 6.18 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
<?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;
use stdClass;
class ContainerRepositoryFactoryTest extends TestCase
{
public function testGetRepositoryReturnsService()
{
$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()
{
$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()
{
$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()
{
$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()
{
$repo = $this->getMockBuilder(ObjectRepository::class)->getMock();
$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->assertSame($repo, $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()
{
$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()
{
$container = $this->createContainer([]);
$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(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;
}
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(static 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
{
}