Passed
Pull Request — master (#12)
by Dan
03:21
created

CacheTest::testSetMultipleWithFailure()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 3
eloc 23
nc 3
nop 0
1
<?php
2
3
namespace Tests\Cache;
4
5
use Ds\Cache\Cache;
6
use Ds\Cache\CacheStorageInterface;
7
use Ds\Cache\NullStorage;
8
use Psr\SimpleCache\InvalidArgumentException;
9
10
/**
11
 * Class CacheTest
12
 * @package Tests\Cache
13
 */
14
class CacheTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var Cache
18
     */
19
    public $cache;
20
21
    /**
22
     * @var CacheStorageInterface
23
     */
24
    public $storageMock;
25
26
    /**
27
     *
28
     */
29
    public function setUp()
30
    {
31
        $this->storageMock = $this->getMockBuilder(CacheStorageInterface::class)->getMock();
32
        $this->cache = new Cache($this->storageMock);
33
    }
34
35
    /**
36
     * Test that CacheStorageInterface::set() invalid key exception
37
     */
38
    public function testSetInvalidKey()
39
    {
40
        $key = 21021000;
41
        $this->expectException(InvalidArgumentException::class);
42
        $this->cache->set($key,'value');
43
    }
44
45
    /**
46
     * Test that CacheStorageInterface::set() is called
47
     */
48
    public function testSet()
49
    {
50
        $key = 'key';
51
        $value = 'value';
52
        $expires = 10;
53
54
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            ->method('set')
56
            ->with(
57
                $this->equalTo($key),
58
                $this->equalTo($value),
59
                $this->equalTo($expires)
60
            );
61
        $this->cache->set($key, $value, $expires);
62
    }
63
64
    /**
65
     * Test that CacheStorageInterface::set() is called
66
     */
67
    public function testSetDateInterval()
68
    {
69
        $key = 'key';
70
        $value = 'value';
71
        $expires = new \DateInterval('PT1H');
72
        $oneHour = 60 * 60 * 1;
73
74
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            ->method('set')
76
            ->with(
77
                $this->equalTo($key),
78
                $this->equalTo($value),
79
                $this->equalTo($oneHour)
80
            );
81
82
        $this->cache->set($key, $value, $expires);
83
    }
84
85
    /**
86
     * Test that CacheStorageInterface::has() invalid key exception
87
     */
88
    public function testHasInvalidKey()
89
    {
90
        $key = 21021000;
91
        $this->expectException(InvalidArgumentException::class);
92
        $this->cache->has($key);
93
    }
94
95
96
    /**
97
     * Test that CacheStorageInterface::has() is called and returns true on finding key
98
     */
99 View Code Duplication
    public function testHasFoundWithKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $key = 'key';
102
        $expected = true;
103
104
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
            ->method('has')
106
            ->with(
107
                $this->equalTo($key)
108
            )
109
            ->willReturn($expected);
110
111
        $actual = $this->cache->has($key);
112
        $this->assertEquals($expected, $actual);
113
    }
114
115
    /**
116
     * Test that CacheStorageInterface::has() is called and returns false on failing to find key
117
     */
118 View Code Duplication
    public function testHasFoundNoKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $key = 'key';
121
        $expected = false;
122
123
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
            ->method('has')
125
            ->with(
126
                $this->equalTo($key)
127
            )
128
            ->willReturn($expected);
129
130
        $actual = $this->cache->has($key);
131
        $this->assertEquals($expected, $actual);
132
    }
133
134
135
    /**
136
     * Test that CacheStorageInterface::has() is called and returns false on failing to find key
137
     */
138
    public function testGetInvalidKey()
139
    {
140
        $key = 21021000;
141
        $this->expectException(InvalidArgumentException::class);
142
        $this->cache->get($key);
143
    }
144
145
    /**
146
     * Test that CacheStorageInterface::get() returns values when key is present.
147
     */
148
    public function testGetFoundValue()
149
    {
150
        $key = 'key';
151
        $expected = 'cacheValue';
152
        $default = 'default value';
153
154
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
            ->method('has')
156
            ->with(
157
                $this->equalTo($key)
158
            )
159
            ->willReturn(true);
160
161
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
            ->method('get')
163
            ->with(
164
                $this->equalTo($key)
165
            )
166
            ->willReturn($expected);
167
168
        $actual = $this->cache->get($key, $default);
169
        $this->assertEquals($expected, $actual);
170
    }
