Completed
Push — in-memory-cache2 ( 8ddfe2...65ff47 )
by André
17:51
created

CachingLanguageHandlerTest::getLanguageHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertInternalType() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3369

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $handler->delete(2) (which targets eZ\Publish\Core\Persiste...achingHandler::delete()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \eZ\Publish\Core\Per...getLanguageCacheMock()) of type object<eZ\Publish\Core\P...anguage\CachingHandler> is incompatible with the declared type object<eZ\Publish\Core\P...ntent\Language\Handler> of property $languageHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
300
                $this->getInnerLanguageHandlerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getInnerLanguageHandlerMock() targeting eZ\Publish\Core\Persiste...erLanguageHandlerMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Persiste...gHandler::__construct() does only seem to accept object<eZ\Publish\SPI\Pe...ntent\Language\Handler>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
301
                $this->getLanguageCacheMock()
0 ignored issues
show
Bug introduced by
It seems like $this->getLanguageCacheMock() targeting eZ\Publish\Core\Persiste...:getLanguageCacheMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Persiste...gHandler::__construct() does only seem to accept object<eZ\Publish\Core\P...InMemory\InMemoryCache>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
302
            );
303
        }
304
305
        return $this->languageHandler;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->languageHandler; of type eZ\Publish\Core\Persiste...ontent\Language\Handler adds the type eZ\Publish\Core\Persiste...ontent\Language\Handler to the return on line 305 which is incompatible with the return type documented by eZ\Publish\Core\Persiste...est::getLanguageHandler of type eZ\Publish\Core\Persiste...Language\CachingHandler.
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...anguage\Handler::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\SPI\Pe...ntent\Language\Handler> of property $innerHandlerMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...y\InMemoryCache::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...InMemory\InMemoryCache> of property $languageCacheMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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