1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Translation\Tests\Repository\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Configuration; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Doctrine\ORM\EntityRepository; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
18
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
19
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
20
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository; |
21
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\RepositoryFactory; |
22
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
23
|
|
|
use Lug\Component\Translation\Repository\Doctrine\ORM\TranslatableRepository; |
24
|
|
|
use Lug\Component\Translation\Repository\Doctrine\ORM\TranslatableRepositoryFactory; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author GeLo <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class TranslatableRepositoryFactoryTest extends \PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var TranslatableRepositoryFactory |
34
|
|
|
*/ |
35
|
|
|
private $repositoryFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
39
|
|
|
*/ |
40
|
|
|
private $container; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface |
44
|
|
|
*/ |
45
|
|
|
private $resourceRegistry; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
49
|
|
|
*/ |
50
|
|
|
private $localeContext; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->resourceRegistry = $this->createServiceRegistryMock(); |
|
|
|
|
58
|
|
|
$this->localeContext = $this->createLocaleContextMock(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->container = $this->createContainerMock(); |
61
|
|
|
$this->container |
|
|
|
|
62
|
|
|
->expects($this->atLeast(1)) |
63
|
|
|
->method('get') |
64
|
|
|
->will($this->returnValueMap([ |
65
|
|
|
[ |
66
|
|
|
'lug.resource.registry', |
67
|
|
|
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, |
68
|
|
|
$this->resourceRegistry, |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
'lug.translation.context.locale', |
72
|
|
|
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, |
73
|
|
|
$this->localeContext, |
74
|
|
|
], |
75
|
|
|
])); |
76
|
|
|
|
77
|
|
|
$this->repositoryFactory = new TranslatableRepositoryFactory($this->container); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testInheritance() |
81
|
|
|
{ |
82
|
|
|
$this->assertInstanceOf(RepositoryFactory::class, $this->repositoryFactory); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testRepository() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$entityManager = $this->createEntityManagerMock(); |
88
|
|
|
$entityManager |
|
|
|
|
89
|
|
|
->expects($this->exactly(3)) |
90
|
|
|
->method('getClassMetadata') |
91
|
|
|
->with($this->identicalTo($entityName = 'entity')) |
92
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
93
|
|
|
|
94
|
|
|
$entityManager |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('getConfiguration') |
97
|
|
|
->will($this->returnValue($configuration = $this->createConfigurationMock())); |
98
|
|
|
|
99
|
|
|
$configuration |
|
|
|
|
100
|
|
|
->expects($this->once()) |
101
|
|
|
->method('getDefaultRepositoryClassName') |
102
|
|
|
->will($this->returnValue($repositoryClass = EntityRepository::class)); |
103
|
|
|
|
104
|
|
|
$classMetadata |
|
|
|
|
105
|
|
|
->expects($this->exactly(3)) |
106
|
|
|
->method('getName') |
107
|
|
|
->will($this->returnValue($model = 'name')); |
108
|
|
|
|
109
|
|
|
$this->resourceRegistry |
110
|
|
|
->expects($this->once()) |
111
|
|
|
->method('getIterator') |
112
|
|
|
->will($this->returnValue(new \ArrayIterator([]))); |
113
|
|
|
|
114
|
|
|
$this->assertInstanceOf( |
115
|
|
|
$repositoryClass, |
116
|
|
|
$repository = $this->repositoryFactory->getRepository($entityManager, $entityName) |
|
|
|
|
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->assertSame($repository, $this->repositoryFactory->getRepository($entityManager, $entityName)); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function testResourceRepository() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$entityManager = $this->createEntityManagerMock(); |
125
|
|
|
$entityManager |
|
|
|
|
126
|
|
|
->expects($this->exactly(3)) |
127
|
|
|
->method('getClassMetadata') |
128
|
|
|
->with($this->identicalTo($entityName = 'entity')) |
129
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
130
|
|
|
|
131
|
|
|
$classMetadata |
|
|
|
|
132
|
|
|
->expects($this->exactly(3)) |
133
|
|
|
->method('getName') |
134
|
|
|
->will($this->returnValue($model = 'name')); |
135
|
|
|
|
136
|
|
|
$this->resourceRegistry |
137
|
|
|
->expects($this->once()) |
138
|
|
|
->method('getIterator') |
139
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
140
|
|
|
|
141
|
|
|
$resource |
|
|
|
|
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('getModel') |
144
|
|
|
->will($this->returnValue($model)); |
145
|
|
|
|
146
|
|
|
$classMetadata->customRepositoryClassName = $repositoryClass = Repository::class; |
147
|
|
|
|
148
|
|
|
$this->assertInstanceOf( |
149
|
|
|
$repositoryClass, |
150
|
|
|
$repository = $this->repositoryFactory->getRepository($entityManager, $entityName) |
|
|
|
|
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->assertSame($repository, $this->repositoryFactory->getRepository($entityManager, $entityName)); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function testTranslatableRepository() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$entityManager = $this->createEntityManagerMock(); |
159
|
|
|
$entityManager |
|
|
|
|
160
|
|
|
->expects($this->exactly(3)) |
161
|
|
|
->method('getClassMetadata') |
162
|
|
|
->with($this->identicalTo($entityName = 'entity')) |
163
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
164
|
|
|
|
165
|
|
|
$classMetadata |
|
|
|
|
166
|
|
|
->expects($this->exactly(3)) |
167
|
|
|
->method('getName') |
168
|
|
|
->will($this->returnValue($model = 'name')); |
169
|
|
|
|
170
|
|
|
$this->resourceRegistry |
171
|
|
|
->expects($this->once()) |
172
|
|
|
->method('getIterator') |
173
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
174
|
|
|
|
175
|
|
|
$resource |
|
|
|
|
176
|
|
|
->expects($this->once()) |
177
|
|
|
->method('getModel') |
178
|
|
|
->will($this->returnValue($model)); |
179
|
|
|
|
180
|
|
|
$classMetadata->customRepositoryClassName = $repositoryClass = TranslatableRepository::class; |
181
|
|
|
|
182
|
|
|
$this->assertInstanceOf( |
183
|
|
|
$repositoryClass, |
184
|
|
|
$repository = $this->repositoryFactory->getRepository($entityManager, $entityName) |
|
|
|
|
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->assertSame($repository, $this->repositoryFactory->getRepository($entityManager, $entityName)); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface |
192
|
|
|
*/ |
193
|
|
|
private function createServiceRegistryMock() |
194
|
|
|
{ |
195
|
|
|
return $this->getMock(RegistryInterface::class); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
200
|
|
|
*/ |
201
|
|
|
private function createContainerMock() |
202
|
|
|
{ |
203
|
|
|
return $this->getMock(ContainerInterface::class); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
208
|
|
|
*/ |
209
|
|
|
private function createLocaleContextMock() |
210
|
|
|
{ |
211
|
|
|
return $this->getMock(LocaleContextInterface::class); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface |
216
|
|
|
*/ |
217
|
|
|
private function createEntityManagerMock() |
218
|
|
|
{ |
219
|
|
|
return $this->getMock(EntityManagerInterface::class); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
224
|
|
|
*/ |
225
|
|
|
private function createClassMetadataMock() |
226
|
|
|
{ |
227
|
|
|
return $this->getMockBuilder(ClassMetadata::class) |
228
|
|
|
->disableOriginalConstructor() |
229
|
|
|
->getMock(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Configuration |
234
|
|
|
*/ |
235
|
|
|
private function createConfigurationMock() |
236
|
|
|
{ |
237
|
|
|
return $this->getMock(Configuration::class); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
242
|
|
|
*/ |
243
|
|
|
private function createResourceMock() |
244
|
|
|
{ |
245
|
|
|
return $this->getMock(ResourceInterface::class); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.