171
172
    /**
173
     * Test that CacheStorageInterface::get() doesn't return values and default is returned.
174
     */
175
    public function testGetDefaultValue()
176
    {
177
        $key = 'key';
178
        $default = 'default value';
179
180
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
            ->method('has')
182
            ->with(
183
                $this->equalTo($key)
184
            )
185
            ->willReturn(false);
186
187
        $actual = $this->cache->get($key, $default);
188
        $this->assertEquals($default, $actual);
189
    }
190
191
    /**
192
     * Test that CacheStorageInterface::delete() is called
193
     */
194
    public function testDelete()
195
    {
196
        $key = 'key';
197
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
            ->method('delete')
199
            ->with(
200
                $this->equalTo($key)
201
            );
202
        $this->cache->delete($key);
203
    }
204
205
    /**
206
     *
207
     */
208
    public function testGetMultiple(){
209
210
        $expected = [
211
            'foo' => 'fooValue',
212
            'bar' => 'barValue',
213
            'baz' => 'bazValue'
214
        ];
215
216
        $i = 0;
217
218
        foreach ($expected as $key => $value){
219
220
            $this->storageMock->expects($this->at($i))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
                ->method('get')
222
                ->with(
223
                    $this->equalTo($key)
224
                )
225
                ->willReturn($value);
226
            $i++;
227
        }
228
229
        $actual = $this->cache->getMultiple(\array_keys($expected));
0 ignored issues
show
Documentation introduced by
\array_keys($expected) is of type array<integer,string>, but the function expects a object<iterable>.

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...
230
231
        $this->assertEquals($expected, $actual);
232
233
    }
234
235
    /**
236
     *
237
     */
238
    public function testSetMultiple(){
239
240
        $keys = [
241
            'foo' => 'fooValue',
242
            'bar' => 'barValue',
243
            'baz' => 'bazValue'
244
        ];
245
246
        $addStatus = true;
247
        $expected = true;
248
249
        $i = 0;
250
        $expires = 60 * 60;
251
252
        foreach ($keys as $key => $value){
253
            $this->storageMock->expects($this->at($i))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
254
                ->method('set')
255
                ->with(
256
                    $this->equalTo($key),
257
                    $this->equalTo($value),
258
                    $this->equalTo($expires)
259
                )
260
                ->willReturn($addStatus);
261
            $i++;
262
        }
263
264
        $actual = $this->cache->setMultiple($keys,$expires);
0 ignored issues
show
Documentation introduced by
$keys is of type array<string,string,{"fo...tring","baz":"string"}>, but the function expects a object<iterable>.

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...
265
        $this->assertEquals($expected, $actual);
266
    }
267
268
    /**
269
     *
270
     */
271
    public function testSetMultipleWithFailure(){
272
273
        $keys = [
274
            'foo' => 'fooValue',
275
            'bar' => 'barValue',
276
            'baz' => 'bazValue'
277
        ];
278
279
        $addStatus = true;
280
        $expected = false;
281
282
        $i = 0;
283
        $expires = 60 * 60;
284
285
        foreach ($keys as $key => $value){
286
287
            $status = $addStatus;
288
289
            if ($i === 1){
290
                $status = false;
291
            }
292
293
            $this->storageMock->expects($this->at($i))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
294
                ->method('set')
295
                ->with(
296
                    $this->equalTo($key),
297
                    $this->equalTo($value),
298
                    $this->equalTo($expires)
299
                )
300
                ->willReturn($status);
301
            $i++;
302
        }
303
304
        $actual = $this->cache->setMultiple($keys,$expires);
0 ignored issues
show
Documentation introduced by
$keys is of type array<string,string,{"fo...tring","baz":"string"}>, but the function expects a object<iterable>.

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...
305
        $this->assertEquals($expected, $actual);
306
    }
307
308
    /**
309
     *
310
     */
311
    public function testDeleteMultiple(){
312
313
        $keys = ['foo','bar','baz'];
314
        $deleteStatus = true;
315
        $expected = true;
316
317
        foreach ($keys as $i => $key){
318
            $this->storageMock->expects($this->at($i))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
319
                ->method('delete')
320
                ->with(
321
                    $this->equalTo($key)
322
                )
323
                ->willReturn($deleteStatus);
324
        }
325
326
        $actual = $this->cache->deleteMultiple($keys);
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a object<iterable>.

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...
327
        $this->assertEquals($expected, $actual);
328
    }
329
330
    /**
331
     *
332
     */
