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\MongoDB; |
13
|
|
|
|
14
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
15
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
18
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
19
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
20
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
21
|
|
|
use Lug\Component\Translation\Model\TranslatableInterface; |
22
|
|
|
use Lug\Component\Translation\Model\TranslatableTrait; |
23
|
|
|
use Lug\Component\Translation\Model\TranslationInterface; |
24
|
|
|
use Lug\Component\Translation\Model\TranslationTrait; |
25
|
|
|
use Lug\Component\Translation\Repository\Doctrine\MongoDB\TranslatableRepository; |
26
|
|
|
use Lug\Component\Translation\Repository\TranslatableRepositoryInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author GeLo <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class TranslatableRepositoryTest extends \PHPUnit_Framework_TestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var TranslatableRepository |
35
|
|
|
*/ |
36
|
|
|
private $translatableRepository; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|DocumentManager |
40
|
|
|
*/ |
41
|
|
|
private $documentManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork |
45
|
|
|
*/ |
46
|
|
|
private $unitOfWork; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
50
|
|
|
*/ |
51
|
|
|
private $classMetadata; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
55
|
|
|
*/ |
56
|
|
|
private $resource; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $class; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
65
|
|
|
*/ |
66
|
|
|
private $localeContext; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
protected function setUp() |
72
|
|
|
{ |
73
|
|
|
if (!class_exists(DocumentManager::class)) { |
74
|
|
|
$this->markTestSkipped(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->documentManager = $this->createDocumentManagerMock(); |
78
|
|
|
$this->unitOfWork = $this->createUnitOfWorkMock(); |
79
|
|
|
$this->classMetadata = $this->createClassMetadataMock(); |
80
|
|
|
$this->resource = $this->createResourceMock(); |
81
|
|
|
$this->localeContext = $this->createLocaleContextMock(); |
82
|
|
|
$this->class = $this->classMetadata->name = TranslatableTest::class; |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->translatableRepository = new TranslatableRepository( |
85
|
|
|
$this->documentManager, |
86
|
|
|
$this->unitOfWork, |
87
|
|
|
$this->classMetadata, |
88
|
|
|
$this->resource, |
89
|
|
|
$this->localeContext |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testInheritance() |
94
|
|
|
{ |
95
|
|
|
$this->assertInstanceOf(TranslatableRepositoryInterface::class, $this->translatableRepository); |
96
|
|
|
$this->assertInstanceOf(DocumentRepository::class, $this->translatableRepository); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testCreateQueryBuilderForCollection() |
100
|
|
|
{ |
101
|
|
|
$this->classMetadata |
|
|
|
|
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('getFieldNames') |
104
|
|
|
->will($this->returnValue([])); |
105
|
|
|
|
106
|
|
|
$this->classMetadata |
107
|
|
|
->expects($this->once()) |
108
|
|
|
->method('getAssociationTargetClass') |
109
|
|
|
->with($this->identicalTo('translations')) |
110
|
|
|
->will($this->returnValue($translationClass = TranslationTest::class)); |
111
|
|
|
|
112
|
|
|
$translationClassMetadata = $this->createClassMetadataMock(); |
113
|
|
|
$translationClassMetadata |
114
|
|
|
->expects($this->once()) |
115
|
|
|
->method('getFieldNames') |
116
|
|
|
->will($this->returnValue(['locale'])); |
117
|
|
|
|
118
|
|
|
$this->documentManager |
|
|
|
|
119
|
|
|
->expects($this->once()) |
120
|
|
|
->method('getClassMetadata') |
121
|
|
|
->with($this->identicalTo($translationClass)) |
122
|
|
|
->will($this->returnValue($translationClassMetadata)); |
123
|
|
|
|
124
|
|
|
$this->documentManager |
125
|
|
|
->expects($this->once()) |
126
|
|
|
->method('createQueryBuilder') |
127
|
|
|
->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
128
|
|
|
|
129
|
|
|
$queryBuilder |
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('field') |
132
|
|
|
->with($this->identicalTo('translations.locale')) |
133
|
|
|
->will($this->returnSelf()); |
134
|
|
|
|
135
|
|
|
$this->localeContext |
|
|
|
|
136
|
|
|
->expects($this->once()) |
137
|
|
|
->method('getLocales') |
138
|
|
|
->will($this->returnValue($locales = ['fr'])); |
139
|
|
|
|
140
|
|
|
$this->localeContext |
141
|
|
|
->expects($this->once()) |
142
|
|
|
->method('getFallbackLocale') |
143
|
|
|
->will($this->returnValue($fallbackLocale = 'en')); |
144
|
|
|
|
145
|
|
|
$queryBuilder |
146
|
|
|
->expects($this->once()) |
147
|
|
|
->method('in') |
148
|
|
|
->with($this->identicalTo(array_merge($locales, [$fallbackLocale]))) |
149
|
|
|
->will($this->returnSelf()); |
150
|
|
|
|
151
|
|
|
$this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|DocumentManager |
156
|
|
|
*/ |
157
|
|
|
private function createDocumentManagerMock() |
158
|
|
|
{ |
159
|
|
|
return $this->createMock(DocumentManager::class); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|UnitOfWork |
164
|
|
|
*/ |
165
|
|
|
private function createUnitOfWorkMock() |
166
|
|
|
{ |
167
|
|
|
return $this->createMock(UnitOfWork::class); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
172
|
|
|
*/ |
173
|
|
|
private function createClassMetadataMock() |
174
|
|
|
{ |
175
|
|
|
return $this->createMock(ClassMetadata::class); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
180
|
|
|
*/ |
181
|
|
|
private function createResourceMock() |
182
|
|
|
{ |
183
|
|
|
return $this->createMock(ResourceInterface::class); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
188
|
|
|
*/ |
189
|
|
|
private function createLocaleContextMock() |
190
|
|
|
{ |
191
|
|
|
return $this->createMock(LocaleContextInterface::class); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Builder |
196
|
|
|
*/ |
197
|
|
|
private function createQueryBuilderMock() |
198
|
|
|
{ |
199
|
|
|
return $this->createMock(Builder::class); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @author GeLo <[email protected]> |
205
|
|
|
*/ |
206
|
|
|
class TranslatableTest implements TranslatableInterface |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
use TranslatableTrait; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @author GeLo <[email protected]> |
213
|
|
|
*/ |
214
|
|
|
class TranslationTest implements TranslationInterface |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
use TranslationTrait; |
217
|
|
|
} |
218
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: