Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

URLHandlerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 245
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
B testUpdateUrl() 0 32 1
A testFind() 0 13 1
B testLoadByIdWithCache() 0 24 1
A testLoadByIdWithoutCache() 0 49 1
B testFindUsagesWithCache() 0 25 1
A testFindUsagesWithoutCache() 0 50 1
A getUrl() 0 7 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Tests\Core\Persistence\Cache;
8
9
use eZ\Publish\API\Repository\Values\URL\URLQuery;
10
use eZ\Publish\Core\Persistence\Cache\CacheServiceDecorator;
11
use eZ\Publish\Core\Persistence\Cache\PersistenceLogger;
12
use eZ\Publish\Core\Persistence\Cache\URLHandler as CacheUrlHandler;
13
use eZ\Publish\SPI\Persistence\Handler;
14
use eZ\Publish\SPI\Persistence\URL\Handler as UrlHandler;
15
use eZ\Publish\SPI\Persistence\URL\URL;
16
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct;
17
use PHPUnit\Framework\TestCase;
18
use Stash\Interfaces\ItemInterface;
19
20
class URLHandlerTest extends TestCase
21
{
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockObject
24
     */
25
    private $cache;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $persistenceHandler;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $logger;
36
37
    /**
38
     * @var \eZ\Publish\Core\Persistence\Cache\URLHandler
39
     */
40
    private $urlHandler;
41
42
    protected function setUp()
43
    {
44
        parent::setUp();
45
        $this->cache = $this->createMock(CacheServiceDecorator::class);
46
        $this->persistenceHandler = $this->createMock(UrlHandler::class);
47
48
        $persistence = $this->createMock(Handler::class);
49
        $persistence
50
            ->expects($this->any())
51
            ->method('urlHandler')
52
            ->willReturn($this->persistenceHandler);
53
54
        $this->logger = $this->createMock(PersistenceLogger::class);
55
        $this->urlHandler = new CacheUrlHandler($this->cache, $persistence, $this->logger);
56
    }
57
58
    public function testUpdateUrl()
59
    {
60
        $urlUpdateStruct = new URLUpdateStruct();
61
        $url = $this->getUrl();
62
63
        $this->logger
64
            ->expects($this->once())
65
            ->method('logCall')
66
            ->with('eZ\Publish\Core\Persistence\Cache\URLHandler::updateUrl',
67
                [
68
                    'url' => $url->id,
69
                    'struct' => $urlUpdateStruct,
70
                ]);
71
72
        $this->persistenceHandler
73
            ->expects($this->once())
74
            ->method('updateUrl')
75
            ->with($url->id, $urlUpdateStruct)
76
            ->will($this->returnValue($url));
77
78
        $this->cache
79
            ->expects($this->at(0))
80
            ->method('clear')
81
            ->with('url', $url->id);
82
83
        $this->cache
84
            ->expects($this->at(1))
85
            ->method('clear')
86
            ->with('content');
87
88
        $this->assertEquals($url, $this->urlHandler->updateUrl($url->id, $urlUpdateStruct));
89
    }
90
91
    public function testFind()
92
    {
93
        $query = new URLQuery();
94
95
        $this->logger
96
            ->expects($this->once())
97
            ->method('logCall')
98
            ->with('eZ\Publish\Core\Persistence\Cache\URLHandler::find', [
99
                'query' => $query,
100
            ]);
101
102
        $this->urlHandler->find($query);
103
    }
104
105
    public function testLoadByIdWithCache()
106
    {
107
        $url = $this->getUrl();
108
109
        $cacheItem = $this->createMock(ItemInterface::class);
110
111
        $this->cache
112
            ->expects($this->once())
113
            ->method('getItem')
114
            ->with('url', $url->id)
115
            ->will($this->returnValue($cacheItem));
116
117
        $cacheItem
118
            ->expects($this->at(0))
119
            ->method('get')
120
            ->will($this->returnValue($url));
121
122
        $cacheItem
123
            ->expects($this->at(1))
124
            ->method('isMiss')
125
            ->will($this->returnValue(false));
126
127
        $this->assertEquals($url, $this->urlHandler->loadById($url->id));
128
    }
129
130
    public function testLoadByIdWithoutCache()
131
    {
132
        $url = $this->getUrl();
133
134
        $cacheItem = $this->createMock(ItemInterface::class);
135
136
        $this->cache
137
            ->expects($this->once())
138
            ->method('getItem')
139
            ->with('url', $url->id)
140
            ->will($this->returnValue($cacheItem));
141
142
        $cacheItem
143
            ->expects($this->at(0))
144
            ->method('get')
145
            ->will($this->returnValue($url));
146
147
        $cacheItem
148
            ->expects($this->at(1))
149
            ->method('isMiss')
150
            ->will($this->returnValue(true));
151
152
        $this->logger
153
            ->expects($this->once())
154
            ->method('logCall')
155
            ->with('eZ\Publish\Core\Persistence\Cache\URLHandler::loadById', [
156
                'url' => $url->id,
157
            ]);
158
159
        $this->persistenceHandler
160
            ->expects($this->once())
161
            ->method('loadById')
162
            ->with($url->id)
163
            ->will($this->returnValue($url));
164
165
        $cacheItem
166
            ->expects($this->any())
167
            ->method('set')
168
            ->with($url)
169
            ->will($this->returnSelf());
170
171
        $cacheItem
172
            ->expects($this->any())
173
            ->method('save')
174
            ->with()
175
            ->will($this->returnSelf());
176
177
        $this->assertEquals($url, $this->urlHandler->loadById($url->id));
178
    }
179
180
    public function testFindUsagesWithCache()
181
    {
182
        $url = $this->getUrl();
183
        $usages = [1, 2, 3];
184
185
        $cacheItem = $this->createMock(ItemInterface::class);
186
187
        $this->cache
188
            ->expects($this->once())
189
            ->method('getItem')
190
            ->with('url', $url->id, 'usages')
191
            ->will($this->returnValue($cacheItem));
192
193
        $cacheItem
194
            ->expects($this->at(0))
195
            ->method('get')
196
            ->will($this->returnValue($usages));
197
198
        $cacheItem
199
            ->expects($this->at(1))
200
            ->method('isMiss')
201
            ->will($this->returnValue(false));
202
203
        $this->assertEquals($usages, $this->urlHandler->findUsages($url->id));
204
    }
205
206
    public function testFindUsagesWithoutCache()
207
    {
208
        $url = $this->getUrl();
209
        $usages = [1, 2, 3];
210
211
        $cacheItem = $this->createMock(ItemInterface::class);
212
213
        $this->cache
214
            ->expects($this->once())
215
            ->method('getItem')
216
            ->with('url', $url->id, 'usages')
217
            ->will($this->returnValue($cacheItem));
218
219
        $cacheItem
220
            ->expects($this->at(0))
221
            ->method('get')
222
            ->will($this->returnValue($usages));
223
224
        $cacheItem
225
            ->expects($this->at(1))
226
            ->method('isMiss')
227
            ->will($this->returnValue(true));
228
229
        $this->logger
230
            ->expects($this->once())
231
            ->method('logCall')
232
            ->with('eZ\Publish\Core\Persistence\Cache\URLHandler::findUsages',
233
                [
234
                    'url' => $url->id,
235
                ]);
236
        $this->persistenceHandler
237
            ->expects($this->once())
238
            ->method('findUsages')
239
            ->with($url->id)
240
            ->will($this->returnValue($usages));
241
242
        $cacheItem
243
            ->expects($this->any())
244
            ->method('set')
245
            ->with($usages)
246
            ->will($this->returnSelf());
247
248
        $cacheItem
249
            ->expects($this->any())
250
            ->method('save')
251
            ->with()
252
            ->will($this->returnSelf());
253
254
        $this->assertEquals($usages, $this->urlHandler->findUsages($url->id));
255
    }
256
257
    private function getUrl()
258
    {
259
        $url = new URL();
260
        $url->id = 12;
261
262
        return $url;
263
    }
264
}
265