1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\Language\CachingLanguageHandlerTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\Language; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException; |
12
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase; |
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language; |
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct as SPILanguageCreateStruct; |
15
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
16
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as SPILanguageHandler; |
17
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler; |
18
|
|
|
use eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test case for caching Language Handler. |
22
|
|
|
*/ |
23
|
|
|
class CachingLanguageHandlerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Language handler. |
27
|
|
|
* |
28
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
29
|
|
|
*/ |
30
|
|
|
protected $languageHandler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Inner language handler mock. |
34
|
|
|
* |
35
|
|
|
* @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
36
|
|
|
*/ |
37
|
|
|
protected $innerHandlerMock; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Language cache mock. |
41
|
|
|
* |
42
|
|
|
* @var \eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache |
43
|
|
|
*/ |
44
|
|
|
protected $languageCacheMock; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::__construct |
48
|
|
|
*/ |
49
|
|
|
public function testCtorPropertyInnerHandler() |
50
|
|
|
{ |
51
|
|
|
$handler = $this->getLanguageHandler(); |
52
|
|
|
|
53
|
|
|
$this->assertAttributeSame( |
|
|
|
|
54
|
|
|
$this->getInnerLanguageHandlerMock(), |
55
|
|
|
'innerHandler', |
56
|
|
|
$handler |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::__construct |
62
|
|
|
*/ |
63
|
|
|
public function testCtorPropertyLanguageCache() |
64
|
|
|
{ |
65
|
|
|
$handler = $this->getLanguageHandler(); |
66
|
|
|
|
67
|
|
|
$this->assertAttributeSame( |
|
|
|
|
68
|
|
|
$this->getLanguageCacheMock(), |
69
|
|
|
'cache', |
70
|
|
|
$handler |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::create |
76
|
|
|
*/ |
77
|
|
|
public function testCreate() |
78
|
|
|
{ |
79
|
|
|
$handler = $this->getLanguageHandler(); |
80
|
|
|
$innerHandlerMock = $this->getInnerLanguageHandlerMock(); |
81
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
82
|
|
|
|
83
|
|
|
$languageFixture = $this->getLanguageFixture(); |
84
|
|
|
|
85
|
|
|
$innerHandlerMock->expects($this->once()) |
86
|
|
|
->method('create') |
87
|
|
|
->with( |
88
|
|
|
$this->isInstanceOf( |
89
|
|
|
SPILanguageCreateStruct::class |
90
|
|
|
) |
91
|
|
|
)->will($this->returnValue($languageFixture)); |
92
|
|
|
|
93
|
|
|
$cacheMock->expects($this->once()) |
94
|
|
|
->method('setMulti') |
95
|
|
|
->with($this->equalTo([$languageFixture])); |
96
|
|
|
|
97
|
|
|
$createStruct = $this->getCreateStructFixture(); |
98
|
|
|
|
99
|
|
|
$result = $handler->create($createStruct); |
100
|
|
|
|
101
|
|
|
$this->assertEquals( |
102
|
|
|
$languageFixture, |
103
|
|
|
$result |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns a Language CreateStruct. |
109
|
|
|
* |
110
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct |
111
|
|
|
*/ |
112
|
|
|
protected function getCreateStructFixture() |
113
|
|
|
{ |
114
|
|
|
return new Language\CreateStruct(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a Language. |
119
|
|
|
* |
120
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language |
121
|
|
|
*/ |
122
|
|
|
protected function getLanguageFixture() |
123
|
|
|
{ |
124
|
|
|
$language = new Language(); |
125
|
|
|
$language->id = 8; |
126
|
|
|
$language->languageCode = 'de-DE'; |
127
|
|
|
|
128
|
|
|
return $language; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::update |
133
|
|
|
*/ |
134
|
|
|
public function testUpdate() |
135
|
|
|
{ |
136
|
|
|
$handler = $this->getLanguageHandler(); |
137
|
|
|
|
138
|
|
|
$innerHandlerMock = $this->getInnerLanguageHandlerMock(); |
139
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
140
|
|
|
|
141
|
|
|
$innerHandlerMock->expects($this->once()) |
142
|
|
|
->method('update') |
143
|
|
|
->with($this->getLanguageFixture()); |
144
|
|
|
|
145
|
|
|
$languageFixture = $this->getLanguageFixture(); |
146
|
|
|
$cacheMock->expects($this->once()) |
147
|
|
|
->method('setMulti') |
148
|
|
|
->with($this->equalTo([$languageFixture])); |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
$handler->update($languageFixture); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::load |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function testLoad() |
158
|
|
|
{ |
159
|
|
|
$handler = $this->getLanguageHandler(); |
160
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
161
|
|
|
|
162
|
|
|
$cacheMock->expects($this->once()) |
163
|
|
|
->method('get') |
164
|
|
|
->with($this->equalTo('ez-language-2')) |
165
|
|
|
->willReturn($this->getLanguageFixture()); |
166
|
|
|
|
167
|
|
|
$result = $handler->load(2); |
168
|
|
|
|
169
|
|
|
$this->assertEquals( |
170
|
|
|
$this->getLanguageFixture(), |
171
|
|
|
$result |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::load |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function testLoadFailure() |
179
|
|
|
{ |
180
|
|
|
$handler = $this->getLanguageHandler(); |
181
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
182
|
|
|
$innerHandlerMock = $this->getInnerLanguageHandlerMock(); |
183
|
|
|
|
184
|
|
|
$cacheMock->expects($this->once()) |
185
|
|
|
->method('get') |
186
|
|
|
->with($this->equalTo('ez-language-2')) |
187
|
|
|
->willReturn(null); |
188
|
|
|
|
189
|
|
|
$innerHandlerMock->expects($this->once()) |
190
|
|
|
->method('load') |
191
|
|
|
->with($this->equalTo(2)) |
192
|
|
|
->will( |
193
|
|
|
$this->throwException( |
194
|
|
|
new NotFoundException('Language', 2) |
195
|
|
|
) |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->expectException(APINotFoundException::class); |
199
|
|
|
$handler->load(2); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::loadByLanguageCode |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function testLoadByLanguageCode() |
206
|
|
|
{ |
207
|
|
|
$handler = $this->getLanguageHandler(); |
208
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
209
|
|
|
|
210
|
|
|
$cacheMock->expects($this->once()) |
211
|
|
|
->method('get') |
212
|
|
|
->with($this->equalTo('ez-language-code-eng-US')) |
213
|
|
|
->willReturn($this->getLanguageFixture()); |
214
|
|
|
|
215
|
|
|
$result = $handler->loadByLanguageCode('eng-US'); |
216
|
|
|
|
217
|
|
|
$this->assertEquals( |
218
|
|
|
$this->getLanguageFixture(), |
219
|
|
|
$result |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::loadByLanguageCode |
225
|
|
|
*/ |
226
|
|
View Code Duplication |
public function testLoadByLanguageCodeFailure() |
227
|
|
|
{ |
228
|
|
|
$handler = $this->getLanguageHandler(); |
229
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
230
|
|
|
$innerHandlerMock = $this->getInnerLanguageHandlerMock(); |
231
|
|
|
|
232
|
|
|
$cacheMock->expects($this->once()) |
233
|
|
|
->method('get') |
234
|
|
|
->with($this->equalTo('ez-language-code-eng-US')) |
235
|
|
|
->willReturn(null); |
236
|
|
|
|
237
|
|
|
$innerHandlerMock->expects($this->once()) |
238
|
|
|
->method('loadByLanguageCode') |
239
|
|
|
->with($this->equalTo('eng-US')) |
240
|
|
|
->will( |
241
|
|
|
$this->throwException( |
242
|
|
|
new NotFoundException('Language', 2) |
243
|
|
|
) |
244
|
|
|
); |
245
|
|
|
|
246
|
|
|
$this->expectException(APINotFoundException::class); |
247
|
|
|
$handler->loadByLanguageCode('eng-US'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler::loadAll |
252
|
|
|
*/ |
253
|
|
|
public function testLoadAll() |
254
|
|
|
{ |
255
|
|
|
$handler = $this->getLanguageHandler(); |
256
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
257
|
|
|
|
258
|
|
|
$cacheMock->expects($this->once()) |
259
|
|
|
->method('get') |
260
|
|
|
->with($this->equalTo('ez-language-list')) |
261
|
|
|
->willReturn([]); |
262
|
|
|
|
263
|
|
|
$result = $handler->loadAll(); |
264
|
|
|
|
265
|
|
|
$this->assertInternalType( |
|
|
|
|
266
|
|
|
'array', |
267
|
|
|
$result |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler::delete |
273
|
|
|
*/ |
274
|
|
|
public function testDelete() |
275
|
|
|
{ |
276
|
|
|
$handler = $this->getLanguageHandler(); |
277
|
|
|
$cacheMock = $this->getLanguageCacheMock(); |
278
|
|
|
$innerHandlerMock = $this->getInnerLanguageHandlerMock(); |
279
|
|
|
|
280
|
|
|
$innerHandlerMock->expects($this->once()) |
281
|
|
|
->method('delete') |
282
|
|
|
->with($this->equalTo(2)); |
283
|
|
|
|
284
|
|
|
$cacheMock->expects($this->once()) |
285
|
|
|
->method('deleteMulti') |
286
|
|
|
->with($this->equalTo(['ez-language-2', 'ez-language-list'])); |
287
|
|
|
|
288
|
|
|
$result = $handler->delete(2); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Returns the language handler to test. |
293
|
|
|
* |
294
|
|
|
* @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
295
|
|
|
*/ |
296
|
|
|
protected function getLanguageHandler() |
297
|
|
|
{ |
298
|
|
|
if (!isset($this->languageHandler)) { |
299
|
|
|
$this->languageHandler = new CachingHandler( |
|
|
|
|
300
|
|
|
$this->getInnerLanguageHandlerMock(), |
|
|
|
|
301
|
|
|
$this->getLanguageCacheMock() |
|
|
|
|
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $this->languageHandler; |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Returns a mock for the inner language handler. |
310
|
|
|
* |
311
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject |
312
|
|
|
*/ |
313
|
|
|
protected function getInnerLanguageHandlerMock() |
314
|
|
|
{ |
315
|
|
|
if (!isset($this->innerHandlerMock)) { |
316
|
|
|
$this->innerHandlerMock = $this->createMock(SPILanguageHandler::class); |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $this->innerHandlerMock; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns a mock for the in-memory cache. |
324
|
|
|
* |
325
|
|
|
* @return \eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache|\PHPUnit\Framework\MockObject\MockObject |
326
|
|
|
*/ |
327
|
|
|
protected function getLanguageCacheMock() |
328
|
|
|
{ |
329
|
|
|
if (!isset($this->languageCacheMock)) { |
330
|
|
|
$this->languageCacheMock = $this->createMock(InMemoryCache::class); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $this->languageCacheMock; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Returns an array with 2 languages. |
338
|
|
|
* |
339
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language[] |
340
|
|
|
*/ |
341
|
|
View Code Duplication |
protected function getLanguagesFixture() |
342
|
|
|
{ |
343
|
|
|
$langUs = new Language(); |
344
|
|
|
$langUs->id = 2; |
345
|
|
|
$langUs->languageCode = 'eng-US'; |
346
|
|
|
$langUs->name = 'English (American)'; |
347
|
|
|
$langUs->isEnabled = true; |
348
|
|
|
|
349
|
|
|
$langGb = new Language(); |
350
|
|
|
$langGb->id = 4; |
351
|
|
|
$langGb->languageCode = 'eng-GB'; |
352
|
|
|
$langGb->name = 'English (United Kingdom)'; |
353
|
|
|
$langGb->isEnabled = true; |
354
|
|
|
|
355
|
|
|
return array($langUs, $langGb); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.