Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a...6abe82 )
by
unknown
79:54 queued 33:48
created

LocationAwareStoreTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 263
Duplicated Lines 47.15 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 124
loc 263
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 7

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 5 1
A testGetFilesystem() 0 4 1
A testSetFilesystem() 0 6 1
A testGetPath() 0 6 1
A testGetStalePath() 0 13 1
A testGetPathDeadProcess() 0 15 2
A getFilesystemMock() 0 4 1
B testPurgeByRequestSingleLocation() 0 30 1
B testPurgeByRequestMultipleLocationsBC() 33 33 2
B testPurgeByRequestMultipleLocations() 33 33 2
B testPurgeAllContent() 0 27 1
B testPurgeAllContentByRequest() 29 29 1
B testPurgeAllContentByRequestBC() 29 29 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the LocationAwareStoreTest 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\MVC\Symfony\CacheTests\Http;
12
13
use eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\HttpFoundation\Request;
16
use PHPUnit_Framework_TestCase;
17
18
class LocationAwareStoreTest extends PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\LocationAwareStore
22
     */
23
    private $store;
24
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
        $this->store = new LocationAwareStore(__DIR__);
29
    }
30
31
    protected function tearDown()
32
    {
33
        array_map('unlink', glob(__DIR__ . '/*.purging'));
34
        parent::tearDown();
35
    }
36
37
    public function testGetFilesystem()
38
    {
39
        $this->assertInstanceOf('Symfony\\Component\\Filesystem\\Filesystem', $this->store->getFilesystem());
40
    }
41
42
    public function testSetFilesystem()
43
    {
44
        $fs = new Filesystem();
45
        $this->store->setFilesystem($fs);
46
        $this->assertSame($fs, $this->store->getFilesystem());
47
    }
48
49
    public function testGetPath()
50
    {
51
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . '123' . DIRECTORY_SEPARATOR;
52
        $path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent'));
53
        $this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefix) === 0);
54
    }
55
56
    public function testGetStalePath()
57
    {
58
        // Generate the lock file to force using the stale cache dir
59
        $locationId = 123;
60
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId;
61
        $prefixStale = LocationAwareStore::LOCATION_STALE_CACHE_DIR . DIRECTORY_SEPARATOR . $locationId;
62
        $lockFile = $this->store->getLocationCacheLockName($locationId);
63
        file_put_contents($lockFile, getmypid());
64
65
        $path = $this->store->getPath($prefix . DIRECTORY_SEPARATOR . 'en' . sha1('someContent'));
66
        $this->assertTrue(strpos($path, __DIR__ . DIRECTORY_SEPARATOR . $prefixStale) === 0);
67
        @unlink($lockFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
68
    }
69
70
    public function testGetPathDeadProcess()
71
    {
72
        if (!function_exists('posix_kill')) {
73
            self::markTestSkipped('posix_kill() function is needed for this test');
74
        }
75
76
        $locationId = 123;
77
        $prefix = LocationAwareStore::LOCATION_CACHE_DIR . "/$locationId";
78
        $lockFile = $this->store->getLocationCacheLockName($locationId);
79
        file_put_contents($lockFile, '99999999999999999');
80
81
        $path = $this->store->getPath("$prefix/en" . sha1('someContent'));
82
        $this->assertTrue(strpos($path, __DIR__ . "/$prefix") === 0);
83
        $this->assertFalse(file_exists($lockFile));
84
    }
85
86
    /**
87
     * @return \PHPUnit_Framework_MockObject_MockObject
88
     */
89
    private function getFilesystemMock()
90
    {
91
        return $this->getMock('Symfony\\Component\\Filesystem\\Filesystem');
92
    }
93
94
    public function testPurgeByRequestSingleLocation()
95
    {
96
        $fs = $this->getFilesystemMock();
97
        $this->store->setFilesystem($fs);
98
        $locationId = 123;
99
        $locationCacheDir = $this->store->getLocationCacheDir($locationId);
100
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
101
102
        $fs
103
            ->expects($this->any())
104
            ->method('exists')
105
            ->with($locationCacheDir)
106
            ->will($this->returnValue(true));
107
        $fs
108
            ->expects($this->once())
109
            ->method('mkdir')
110
            ->with($staleCacheDir);
111
        $fs
112
            ->expects($this->once())
113
            ->method('mirror')
114
            ->with($locationCacheDir, $staleCacheDir);
115
        $fs
116
            ->expects($this->once())
117
            ->method('remove')
118
            ->with(array($staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir));
119
120
        $request = Request::create('/', 'PURGE');
121
        $request->headers->set('X-Location-Id', "$locationId");
122
        $this->store->purgeByRequest($request);
123
    }
124
125 View Code Duplication
    public function testPurgeByRequestMultipleLocationsBC()
126
    {
127
        $fs = $this->getFilesystemMock();
128
        $this->store->setFilesystem($fs);
129
        $locationIds = array(123, 456, 789);
130
        $i = 0;
131
        foreach ($locationIds as $locationId) {
132
            $locationCacheDir = $this->store->getLocationCacheDir($locationId);
133
            $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
134
135
            $fs
136
                ->expects($this->at($i++))
137
                ->method('exists')
138
                ->with($locationCacheDir)
139
                ->will($this->returnValue(true));
140
            $fs
141
                ->expects($this->at($i++))
142
                ->method('mkdir')
143
                ->with($staleCacheDir);
144
            $fs
145
                ->expects($this->at($i++))
146
                ->method('mirror')
147
                ->with($locationCacheDir, $staleCacheDir);
148
            $fs
149
                ->expects($this->at($i++))
150
                ->method('remove')
151
                ->with(array($staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir));
152
        }
153
154
        $request = Request::create('/', 'PURGE');
155
        $request->headers->set('X-Group-Location-Id', implode('; ', $locationIds));
156
        $this->store->purgeByRequest($request);
157
    }
158
159 View Code Duplication
    public function testPurgeByRequestMultipleLocations()
160
    {
161
        $fs = $this->getFilesystemMock();
162
        $this->store->setFilesystem($fs);
163
        $locationIds = array(123, 456, 789);
164
        $i = 0;
165
        foreach ($locationIds as $locationId) {
166
            $locationCacheDir = $this->store->getLocationCacheDir($locationId);
167
            $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
168
169
            $fs
170
                ->expects($this->at($i++))
171
                ->method('exists')
172
                ->with($locationCacheDir)
173
                ->will($this->returnValue(true));
174
            $fs
175
                ->expects($this->at($i++))
176
                ->method('mkdir')
177
                ->with($staleCacheDir);
178
            $fs
179
                ->expects($this->at($i++))
180
                ->method('mirror')
181
                ->with($locationCacheDir, $staleCacheDir);
182
            $fs
183
                ->expects($this->at($i++))
184
                ->method('remove')
185
                ->with(array($staleCacheDir, $this->store->getLocationCacheLockName($locationId), $locationCacheDir));
186
        }
187
188
        $request = Request::create('/', 'BAN');
189
        $request->headers->set('X-Location-Id', '(' . implode('|', $locationIds) . ')');
190
        $this->store->purgeByRequest($request);
191
    }
192
193
    public function testPurgeAllContent()
194
    {
195
        $fs = $this->getFilesystemMock();
196
        $this->store->setFilesystem($fs);
197
        $locationCacheDir = $this->store->getLocationCacheDir();
198
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
199
200
        $fs
201
            ->expects($this->any())
202
            ->method('exists')
203
            ->with($locationCacheDir)
204
            ->will($this->returnValue(true));
205
        $fs
206
            ->expects($this->once())
207
            ->method('mkdir')
208
            ->with($staleCacheDir);
209
        $fs
210
            ->expects($this->once())
211
            ->method('mirror')
212
            ->with($locationCacheDir, $staleCacheDir);
213
        $fs
214
            ->expects($this->once())
215
            ->method('remove')
216
            ->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir));
