|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory; |
|
6
|
|
|
use Doctrine\ORM\Configuration; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\EntityRepository; |
|
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ContainerRepositoryFactoryTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testGetRepositoryReturnsService() |
|
16
|
|
|
{ |
|
17
|
|
|
$em = $this->createEntityManager(array( |
|
18
|
|
|
'Foo\CoolEntity' => 'my_repo' |
|
19
|
|
|
)); |
|
20
|
|
|
$repo = new StubRepository($em, new ClassMetadata('')); |
|
21
|
|
|
$container = $this->createContainer(array( |
|
22
|
|
|
'my_repo' => $repo |
|
23
|
|
|
)); |
|
24
|
|
|
|
|
25
|
|
|
$factory = new ContainerRepositoryFactory($container); |
|
26
|
|
|
$this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testGetRepositoryReturnsEntityRepository() |
|
30
|
|
|
{ |
|
31
|
|
|
$container = $this->createContainer(array()); |
|
32
|
|
|
$em = $this->createEntityManager(array( |
|
33
|
|
|
'Foo\BoringEntity' => null |
|
34
|
|
|
)); |
|
35
|
|
|
|
|
36
|
|
|
$factory = new ContainerRepositoryFactory($container); |
|
37
|
|
|
$actualRepo = $factory->getRepository($em, 'Foo\BoringEntity'); |
|
38
|
|
|
$this->assertInstanceOf(EntityRepository::class, $actualRepo); |
|
39
|
|
|
// test the same instance is returned |
|
40
|
|
|
$this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @expectedException \RuntimeException |
|
45
|
|
|
* @expectedExceptionMessage The service "my_repo" must extend EntityRepository (or a base class, like ServiceEntityRepository). |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function testServiceRepositoriesMustExtendEntityRepository() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$repo = new \stdClass(); |
|
50
|
|
|
$container = $this->createContainer(array( |
|
51
|
|
|
'my_repo' => $repo |
|
52
|
|
|
)); |
|
53
|
|
|
$em = $this->createEntityManager(array( |
|
54
|
|
|
'Foo\CoolEntity' => 'my_repo' |
|
55
|
|
|
)); |
|
56
|
|
|
|
|
57
|
|
|
$factory = new ContainerRepositoryFactory($container); |
|
58
|
|
|
$factory->getRepository($em, 'Foo\CoolEntity'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @expectedException \RuntimeException |
|
63
|
|
|
* @expectedExceptionMessage Could not find the repository service for the "Foo\CoolEntity" entity. Make sure the "my_repo" service exists and is tagged with "doctrine.repository_service". |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function testIfServiceDoesNotExist() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$container = $this->createContainer(array()); |
|
68
|
|
|
$em = $this->createEntityManager(array( |
|
69
|
|
|
'Foo\CoolEntity' => 'my_repo' |
|
70
|
|
|
)); |
|
71
|
|
|
|
|
72
|
|
|
$factory = new ContainerRepositoryFactory($container); |
|
73
|
|
|
$factory->getRepository($em, 'Foo\CoolEntity'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function createContainer(array $services) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
|
79
|
|
|
$container->expects($this->any()) |
|
80
|
|
|
->method('has') |
|
81
|
|
|
->willReturnCallback(function($id) use ($services) { |
|
82
|
|
|
return isset($services[$id]); |
|
83
|
|
|
}); |
|
84
|
|
|
$container->expects($this->any()) |
|
85
|
|
|
->method('get') |
|
86
|
|
|
->willReturnCallback(function($id) use ($services) { |
|
87
|
|
|
return $services[$id]; |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
return $container; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function createEntityManager(array $entityRepositoryClasses) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$classMetadatas = array(); |
|
96
|
|
|
foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) { |
|
97
|
|
|
$metadata = new ClassMetadata($entityClass); |
|
98
|
|
|
$metadata->customRepositoryClassName = $entityRepositoryClass; |
|
99
|
|
|
|
|
100
|
|
|
$classMetadatas[$entityClass] = $metadata; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$em = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
104
|
|
|
$em->expects($this->any()) |
|
105
|
|
|
->method('getClassMetadata') |
|
106
|
|
|
->willReturnCallback(function($class) use ($classMetadatas) { |
|
107
|
|
|
return $classMetadatas[$class]; |
|
108
|
|
|
}); |
|
109
|
|
|
|
|
110
|
|
|
$em->expects($this->any()) |
|
111
|
|
|
->method('getConfiguration') |
|
112
|
|
|
->willReturn(new Configuration()); |
|
113
|
|
|
|
|
114
|
|
|
return $em; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
class StubRepository extends EntityRepository |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
} |
|
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.