Completed
Push — master ( 4c07ef...756562 )
by Dan
06:35 queued 03:19
created

FileStorageTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Tests\Cache\Storage;
4
5
use Ds\Cache\CacheException;
6
use Ds\Cache\InvalidArgumentException;
7
use Ds\Cache\Storage\FileStorage;
8
9
/**
10
 * Class FileStorageTest
11
 *
12
 * @package Tests\Cache\Storage
13
 */
14
class FileStorageTest extends \PHPUnit_Framework_TestCase
15
{
16
17
    /**
18
     * @var FileStorage
19
     */
20
    public $cacheStorage;
21
22
    /**
23
     * @var
24
     */
25
    public $testDir;
26
27
    /**
28
     *
29
     */
30
    public function setUp()
31
    {
32
        $this->testDir = __DIR__ . '/cacheTest';
33
        $this->cacheStorage = new FileStorage( $this->testDir, new \DateInterval('P1M'));
34
    }
35
36
    /**
37
     * Test that InvalidArgumentException is thrown when not using a valid string
38
     */
39
    public function testNonStringDirectory()
40
    {
41
        $this->expectException(InvalidArgumentException::class);
42
        $this->cacheStorage = new FileStorage(false, new \DateInterval('P1M'));
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44
45
    /**
46
     * Test that CacheException is thrown if directory is not valid/writable.
47
     */
48
    public function testUnwritableDirectory(){
49
50
        $this->expectException(CacheException::class);
51
        $this->cacheStorage = new FileStorage('php://memory', new \DateInterval('P1M'));
52
    }
53
54
    /**
55
     * Test that set is called.
56
     */
57
    public function testSet(){
58
        $actual = $this->cacheStorage->set('key','value',60*60);
0 ignored issues
show
Documentation introduced by
'key' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
60 * 60 is of type integer, but the function expects a null|object<int>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $this->assertEquals(true, $actual);
60
    }
61
62
    /**
63
     * Test has when not value is found.
64
     */
65
    public function testHasNoValue(){
66
        $this->assertEquals($this->cacheStorage->has('someRandomKey'), false);
67
    }
68
69
    /**
70
     * Test has when value is found.
71
     */
72
    public function testHas(){
73
        $this->cacheStorage->set('foo','bar');
0 ignored issues
show
Documentation introduced by
'foo' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        $this->assertEquals($this->cacheStorage->has('foo'), true);
75
    }
76
77
    /**
78
     * Test has when value is found but has expired.
79
     */
80
    public function testHasExpired(){
81
        $this->cacheStorage->set('expired','bar', -1200);
0 ignored issues
show
Documentation introduced by
'expired' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
-1200 is of type integer, but the function expects a null|object<int>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $this->assertEquals($this->cacheStorage->has('expired'), false);
83
    }
84
85
    /**
86
     * Test that clear is called.
87
     */
88
    public function testClear(){
89
       $this->assertEquals($this->cacheStorage->clear(), true);
90
    }
91
92
    /**
93
     * Test that get returns a found key.
94
     */
95
    public function testGet(){
96
        $expected = 'bat';
97
        $this->cacheStorage->set('baz',$expected);
0 ignored issues
show
Documentation introduced by
'baz' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
        $actual = $this->cacheStorage->get('baz');
99
        $this->assertEquals($expected, $actual);
100
    }
101
102
    public function testDelete(){
103
        $this->cacheStorage->delete('baz');
0 ignored issues
show
Unused Code introduced by
The call to the method Ds\Cache\Storage\FileStorage::delete() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
104
    }
105
106
    public function testCreateCacheItem(){
107
108
        $reflector = new \ReflectionClass(FileStorage::class);
109
        $method = $reflector->getMethod('_createCacheItem');
110
        $method->setAccessible(true);
111
112
        $key= 'my-key_+';
113
        $value = 'my-value';
114
        $ttl = 60 * 60 * 7;
115
116
        $expected = [
117
            'file' => $this->testDir . DIRECTORY_SEPARATOR . implode('.',[$key, FileStorage::EXTENSION]),
118
            'data' => json_encode([$ttl, $value])
119
        ];
120
121
        $actual = $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]);
122
        $this->assertEquals($expected, $actual);
123
    }
124
125
    public function testCreateCacheItemInvalidKey(){
126
127
        $this->expectException(InvalidArgumentException::class);
128
        $reflector = new \ReflectionClass(FileStorage::class);
129
        $method = $reflector->getMethod('_createCacheItem');
130
        $method->setAccessible(true);
131
        $key = '\jlasd$dll.';
132
        $value = 'my-value';
133
        $ttl = 60 * 60 * 7;
134
        $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]);
135
    }
136
137
    public function testFetchCacheNoItem(){
138
        $reflector = new \ReflectionClass(FileStorage::class);
139
        $method = $reflector->getMethod('_fetchCacheFile');
140
        $method->setAccessible(true);
141
        $key= 'unknown';
142
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
143
        $this->assertEquals(false, $actual);
144
    }
145
146
    public function testFetchCacheValid(){
147
148
        $key= 'my-item';
149
        $ttl = 60 * 60;
150
        $value = 'some-value';
151
152
        $this->cacheStorage->set($key, $value, $ttl);
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$ttl is of type integer, but the function expects a null|object<int>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
154
        $expected = [
155
            0 => time() + $ttl,
156
            1 => $value
157
        ];
158
159
        $reflector = new \ReflectionClass(FileStorage::class);
160
        $method = $reflector->getMethod('_fetchCacheFile');
161
        $method->setAccessible(true);
162
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
163
        $this->assertEquals($expected, $actual);
164
    }
165
166
    public function testFetchCacheExpired(){
167
        $key= 'my-item';
168
        $ttl = -1500;
169
        $value = 'some-value';
170
        $this->cacheStorage->set($key, $value, $ttl);
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$ttl is of type integer, but the function expects a null|object<int>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
171
        $reflector = new \ReflectionClass(FileStorage::class);
172
        $method = $reflector->getMethod('_fetchCacheFile');
173
        $method->setAccessible(true);
174
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
175
        $this->assertEquals(false, $actual);
176
    }
177
178
    /**
179
     * Clean up any cache files left on system.
180
     */
181
    public function tearDown()
182
    {
183
        $dir = $this->testDir;
184
185
        if (is_dir( $dir)){
186
            array_map('unlink', glob("$dir/*.*"));
187
        }
188
        \rmdir($dir);
189
    }
190
191
}
192