Completed
Push — master ( 511c4d...dce3f3 )
by Razon
01:21
created

RedisManagerTest::testSaveWithoutTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
namespace RazonYang\TokenBucket\Tests;
3
4
use Psr\Log\NullLogger;
5
use RazonYang\TokenBucket\Manager\RedisManager;
6
7
class RedisManagerTest extends TestCase
8
{
9
    private $capacity = 10;
10
11
    private $rate = 1;
12
13
    private $ttl = 60;
14
15
    private $prefix = 'test:';
16
17
    /**
18
     * @var \Redis
19
     */
20
    private $redis;
21
22
    public function setUp(): void
23
    {
24
        parent::setUp();
25
26
        $redis = new \Redis();
27
        $redis->connect('localhost', 6379);
28
        $redis->auth('foobar');
29
        $this->redis = $redis;
30
    }
31
32
    public function tearDown(): void
33
    {
34
        $this->redis = null;
35
36
        parent::tearDown();
37
    }
38
39
    public function createManager(): RedisManager
40
    {
41
        return new RedisManager($this->capacity, $this->rate, new NullLogger(), $this->redis, $this->ttl, $this->prefix);
42
    }
43
44
    public function testSetUp()
45
    {
46
        $manager = $this->createManager();
47
48
        $ttl = new \ReflectionProperty(RedisManager::class, 'ttl');
49
        $ttl->setAccessible(true);
50
        $this->assertSame($this->ttl, $ttl->getValue($manager));
51
52
        $prefix = new \ReflectionProperty(RedisManager::class, 'prefix');
53
        $prefix->setAccessible(true);
54
        $this->assertSame($this->prefix, $prefix->getValue($manager));
55
56
        $conn = new \ReflectionProperty(RedisManager::class, 'conn');
57
        $conn->setAccessible(true);
58
        $this->assertSame($this->redis, $conn->getValue($manager));
59
    }
60
61
    public function testSaveAndLoad()
62
    {
63
        $manager = $this->createManager();
64
        $save = new \ReflectionMethod(RedisManager::class, 'save');
65
        $save->setAccessible(true);
66
67
        $load = new \ReflectionMethod(RedisManager::class, 'load');
68
        $load->setAccessible(true);
69
70
        $name = 'test';
71
        $data = uniqid();
72
        $save->invoke($manager, $name, $data);
73
74
        $this->assertSame($data, $load->invoke($manager, $name));
75
        $this->assertSame($data, $this->redis->get($this->prefix . $name));
76
        $this->assertTrue($this->redis->ttl($this->prefix . $name) > 0);
77
    }
78
79
    public function testSaveWithoutTtl()
80
    {
81
        $manager = new RedisManager($this->capacity, $this->rate, new NullLogger(), $this->redis, 0, $this->prefix);
82
        $save = new \ReflectionMethod(RedisManager::class, 'save');
83
        $save->setAccessible(true);
84
        $name = 'test';
85
        $data = uniqid();
86
        $save->invoke($manager, $name, $data);
87
        $this->assertEquals(-1, $this->redis->ttl($this->prefix . $name));
88
    }
89
}
90