Completed
Push — master ( c76232...ce4950 )
by Matthew
27:26 queued 01:28
created

PredisPhpRedisTest::lRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 1
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Redis;
4
5
use Dtc\QueueBundle\Redis\RedisInterface;
6
use Dtc\QueueBundle\Redis\Predis;
7
use Dtc\QueueBundle\Redis\PhpRedis;
8
use Predis\Client;
9
10
class PredisPhpRedisTest extends \PHPUnit\Framework\TestCase
11
{
12
    public function getPhpRedis()
13
    {
14
        $host = getenv('REDIS_HOST');
15
        $port = getenv('REDIS_PORT') ?: 6379;
16
        $jobTimingClass = JobTiming::class;
0 ignored issues
show
Unused Code introduced by
$jobTimingClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
17
        $runClass = Run::class;
0 ignored issues
show
Unused Code introduced by
$runClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18
        $redis = new \Redis();
19
        $redis->connect($host, $port);
20
        $redis->flushAll();
21
        $phpredis = new PhpRedis($redis);
22
23
        return $phpredis;
24
    }
25
26
    public function getPredis()
27
    {
28
        $host = getenv('REDIS_HOST');
29
        $port = getenv('REDIS_PORT') ?: 6379;
30
        $jobTimingClass = JobTiming::class;
0 ignored issues
show
Unused Code introduced by
$jobTimingClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
        $runClass = Run::class;
0 ignored issues
show
Unused Code introduced by
$runClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
        $predisClient = new Client(['scheme' => 'tcp', 'host' => $host, 'port' => $port]);
33
        $predisClient->flushall();
34
        $predis = new Predis($predisClient);
35
36
        return $predis;
37
    }
38
39
    public function testPredisRedis()
40
    {
41
        $redisConnections = [$this->getPhpRedis(), $this->getPredis()];
42
43
        foreach ($redisConnections as $connection) {
44
            $this->getSet($connection);
45
            $this->del($connection);
46
            $this->setEx($connection);
47
            $this->lPush($connection);
48
            $this->lRem($connection);
49
            $this->lRange($connection);
50
            $this->zAdd($connection);
51
            $this->zRem($connection);
52
            $this->zPop($connection);
53
            $this->zPopByMaxScore($connection);
54
        }
55
    }
56
57
    public function getSet(RedisInterface $redis)
58
    {
59
        $redis->del(['testKey']);
60
        self::assertFalse($redis->get('testKey'));
61
        self::assertTrue($redis->set('testKey', 1234));
62
        self::assertEquals(1234, $redis->get('testKey'));
63
        self::assertTrue($redis->set('testKey', 12345));
64
        self::assertEquals(12345, $redis->get('testKey'));
65
    }
66
67
    public function setEx(RedisInterface $redis)
68
    {
69
        self::assertTrue($redis->setEx('testKey', 1, 1234));
70
        self::assertEquals(1234, $redis->get('testKey'));
71
        sleep(2);
72
        self::assertEquals(false, $redis->get('testKey'));
73
    }
74
75
    public function lPush(RedisInterface $redis)
76
    {
77
        $redis->del(['test_list']);
78
        $result = $redis->lPush('test_list', ['a']);
79
        self::assertEquals(1, $result);
80
        $result = $redis->lPush('test_list', ['b']);
81
        self::assertEquals(2, $result);
82
        $result = $redis->lPush('test_list', ['c', 'd']);
83
        self::assertEquals(4, $result);
84
    }
85
86
    public function lRem(RedisInterface $redis)
87
    {
88
        $redis->del(['test_list']);
89
        $result = $redis->lPush('test_list', ['a']);
90
        self::assertEquals(1, $result);
91
        $result = $redis->lPush('test_list', ['b']);
92
        self::assertEquals(2, $result);
93
        $result = $redis->lRem('test_list', 1, 'a');
94
        self::assertEquals(1, $result);
95
        $result = $redis->lRem('test_list', 1, 'a');
96
        self::assertEquals(false, $result);
97
        $result = $redis->lPush('test_list', ['a', 'a', 'a']);
98
        self::assertEquals(4, $result);
99
        $result = $redis->lRem('test_list', 1, 'a');
100
        self::assertEquals(1, $result);
101
        $result = $redis->lRem('test_list', 3, 'a');
102
        self::assertEquals(2, $result);
103
        $result = $redis->lRem('test_list', 3, 'b');
104
        self::assertEquals(1, $result);
105
    }
106
107
    public function lRange(RedisInterface $redis)
108
    {
109
        $redis->del(['test_list']);
110
        $redis->lPush('test_list', ['a']);
111
        $result = $redis->lRange('test_list', 0, 1);
112
        self::assertEquals(['a'], $result);
113
        $result = $redis->lRange('test_list', 0, 0);
114
        self::assertEquals(['a'], $result);
115
        $result = $redis->lRange('test_list', 0, 2);
116
        self::assertEquals(['a'], $result);
117
        $redis->lPush('test_list', ['b']);
118
        $result = $redis->lRange('test_list', 0, 0);
119
        self::assertEquals(['b'], $result);
120
        $result = $redis->lRange('test_list', 0, 1);
121
        self::assertEquals(['b', 'a'], $result);
122
        $result = $redis->lRange('test_list', 0, -1);
123
        self::assertEquals(['b', 'a'], $result);
124
        $result = $redis->lRange('test_list', 0, -2);
125
        self::assertEquals(['b'], $result);
126
    }
127
128
    public function del(RedisInterface $redis)
129
    {
130
        self::assertTrue($redis->set('testKey', 1234));
131
        self::assertTrue($redis->set('testKey1', 12345));
132
        $result = $redis->del(['testKey']);
133
        self::assertEquals(1, $result);
134
        $result = $redis->del(['testKey']);
135
        self::assertEquals(0, $result);
136
        self::assertTrue($redis->set('testKey1', 12345));
137
        $result = $redis->del(['testKey', 'testKey1']);
138
        self::assertEquals(1, $result);
139
        $result = $redis->del(['testKey', 'testKey1']);
140
        self::assertEquals(0, $result);
141
        self::assertTrue($redis->set('testKey', 1234));
142
        self::assertTrue($redis->set('testKey1', 12345));
143
        $result = $redis->del(['testKey', 'testKey1']);
144
        self::assertEquals(2, $result);
145
    }
146
147
    public function zAdd(RedisInterface $redis)
148
    {
149
        $redis->del(['test_zkey']);
150
        $result = $redis->zAdd('test_zkey', 1, 'a');
151
        self::assertEquals(1, $result);
152
        $result = $redis->zAdd('test_zkey', 2, 'a');
153
        self::assertEquals(0, $result);
154
    }
155
156
    public function zRem(RedisInterface $redis)
157
    {
158
        $redis->del(['test_zkey']);
159
        $redis->zAdd('test_zkey', 1, 'a');
160
        $result = $redis->zRem('test_zkey', 'a');
161
        self::assertEquals(1, $result);
162
        $result = $redis->zRem('test_zkey', 'a');
163
        self::assertEquals(0, $result);
164
        $redis->zAdd('test_zkey', 1, 'a');
165
        $redis->zAdd('test_zkey', 1, 'b');
166
        $result = $redis->zRem('test_zkey', 'a');
167
        self::assertEquals(1, $result);
168
        $result = $redis->zRem('test_zkey', 'b');
169
        self::assertEquals(1, $result);
170
    }
171
172
    public function zPop(RedisInterface $redis)
173
    {
174
        $redis->del(['test_zkey']);
175
        $redis->zAdd('test_zkey', 1, 'a');
176
        $redis->zAdd('test_zkey', 2, 'b');
177
        $result = $redis->zPop('test_zkey');
178
        self::assertEquals('a', $result);
179
        $result = $redis->zPop('test_zkey');
180
        self::assertEquals('b', $result);
181
    }
182
183
    public function zPopByMaxScore(RedisInterface $redis)
184
    {
185
        $redis->del(['test_zkey']);
186
        $redis->zAdd('test_zkey', 123456789, 'a');
187
        $redis->zAdd('test_zkey', 212345678, 'b');
188
        $result = $redis->zPopByMaxScore('test_zkey', 0);
189
        self::assertEquals(false, $result);
190
        $result = $redis->zPopByMaxScore('test_zkey', 200000000);
191
        self::assertEquals('a', $result);
192
        $result = $redis->zPopByMaxScore('test_zkey', 200000000);
193
        self::assertEquals(false, $result);
194
        $result = $redis->zPopByMaxScore('test_zkey', 213345678);
195
        self::assertEquals('b', $result);
196
    }
197
}
198