1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Manager; |
4
|
|
|
|
5
|
|
|
use FOS\ElasticaBundle\Configuration\Search; |
6
|
|
|
use FOS\ElasticaBundle\Manager\RepositoryManager; |
7
|
|
|
|
8
|
|
|
class CustomRepository |
9
|
|
|
{ |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
class Entity |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Richard Miller <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class RepositoryManagerTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder |
23
|
|
|
*/ |
24
|
|
|
private function createFinderMock() |
25
|
|
|
{ |
26
|
|
|
$finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
27
|
|
|
->disableOriginalConstructor() |
28
|
|
|
->getMock(); |
29
|
|
|
|
30
|
|
|
return $finderMock; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader |
35
|
|
|
*/ |
36
|
|
|
private function createReaderMock() |
37
|
|
|
{ |
38
|
|
|
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader') |
39
|
|
|
->disableOriginalConstructor() |
40
|
|
|
->getMock(); |
41
|
|
|
|
42
|
|
|
return $readerMock; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testThatGetRepositoryReturnsDefaultRepository() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$finderMock = $this->createFinderMock(); |
48
|
|
|
$readerMock = $this->createReaderMock(); |
49
|
|
|
|
50
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
51
|
|
|
|
52
|
|
|
$manager = new RepositoryManager($readerMock); |
53
|
|
|
$manager->addEntity($entityName, $finderMock); |
54
|
|
|
$repository = $manager->getRepository($entityName); |
55
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Repository', $repository); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testThatGetRepositoryReturnsCustomRepository() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$finderMock = $this->createFinderMock(); |
61
|
|
|
$readerMock = $this->createReaderMock(); |
62
|
|
|
|
63
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
64
|
|
|
|
65
|
|
|
$manager = new RepositoryManager($readerMock); |
66
|
|
|
$manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\Manager\CustomRepository'); |
67
|
|
|
$repository = $manager->getRepository($entityName); |
68
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @expectedException \RuntimeException |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function testThatGetRepositoryThrowsExceptionIfEntityNotConfigured() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$finderMock = $this->createFinderMock(); |
77
|
|
|
$readerMock = $this->createReaderMock(); |
78
|
|
|
|
79
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
80
|
|
|
|
81
|
|
|
$manager = new RepositoryManager($readerMock); |
82
|
|
|
$manager->addEntity($entityName, $finderMock); |
83
|
|
|
$manager->getRepository('Missing Entity'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @expectedException \RuntimeException |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$finderMock = $this->createFinderMock(); |
92
|
|
|
$readerMock = $this->createReaderMock(); |
93
|
|
|
|
94
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
95
|
|
|
|
96
|
|
|
$manager = new RepositoryManager($readerMock); |
97
|
|
|
$manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository'); |
98
|
|
|
$manager->getRepository('FOS\ElasticaBundle\Tests\Manager\Entity'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @expectedException \RuntimeException |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function testThatGetRepositoryThrowsExceptionIfEntityDoesNotExist() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$finderMock = $this->createFinderMock(); |
107
|
|
|
$readerMock = $this->createReaderMock(); |
108
|
|
|
|
109
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
110
|
|
|
|
111
|
|
|
$manager = new RepositoryManager($readerMock); |
112
|
|
|
$manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository'); |
113
|
|
|
$manager->getRepository('Missing Entity'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testThatGetRepositoryCachesRepositoryInstances() |
117
|
|
|
{ |
118
|
|
|
$finderMock = $this->createFinderMock(); |
119
|
|
|
$readerMock = $this->createReaderMock(); |
120
|
|
|
|
121
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
122
|
|
|
|
123
|
|
|
$manager = new RepositoryManager($readerMock); |
124
|
|
|
$manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\Manager\CustomRepository'); |
125
|
|
|
$repository = $manager->getRepository($entityName); |
126
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository); |
127
|
|
|
|
128
|
|
|
$repository2 = $manager->getRepository($entityName); |
129
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository2); |
130
|
|
|
$this->assertSame($repository, $repository2); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetRepositoryNameCanReadFromClassAnnotation() |
134
|
|
|
{ |
135
|
|
|
$repositoryClass = 'FOS\ElasticaBundle\Tests\Manager\CustomRepository'; |
136
|
|
|
|
137
|
|
|
$annotation = new Search; |
|
|
|
|
138
|
|
|
$annotation->repositoryClass = $repositoryClass; |
139
|
|
|
|
140
|
|
|
$finderMock = $this->createFinderMock(); |
141
|
|
|
$readerMock = $this->createReaderMock(); |
142
|
|
|
|
143
|
|
|
$readerMock |
144
|
|
|
->expects($this->once()) |
145
|
|
|
->method('getClassAnnotation') |
146
|
|
|
->will($this->returnValue($annotation)); |
147
|
|
|
|
148
|
|
|
$entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity'; |
149
|
|
|
|
150
|
|
|
$manager = new RepositoryManager($readerMock); |
151
|
|
|
$manager->addEntity($entityName, $finderMock); |
152
|
|
|
|
153
|
|
|
$repository = $manager->getRepository($entityName); |
154
|
|
|
|
155
|
|
|
$this->assertInstanceOf($repositoryClass, $repository); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.