217
218
        $this->store->purgeAllContent();
219
    }
220
221 View Code Duplication
    public function testPurgeAllContentByRequest()
222
    {
223
        $fs = $this->getFilesystemMock();
224
        $this->store->setFilesystem($fs);
225
        $locationCacheDir = $this->store->getLocationCacheDir();
226
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
227
228
        $fs
229
            ->expects($this->any())
230
            ->method('exists')
231
            ->with($locationCacheDir)
232
            ->will($this->returnValue(true));
233
        $fs
234
            ->expects($this->once())
235
            ->method('mkdir')
236
            ->with($staleCacheDir);
237
        $fs
238
            ->expects($this->once())
239
            ->method('mirror')
240
            ->with($locationCacheDir, $staleCacheDir);
241
        $fs
242
            ->expects($this->once())
243
            ->method('remove')
244
            ->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir));
245
246
        $request = Request::create('/', 'BAN');
247
        $request->headers->set('X-Location-Id', '.*');
248
        $this->store->purgeByRequest($request);
249
    }
250
251 View Code Duplication
    public function testPurgeAllContentByRequestBC()
252
    {
253
        $fs = $this->getFilesystemMock();
254
        $this->store->setFilesystem($fs);
255
        $locationCacheDir = $this->store->getLocationCacheDir();
256
        $staleCacheDir = str_replace(LocationAwareStore::LOCATION_CACHE_DIR, LocationAwareStore::LOCATION_STALE_CACHE_DIR, $locationCacheDir);
257
258
        $fs
259
            ->expects($this->any())
260
            ->method('exists')
261
            ->with($locationCacheDir)
262
            ->will($this->returnValue(true));
263
        $fs
264
            ->expects($this->once())
265
            ->method('mkdir')
266
            ->with($staleCacheDir);
267
        $fs
268
            ->expects($this->once())
269
            ->method('mirror')
270
            ->with($locationCacheDir, $staleCacheDir);
271
        $fs
272
            ->expects($this->once())
273
            ->method('remove')
274
            ->with(array($staleCacheDir, $this->store->getLocationCacheLockName(), $locationCacheDir));
275
276
        $request = Request::create('/', 'PURGE');
277
        $request->headers->set('X-Location-Id', '*');
278
        $this->store->purgeByRequest($request);
279
    }
280
}
281