GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FileCacheTest::setUp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace JonnyW\PhantomJs\Tests\Unit\Cache;
10
11
use JonnyW\PhantomJs\Cache\FileCache;
12
13
/**
14
 * PHP PhantomJs
15
 *
16
 * @author Jon Wenmoth <[email protected]>
17
 */
18
class FileCacheTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * Test filename
22
     *
23
     * @var string
24
     * @access protected
25
     */
26
    protected $filename;
27
28
    /**
29
     * Test directory
30
     *
31
     * @var string
32
     * @access protected
33
     */
34
    protected $directory;
35
36
/** +++++++++++++++++++++++++++++++++++ **/
37
/** ++++++++++++++ TESTS ++++++++++++++ **/
38
/** +++++++++++++++++++++++++++++++++++ **/
39
40
    /**
41
     * Test false is returned if file
42
     * does not exist.
43
     *
44
     * @access public
45
     * @return void
46
     */
47
    public function testFalseIsReturnedIfFileDoesNotExist()
48
    {
49
        $fileCache = $this->getFileCache($this->directory, 'txt');
50
51
        $this->assertFalse($fileCache->exists($this->filename, 'Test'));
0 ignored issues
show
Unused Code introduced by
The call to FileCache::exists() has too many arguments starting with 'Test'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
    }
53
54
    /**
55
     * Test true is returned if file does exist.
56
     *
57
     * @access public
58
     * @return void
59
     */
60
    public function testTrueIsReturnedIfFileDoesExist()
61
    {
62
        touch($this->getFilename());
63
64
        $fileCache = $this->getFileCache($this->directory, 'txt');
65
66
        $this->assertTrue($fileCache->exists($this->filename));
67
    }
68
69
    /**
70
     * Test not writable exception is thrown if file
71
     * cannot be saved due to write permissions.
72
     *
73
     * @return void
74
     */
75
    public function testNotWritableExceptionIsThrownIfFileCannotBeSavedDueToWritePermissions()
76
    {
77
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
78
79
        $fileCache = $this->getFileCache('/This/Directory/Is/Not/Writable/', 'txt');
80
        $fileCache->save($this->filename, 'Test');
81
    }
82
83
    /**
84
     * Test test file location is returned
85
     * if file is successfully saved.
86
     *
87
     * @access public
88
     * @return void
89
     */
90
    public function testFileLocationIsReturnedIfFileIsSuccessfullySaved()
91
    {
92
        $fileCache  = $this->getFileCache($this->directory, 'txt');
93
        $file       = $fileCache->save($this->filename, 'Test');
94
95
        $this->assertInternalType('string', $file);
96
        $this->assertFileExists($file);
97
    }
98
99
    /**
100
     * Test file can be saved
101
     * with directory path
102
     *
103
     * @access public
104
     * @return void
105
     */
106
    public function testFileCanBeSavedWithDirectoryPath()
107
    {
108
        $fileCache  = $this->getFileCache('', 'txt');
109
        $file       = $fileCache->save($this->directory, 'Test');
110
111
        $this->assertSame(dirname($file), $this->directory);
112
113
        unlink($file);
114
    }
115
116
    /**
117
     * Test not writable exception is thrown
118
     * if directory path is not writable.
119
     *
120
     * @access public
121
     * @return void
122
     */
123
    public function testNotWritableExceptionIsThrownIfDirectoryPathIsNotWritable()
124
    {
125
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
126
127
        $fileCache  = $this->getFileCache($this->directory, 'txt');
128
        $file       = $fileCache->save('/This/Directory/Is/Not/Writable/', 'Test');
0 ignored issues
show
Unused Code introduced by
$file 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...
129
    }
130
131
    /**
132
     * Test file can be saved with absolute
133
     * path.
134
     *
135
     * @access public
136
     * @return void
137
     */
138
    public function testFileCanBeSavedWithAbsolutePath()
