Completed
Branch master (1afc22)
by Dan
09:51 queued 07:56
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
        $this->assertEquals($this->cacheStorage->has('baz'), false);
105
    }
106
107
    public function testCreateCacheItem(){
108
109
        $reflector = new \ReflectionClass(FileStorage::class);
110
        $method = $reflector->getMethod('_createCacheItem');
111
        $method->setAccessible(true);
112
113
        $key= 'my-key_+';
114
        $value = 'my-value';
115
        $ttl = 60 * 60 * 7;
116
117
        $expected = [
118
            'file' => $this->testDir . DIRECTORY_SEPARATOR . implode('.',[$key, FileStorage::EXTENSION]),
119
            'data' => json_encode([$ttl, $value])
120
        ];
121
122
        $actual = $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]);
123
        $this->assertEquals($expected, $actual);
124
    }
125
126
    public function testCreateCacheItemInvalidKey(){
127
128
        $this->expectException(InvalidArgumentException::class);
129
        $reflector = new \ReflectionClass(FileStorage::class);
130
        $method = $reflector->getMethod('_createCacheItem');
131
        $method->setAccessible(true);
132
        $key = '\jlasd$dll.';
133
        $value = 'my-value';
134
        $ttl = 60 * 60 * 7;
135
        $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]);
136
    }
137
138
    public function testFetchCacheNoItem(){
139
        $reflector = new \ReflectionClass(FileStorage::class);
140
        $method = $reflector->getMethod('_fetchCacheFile');
141
        $method->setAccessible(true);
142
        $key= 'unknown';
143
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
144
        $this->assertEquals(false, $actual);
145
    }
146
147
    public function testFetchCacheValid(){
148
149
        $key= 'my-item';
150
        $ttl = 60 * 60;
151
        $value = 'some-value';
152
153
        $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...
154
155
        $expected = [
156
            0 => time() + $ttl,
157
            1 => $value
158
        ];
159
160
        $reflector = new \ReflectionClass(FileStorage::class);
161
        $method = $reflector->getMethod('_fetchCacheFile');
162
        $method->setAccessible(true);
163
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
164
        $this->assertEquals($expected, $actual);
165
    }
166
167
    public function testFetchCacheExpired(){
168
        $key= 'my-item';
169
        $ttl = -1500;
170
        $value = 'some-value';
171
        $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...
172
        $reflector = new \ReflectionClass(FileStorage::class);
173
        $method = $reflector->getMethod('_fetchCacheFile');
174
        $method->setAccessible(true);
175
        $actual = $method->invokeArgs($this->cacheStorage, [$key]);
176
        $this->assertEquals(false, $actual);
177
    }
178
179
    /**
180
     * Clean up any cache files left on system.
181
     */
182
    public function tearDown()
183
    {
184
        $dir = $this->testDir;
185
186
        if (is_dir( $dir)){
187
            array_map('unlink', glob("$dir/*.*"));
188
        }
189
        \rmdir($dir);
190
    }
191
192
}
193