333
    public function testDeleteMultipleFailure(){
334
335
        $keys = ['foo','bar','baz'];
336
        $expected = false;
337
        $deleteStatus = true;
338
339
        foreach ($keys as $i => $key){
340
341
            if ($i === 1){
342
                $deleteStatus = false;
343
            }
344
345
            $this->storageMock->expects($this->at($i))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
346
                ->method('delete')
347
                ->with(
348
                    $this->equalTo($key)
349
                )
350
                ->willReturn($deleteStatus);
351
        }
352
353
        $actual = $this->cache->deleteMultiple($keys);
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a object<iterable>.

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...
354
        $this->assertEquals($expected, $actual);
355
    }
356
357
    /**
358
     * Test that storage clear is called.
359
     */
360
    public function testclear(){
361
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
362
            ->method('clear');
363
        $this->cache->clear();
364
    }
365
366
    /**
367
     * Test that array has keys.
368
     */
369 View Code Duplication
    public function testArrayHasKeys(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
        $data = ['a' => 'foo', 'b' => 'bar'];
371
372
        $reflector = new \ReflectionClass(Cache::class);
373
        $method = $reflector->getMethod('_hasKeys');
374
        $method->setAccessible(true);
375
        $actual = $method->invokeArgs($this->cache, [$data]);
376
377
        $this->assertEquals(true, $actual);
378
    }
379
380
    /**
381
     * Test exception thrown when array is missing keys.
382
     */
383 View Code Duplication
    public function testArrayHasNoKeys(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
384
        $this->expectException(InvalidArgumentException::class);
385
        $data = ['foo','bar'];
386
        $reflector = new \ReflectionClass(Cache::class);
387
        $method = $reflector->getMethod('_hasKeys');
388
        $method->setAccessible(true);
389
        $method->invokeArgs($this->cache, [$data]);
390
    }
391
392
    /**
393
     * Test array contains a failure.
394
     */
395 View Code Duplication
    public function testHasFailure(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
396
        $results = [true,true,true,false,true];
397
        $reflector = new \ReflectionClass(Cache::class);
398
        $method = $reflector->getMethod('_hasFailure');
399
        $method->setAccessible(true);
400
        $actual = $method->invokeArgs($this->cache, [$results]);
401
        $expected = true;
402
        $this->assertEquals($expected, $actual);
403
    }
404
405
    /**
406
     *
407
     */
408 View Code Duplication
    public function testHasNoFailure(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
409
        $results = [true,true,true,true,true];
410
        $reflector = new \ReflectionClass(Cache::class);
411
        $method = $reflector->getMethod('_hasFailure');
412
        $method->setAccessible(true);
413
        $actual = $method->invokeArgs($this->cache, [$results]);
414
        $expected = false;
415
        $this->assertEquals($expected, $actual);
416
    }
417
418
    /**
419
     * Test that array is Traversable
420
     */
421 View Code Duplication
    public function testIsTraversable(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
422
        $data = ['a','b','c','d'];
423
        $reflector = new \ReflectionClass(Cache::class);
424
        $method = $reflector->getMethod('_isTraversable');
425
        $method->setAccessible(true);
426
        $actual = $method->invokeArgs($this->cache, [$data]);
427
        $expected = true;
428
        $this->assertEquals($expected, $actual);
429
    }
430
431
    /**
432
     * Test that string is not Traversable
433
     */
434 View Code Duplication
    public function testIsNotTraversable(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
435
        $this->expectException(InvalidArgumentException::class);
436
        $data = 'some-random-string';
437
        $reflector = new \ReflectionClass(Cache::class);
438
        $method = $reflector->getMethod('_isTraversable');
439
        $method->setAccessible(true);
440
        $method->invokeArgs($this->cache, [$data]);
441
    }
442
443
    /**
444
     * Test that instance of Iterator is traversable
445
     */
446
    public function testIsTraversableIterator(){
447
        $iterator = new class implements \Iterator {
448
            public function current(){}
449
            public function next(){}
450
            public function key(){}
451
            public function valid(){}
452
            public function rewind(){}
453
        };
454
        $reflector = new \ReflectionClass(Cache::class);
455
        $method = $reflector->getMethod('_isTraversable');
456
        $method->setAccessible(true);
457
        $actual = $method->invokeArgs($this->cache, [$iterator]);
458
        $expected = true;
459
        $this->assertEquals($expected, $actual);
460
    }
461
}
462