Completed
Push — master ( 807d05...dfc103 )
by Chad
9s
created

InMemoryCacheTest::getExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SubjectivePHPTest\Psr\SimpleCache;
4
5
use DateTime;
6
use SubjectivePHP\Psr\SimpleCache\InMemoryCache;
7
8
/**
9
 * @coversDefaultClass \SubjectivePHP\Psr\SimpleCache\InMemoryCache
10
 * @covers ::<private>
11
 */
12
final class InMemoryCacheTest extends \PHPUnit\Framework\TestCase
13
{
14
    /**
15
     * @var InMemoryCache
16
     */
17
    private $cache;
18
19
    /**
20
     * @return void
21
     */
22
    public function setUp()
23
    {
24
        $this->cache = new InMemoryCache();
25
    }
26
27
    /**
28
     * @test
29
     * @covers ::get
30
     *
31
     * @return void
32
     */
33
    public function get()
34
    {
35
        $dateTime = new DateTime();
36
        $this->cache->set('foo', $dateTime);
37
        $this->assertEquals($dateTime, $this->cache->get('foo'));
38
    }
39
40
    /**
41
     * @test
42
     * @covers ::get
43
     *
44
     * @return void
45
     */
46
    public function getKeyNotFound()
47
    {
48
        $default = new \StdClass();
49
        $this->assertSame($default, $this->cache->get('foo', $default));
50
    }
51
52
    /**
53
     * @test
54
     * @covers ::get
55
     *
56
     * @return void
57
     */
58
    public function getExpired()
59
    {
60
        $this->cache->set('foo', new DateTime(), -10);
61
        $this->assertNull($this->cache->get('foo'));
62
    }
63
64
    /**
65
     * @test
66
     * @covers ::getMultiple
67
     *
68
     * @return void
69
     */
70
    public function getMultple()
71
    {
72
        $default = new \StdClass();
73
        $dateTime = new \DateTime();
74
        $exception = new \RuntimeException();
75
        $this->cache->set('foo', $dateTime);
76
        $this->cache->set('bar', $exception);
77
        $actual = $this->cache->getMultiple(['foo', 'baz', 'bar'], $default);
0 ignored issues
show
Documentation introduced by
array('foo', 'baz', 'bar') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a object<SubjectivePHP\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...
78
        $this->assertEquals($dateTime, $actual['foo']);
79
        $this->assertSame($default, $actual['baz']);
80
        $this->assertEquals($exception, $actual['bar']);
81
    }
82
83
    /**
84
     * @test
85
     * @covers ::set
86
     *
87
     * @return void
88
     */
89
    public function setWithIntegerTTL()
90
    {
91
        $dateTime = new \DateTime();
92
        $this->assertTrue($this->cache->set('foo', $dateTime, 3600));
93
        $this->assertEquals($dateTime, $this->cache->get('foo'));
94
    }
95
96
    /**
97
     * @test
98
     * @covers ::set
99
     *
100
     * @return void
101
     */
102
    public function setWithNullTTL()
103
    {
104
        $dateTime = new \DateTime();
105
        $this->assertTrue($this->cache->set('foo', $dateTime));
106
        $this->assertEquals($dateTime, $this->cache->get('foo'));
107
    }
108
109
    /**
110
     * @test
111
     * @covers ::setMultiple
112
     *
113
     * @return void
114
     */
115
    public function setMultple()
116
    {
117
        $ttl = \DateInterval::createFromDateString('1 day');
118
        $dateTime = new \DateTime();
119
        $exception = new \RuntimeException();
120
        $this->assertTrue($this->cache->setMultiple(['foo' => $dateTime, 'bar' => $exception], $ttl));
0 ignored issues
show
Documentation introduced by
array('foo' => $dateTime, 'bar' => $exception) is of type array<string,object<Date...ct<RuntimeException>"}>, but the function expects a object<SubjectivePHP\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...
121
        $this->assertEquals($dateTime, $this->cache->get('foo'));
122
        $this->assertEquals($exception, $this->cache->get('bar'));
123
    }
124
125
    /**
126
     * @test
127
     * @covers ::delete
128
     *
129
     * @return void
130
     */
131
    public function delete()
132
    {
133
        $dateTime = new DateTime();
134
        $this->cache->set('foo', $dateTime);
135
        $this->cache->delete('foo');
136
        $this->assertNull($this->cache->get('foo'));
137
    }
138
139
    /**
140
     * @test
141
     * @covers ::deleteMultiple
142
     *
143
     * @return void
144
     */
145
    public function deleteMultiple()
146
    {
147
        $this->cache->set('foo', 'foo');
148
        $this->cache->set('bar', 'bar');
149
        $this->cache->set('baz', 'baz');
150
151
        $this->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<SubjectivePHP\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...
152
        $this->assertNull($this->cache->get('foo'));
153
        $this->assertNull($this->cache->get('bar'));
154
        $this->assertSame('baz', $this->cache->get('baz'));
155
    }
156
157
    /**
158
     * @test
159
     * @covers ::clear
160
     *
161
     * @return void
162
     */
163
    public function clear()
164
    {
165
        $this->cache->set('foo', 'foo');
166
        $this->cache->set('bar', 'bar');
167
        $this->cache->set('baz', 'baz');
168
169
        $this->cache->clear();
170
        $this->assertNull($this->cache->get('foo'));
171
        $this->assertNull($this->cache->get('bar'));
172
        $this->assertNull($this->cache->get('baz'));
173
    }
174
175
    /**
176
     * @test
177
     * @covers ::has
178
     *
179
     * @return void
180
     */
181
    public function has()
182
    {
183
        $this->cache->set('foo', 'foo');
184
        $this->assertTrue($this->cache->has('foo'));
185
        $this->assertFalse($this->cache->has('bar'));
186
    }
187
}
188