Completed
Push — master ( 95d836...63ebe6 )
by BENOIT
09:05
created

ArrayCacheTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace BenTools\Currency\Tests\Cache;
4
5
use BenTools\Currency\Cache\ArrayCache;
6
use PHPUnit\Framework\TestCase;
7
8
class ArrayCacheTest extends TestCase
9
{
10
11
    public function testGet()
12
    {
13
        $cache = new ArrayCache();
14
        $cache->set('foo', 'bar');
15
        $this->assertEquals('bar', $cache->get('foo'));
16
        $this->assertEquals(null, $cache->get('baz'));
17
    }
18
19
    public function testDelete()
20
    {
21
        $cache = new ArrayCache();
22
        $cache->set('foo', 'bar');
23
        $cache->delete('foo');
24
        $this->assertEquals(null, $cache->get('foo'));
25
    }
26
27
    public function testClear()
28
    {
29
        $cache = new ArrayCache();
30
        $cache->set('foo', 'bar');
31
        $cache->clear();
32
        $this->assertEquals(null, $cache->get('foo'));
33
    }
34
35
    public function testGetMultiple()
36
    {
37
        $cache = new ArrayCache();
38
        $values = [
39
            'foo' => 'bar',
40
            'baz' => 'bat'
41
        ];
42
        $cache->setMultiple($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string,{"fo...tring","baz":"string"}>, but the function expects a object<Psr\SimpleCache\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...
43
        $this->assertEquals($values, iterable_to_array($cache->getMultiple(array_keys($values))));
0 ignored issues
show
Documentation introduced by
array_keys($values) is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\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...
44
    }
45
46
    public function testDeleteMultiple()
47
    {
48
        $cache = new ArrayCache();
49
        $values = [
50
            'foo' => 'bar',
51
            'baz' => 'bat',
52
            'bar' => 'foo',
53
        ];
54
        $cache->setMultiple($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string,{"fo...tring","bar":"string"}>, but the function expects a object<Psr\SimpleCache\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...
55
        $cache->deleteMultiple(['foo', 'bar']);
0 ignored issues
show
Documentation introduced by
array('foo', 'bar') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Psr\SimpleCache\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...
56
        $this->assertEquals([
57
            'foo' => null,
58
            'baz' => 'bat',
59
            'bar' => null,
60
        ], iterable_to_array($cache->getMultiple(array_keys($values))));
0 ignored issues
show
Documentation introduced by
array_keys($values) is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\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...
61
    }
62
63
    public function testHas()
64
    {
65
        $cache = new ArrayCache();
66
        $cache->set('foo', 'bar');
67
        $this->assertTrue($cache->has('foo'));
68
        $this->assertFalse($cache->has('baz'));
69
    }
70
71
    public function testDefaultTtl()
72
    {
73
        $cache = new ArrayCache(1);
74
        $cache->set('foo', 'bar');
75
        $this->assertTrue($cache->has('foo'));
76
        $this->assertEquals('bar', $cache->get('foo'));
77
        sleep(1);
78
        $this->assertFalse($cache->has('foo'));
79
        $this->assertNull($cache->get('foo'));
80
    }
81
82
    public function testTtlOverride()
83
    {
84
        $cache = new ArrayCache(1);
85
        $cache->set('foo', 'bar', 2);
86
        $this->assertTrue($cache->has('foo'));
87
        $this->assertEquals('bar', $cache->get('foo'));
88
        sleep(1);
89
        $this->assertTrue($cache->has('foo'));
90
        $this->assertEquals('bar', $cache->get('foo'));
91
        sleep(1);
92
        $this->assertFalse($cache->has('foo'));
93
        $this->assertNull($cache->get('foo'));
94
    }
95
}
96