colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
 * PhpUnderControl_PhalApiRedis_Test
 *
 * 针对 ../../PhalApi/Cache/Redis.php PhalApi_Redis 类的PHPUnit单元测试
 *
 * @author: dogstar 20150516
 */
 
require_once dirname(__FILE__) . '/../test_env.php';
 
if (!class_exists('PhalApi_Cache_Redis')) {
    require dirname(__FILE__) . '/../../PhalApi/Cache/Redis.php';
}
 
class PhpUnderControl_PhalApiRedis_Test extends PHPUnit_Framework_TestCase
{
    public $phalApiRedis;
 
    protected function setUp()
    {
        parent::setUp();
 
        $config = array('host' => '127.0.0.1', 'port' => 6379);
        $this->phalApiRedis = new PhalApi_Cache_Redis($config);
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testSet
     */ 
    public function testSet()
    {
        $key = 'testSetKey';
        $value = 'phalapi';
        $expire = 2;
 
        $rs = $this->phalApiRedis->set($key, $value, $expire);
    }
 
    /**
     * @group testGet
     */ 
    public function testGet()
    {
        $key = 'testSetKey';
 
        $rs = $this->phalApiRedis->get($key);
 
        $this->assertEquals('phalapi', $rs);
 
        sleep(3);
 
        $this->assertSame(NULL, $this->phalApiRedis->get($key));
    }
 
    /**
     * @group testDelete
     */ 
    public function testDelete()
    {
        $key = 'testDeleteKey';
 
        $this->phalApiRedis->set($key, 'net', 3);
 
        $this->assertNotEmpty($this->phalApiRedis->get($key));
 
        $rs = $this->phalApiRedis->delete($key);
 
        $this->assertEmpty($this->phalApiRedis->get($key));
    }
 
    /**
     * @group testSetnx
     */ 
    public function testSetnx()
    {
        $key = 'testSetnxKey';
        $value = array('name' => 'phalapi');
 
        $rs = $this->phalApiRedis->setnx($key, $value);
 
        $this->assertEquals(array('name' => 'phalapi'), $this->phalApiRedis->get($key));
    }
 
    /**
     * @group testLPush
     */ 
    public function testLPush()
    {
        $key = 'testLPushKey';
 
        $this->phalApiRedis->delete($key);
 
        $rs = $this->phalApiRedis->lPush($key, 1);
        $this->assertEquals(1, $rs);
        $rs = $this->phalApiRedis->lPush($key, 2);
        $this->assertEquals(2, $rs);
        $rs = $this->phalApiRedis->lPush($key, 3);
        $this->assertEquals(3, $rs);
    }
 
    /**
     * @group testRPush
     */ 
    public function testRPush()
    {
        $key = 'testRPushKey';
 
        $this->phalApiRedis->delete($key);
 
        $rs = $this->phalApiRedis->rPush($key, 1);
        $this->assertEquals(1, $rs);
        $rs = $this->phalApiRedis->rPush($key, 'haha~');
        $this->assertEquals(2, $rs);
        $rs = $this->phalApiRedis->rPush($key, true);
        $this->assertEquals(3, $rs);
        $rs = $this->phalApiRedis->rPush($key, array('name' => 'phalapi'));
        $this->assertEquals(4, $rs);
    }
 
    /**
     * @group testLPop
     */ 
    public function testLPop()
    {
        $key = 'testLPushKey';
 
        $rs = $this->phalApiRedis->lPop($key);
        $this->assertEquals(3, $rs);
        $rs = $this->phalApiRedis->lPop($key);
        $this->assertEquals(2, $rs);
        $rs = $this->phalApiRedis->lPop($key);
        $this->assertEquals(1, $rs);
 
        $rs = $this->phalApiRedis->lPop($key);
        $this->assertEquals(NULL, $rs);
    }
 
    /**
     * @group testRPop
     */ 
    public function testRPop()
    {
        $key = 'testRPushKey';
 
        $rs = $this->phalApiRedis->rPop($key);
        $this->assertSame(array('name' => 'phalapi'), $rs);
        $rs = $this->phalApiRedis->rPop($key);
        $this->assertSame(true, $rs);
        $rs = $this->phalApiRedis->rPop($key);
        $this->assertSame('haha~', $rs);
        $rs = $this->phalApiRedis->rPop($key);
        $this->assertSame(1, $rs);
 
        $rs = $this->phalApiRedis->rPop($key);
        $this->assertSame(NULL, $rs);
    }
 
    public function testPushAndPop()
    {
        $key = 'testPushAndPopKey';
 
        $this->phalApiRedis->delete($key);
 
        $this->phalApiRedis->rPush($key, 'www');
        $this->phalApiRedis->rPush($key, 'phalapi');
        $this->phalApiRedis->rPush($key, 'net');
 
        $this->assertEquals('www', $this->phalApiRedis->lPop($key));
        $this->assertEquals('phalapi', $this->phalApiRedis->lPop($key));
        $this->assertEquals('net', $this->phalApiRedis->lPop($key));
    }
}