139
    {
140
        $test = sprintf('%1$s/%2$s', $this->directory, 'new-file.txt');
141
142
        $fileCache = $this->getFileCache('', 'txt');
143
        $file      = $fileCache->save($test, 'Test');
144
145
        $this->assertSame($test, $file);
146
147
        unlink($file);
148
    }
149
150
    /**
151
     * Test not exists exception is thrown when
152
     * fetching data that doesn't exsit
153
     *
154
     * @access public
155
     * @return void
156
     */
157
    public function testNotExistsExceptionIsThrownIfWhenFetchingDataThatDoesntExist()
158
    {
159
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotExistsException');
160
161
        $fileCache = $this->getFileCache('', 'txt');
162
163
        $this->assertFalse($fileCache->fetch($this->filename));
164
    }
165
166
    /**
167
     * Test data can be fetched from cache.
168
     *
169
     * @access public
170
     * @return void
171
     */
172
    public function testDataCanBeFetchedFromCache()
173
    {
174
        $test = 'Test';
175
176
        $fileCache = $this->getFileCache($this->directory, 'txt');
177
        $fileCache->save($this->filename, $test);
178
179
        $content = $fileCache->fetch($this->filename);
180
181
        $this->assertSame($test, $content);
182
    }
183
184
    /**
185
     * Test data can be deleted from cache.
186
     *
187
     * @access public
188
     * @return void
189
     */
190
    public function testDataCanBeDeletedFromCache()
191
    {
192
        $fileCache = $this->getFileCache($this->directory, 'txt');
193
194
        $file = $fileCache->save($this->filename, 'Test');
195
196
        $fileCache->delete($this->filename);
197
198
        $this->assertFileNotExists($file);
199
    }
200
201
    /**
202
     * Test data can be deleted from
203
     * cache using wildcard.
204
     *.
205
     *
206
     * @access public
207
     * @return void
208
     */
209
    public function testDataCanBeDeletedFromCacheUsingWildcard()
210
    {
211
        $fileCache = $this->getFileCache($this->directory, 'txt');
212
213
        $file1 = $fileCache->save('test_file_1', 'Test1');
214
        $file2 = $fileCache->save('test_file_2', 'Test2');
215
216
        $fileCache->delete('test_file_*');
217
218
        $this->assertFileNotExists($file1);
219
        $this->assertFileNotExists($file2);
220
    }
221
222
/** +++++++++++++++++++++++++++++++++++ **/
223
/** ++++++++++ TEST ENTITIES ++++++++++ **/
224
/** +++++++++++++++++++++++++++++++++++ **/
225
226
    /**
227
     * Get file write instance
228
     *
229
     * @param  string                            $directory
230
     * @param  string                            $extension
231
     * @return \JonnyW\PhantomJs\Cache\FileCache
232
     */
233
    protected function getFileCache($directory, $extension)
234
    {
235
        $fileCache = new FileCache($directory, $extension);
236
237
        return $fileCache;
238
    }
239
240
/** +++++++++++++++++++++++++++++++++++ **/
241
/** ++++++++++++ UTILITIES ++++++++++++ **/
242
/** +++++++++++++++++++++++++++++++++++ **/
243
244
    /**
245
     * Set up test environment.
246
     *
247
     * @access public
248
     * @return void
249
     */
250
    public function setUp()
251
    {
252
        $this->filename  = 'test.txt';
253
        $this->directory = sys_get_temp_dir();
254
255
        if (!is_writable($this->directory)) {
256
            throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
257
        }
258
    }
259
260
    /**
261
     * Tear down test environment.
262
     *
263
     * @access public
264
     * @return void
265
     */
266
    public function tearDown()
267
    {
268
        $filename = $this->getFilename();
269
270
        if (file_exists($filename)) {
271
            unlink($filename);
272
        }
273
    }
274
275
    /**
276
     * Get test filename.
277
     *
278
     * @access public
279
     * @return string
280
     */
281
    public function getFilename()
282
    {
283
        return sprintf('%1$s/%2$s', $this->directory, $this->filename);
284
    }
285
}
286