RedisCacheTest::setMultple()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
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\RedisCache;
7
use Predis\Client;
8
9
/**
10
 * @coversDefaultClass \SubjectivePHP\Psr\SimpleCache\RedisCache
11
 * @covers ::__construct
12
 * @covers ::<private>
13
 */
14
final class RedisCacheTest extends \PHPUnit\Framework\TestCase
15
{
16
    /**
17
     * @var Client
18
     */
19
    private $predis;
20
21
    /**
22
     * @var RedisCache
23
     */
24
    private $cache;
25
26
    /**
27
     * @return void
28
     */
29
    public function setUp()
30
    {
31
        $this->predis = new Client();
32
        $this->predis->flushall();
33
        $this->cache = new RedisCache($this->predis);
34
    }
35
36
    /**
37
     * @test
38
     * @covers ::get
39
     *
40
     * @return void
41
     */
42
    public function get()
43
    {
44
        $dateTime = new DateTime();
45
        $this->predis->set('foo', serialize($dateTime));
46
        $this->assertEquals($dateTime, $this->cache->get('foo'));
47
    }
48
49
    /**
50
     * @test
51
     * @covers ::get
52
     *
53
     * @return void
54
     */
55
    public function getKeyNotFound()
56
    {
57
        $default = new \StdClass();
58
        $this->assertSame($default, $this->cache->get('foo', $default));
59
    }
60
61
    /**
62
     * @test
63
     * @covers ::getMultiple
64
     *
65
     * @return void
66
     */
67
    public function getMultple()
68
    {
69
        $default = new \StdClass();
70
        $dateTime = new \DateTime();
71
        $exception = new \RuntimeException();
72
        $this->predis->set('foo', serialize($dateTime));
73
        $this->predis->set('bar', serialize($exception));
74
        $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...
75
        $this->assertEquals($dateTime, $actual['foo']);
76
        $this->assertSame($default, $actual['baz']);
77
        $this->assertEquals($exception, $actual['bar']);
78
    }
79
80
    /**
81
     * @test
82
     * @covers ::set
83
     *
84
     * @return void
85
     */
86
    public function set()
87
    {
88
        $dateTime = new \DateTime();
89
        $this->assertTrue($this->cache->set('foo', $dateTime, 3600));
90
        $ttl = $this->predis->ttl('foo');
91
        $this->assertSame(serialize($dateTime), $this->predis->get('foo'));
92
        $this->assertGreaterThan(3598, $ttl);
93
        $this->assertLessThanOrEqual(3600, $ttl);
94
    }
95
96
    /**
97
     * @test
98
     * @covers ::set
99
     *
100
     * @return void
101
     */
102
    public function setFails()
103
    {
104
        $mockPredis = $this->getMockBuilder('\\Predis\\ClientInterface')->getMock();
105
        $mockPredis->expects($this->once())->method('__call')->willReturn(new \Predis\Response\Status('Not OK'));
106
        $cache = new RedisCache($mockPredis);
0 ignored issues
show
Documentation introduced by
$mockPredis is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Predis\ClientInterface>.

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...
107
        $this->assertFalse($cache->set('foo', new \DateTime(), 3600));
108
    }
109
110
    /**
111
     * @test
112
     * @covers ::setMultiple
113
     *
114
     * @return void
115
     */
116
    public function setMultple()
117
    {
118
        $dateTime = new \DateTime();
119
        $exception = new \RuntimeException();
120
        $this->assertTrue($this->cache->setMultiple(['foo' => $dateTime, 'bar' => $exception]));
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->assertSame(serialize($dateTime), $this->predis->get('foo'));
122
        $this->assertSame(serialize($exception), $this->predis->get('bar'));
123
    }
124
125
    /**
126
     * @test
127
     * @covers ::setMultiple
128
     *
129
     * @return void
130
     */
131
    public function setMultpleFails()
132
    {
133
        $passStatus = new \Predis\Response\Status('OK');
134
        $failStatus = new \Predis\Response\Status('Not OK');
135
136
        $mockPredis = $this->getMockBuilder('\\Predis\\ClientInterface')->getMock();
137
        $mockPredis->expects($this->exactly(3))->method('__call')->withConsecutive(
138
            [$this->equalTo('set'), $this->anything()],
139
            [$this->equalTo('expireat'), $this->anything()],
140
            [$this->equalTo('set'), $this->anything()]
141
        )->will($this->onConsecutiveCalls($passStatus, true, $failStatus));
142
        $cache = new RedisCache($mockPredis);
0 ignored issues
show
Documentation introduced by
$mockPredis is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Predis\ClientInterface>.

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...
143
        $this->assertFalse(
144
            $cache->setMultiple(
145
                ['foo' => new \DateTime(), 'bar' => new \DateTime()],
0 ignored issues
show
Documentation introduced by
array('foo' => new \Date...ar' => new \DateTime()) is of type array<string,object<Date...r":"object<DateTime>"}>, 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...
146
                \DateInterval::createFromDateString('1 day')
147
            )
148
        );
149
    }
150
151
    /**
152
     * @test
153
     * @covers ::delete
154
     *
155
     * @return void
156
     */
157
    public function delete()
158
    {
159
        $dateTime = new DateTime();
160
        $this->predis->set('foo', serialize($dateTime));
161
        $this->cache->delete('foo');
162
        $this->assertSame(0, $this->predis->exists('foo'));
163
    }
164
165
    /**
166
     * @test
167
     * @covers ::deleteMultiple
168
     *
169
     * @return void
170
     */
171 View Code Duplication
    public function deleteMultiple()
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...
172
    {
173
        $this->predis->set('foo', 'foo');
174
        $this->predis->set('bar', 'bar');
175
        $this->predis->set('baz', 'baz');
176
177
        $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...
178
        $this->assertSame(0, $this->predis->exists('foo'));
179
        $this->assertSame(0, $this->predis->exists('bar'));
180
        $this->assertSame(1, $this->predis->exists('baz'));
181
    }
182
183
    /**
184
     * @test
185
     * @covers ::clear
186
     *
187
     * @return void
188
     */
189 View Code Duplication
    public function clear()
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...
190
    {
191
        $this->predis->set('foo', 'foo');
192
        $this->predis->set('bar', 'bar');
193
        $this->predis->set('baz', 'baz');
194
195
        $this->cache->clear();
196
        $this->assertSame(0, $this->predis->exists('foo'));
197
        $this->assertSame(0, $this->predis->exists('bar'));
198
        $this->assertSame(0, $this->predis->exists('baz'));
199
    }
200
201
    /**
202
     * @test
203
     * @covers ::has
204
     *
205
     * @return void
206
     */
207
    public function has()
208
    {
209
        $this->predis->set('foo', 'foo');
210
        $this->assertTrue($this->cache->has('foo'));
211
        $this->assertFalse($this->cache->has('bar'));
212
    }
213
}
214