1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kazuakim\Reddish; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ClientsTest. |
7
|
|
|
* |
8
|
|
|
* @property array $_defaultConfig |
9
|
|
|
* @property object $_clients; |
10
|
|
|
* |
11
|
|
|
* @copyright KazuakiM <[email protected]> |
12
|
|
|
* @author KazuakiM <[email protected]> |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/KazuakiM/reddish |
16
|
|
|
*/ |
17
|
|
|
class ClientsTest extends \PHPUnit\Framework\TestCase //{{{ |
18
|
|
|
{ |
19
|
|
|
// Class variable {{{ |
|
|
|
|
20
|
|
|
private $_defaultConfig = [ |
21
|
|
|
// 'connect' paramater |
22
|
|
|
'host' => '127.0.0.1', //can be a host, or the path to a unix domain socket |
23
|
|
|
'port' => 6379, |
24
|
|
|
'timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited) |
25
|
|
|
'reserved' => null, //should be NULL if retry_interval is specified |
26
|
|
|
'read_timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited) |
27
|
|
|
|
28
|
|
|
// 'pconnect' paramater |
29
|
|
|
'persistent_id' => '', //identity for the requested persistent connection |
30
|
|
|
|
31
|
|
|
// 'auth' paramater |
32
|
|
|
'password' => null, |
33
|
|
|
|
34
|
|
|
// serializer |
35
|
|
|
'serializer' => \Redis::SERIALIZER_NONE, |
36
|
|
|
|
37
|
|
|
// 'connect' or 'pconnect' |
|
|
|
|
38
|
|
|
'persistent' => false, //default is connect |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
private $_clients; |
42
|
|
|
//}}} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* setUp. |
46
|
|
|
*/ |
47
|
|
|
protected function setUp() //{{{ |
48
|
|
|
{ |
49
|
|
|
$this->_clients = new Clients($this->_defaultConfig); |
50
|
|
|
} //}}} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* tearDown. |
54
|
|
|
*/ |
55
|
|
|
protected function tearDown() //{{{ |
56
|
|
|
{ |
57
|
|
|
unset($this->_clients); |
58
|
|
|
} //}}} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* testConnectionPersistentError. |
62
|
|
|
* |
63
|
|
|
* @expectedException \RedisException |
64
|
|
|
* @expectedExceptionCode 0 |
65
|
|
|
* @expectedExceptionMessage connect errored. |
66
|
|
|
*/ |
67
|
|
|
public function testConnectionPersistentError() //{{{ |
68
|
|
|
{ |
69
|
|
|
$config = $this->_defaultConfig; |
70
|
|
|
$config['host'] = '127.0.0.2'; |
71
|
|
|
$clients = new Clients($config); |
|
|
|
|
72
|
|
|
} //}}} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* testConnectionError. |
76
|
|
|
* |
77
|
|
|
* @expectedException \RedisException |
78
|
|
|
* @expectedExceptionCode 0 |
79
|
|
|
* @expectedExceptionMessage pconnect errored. |
80
|
|
|
*/ |
81
|
|
|
public function testConnectionError() //{{{ |
82
|
|
|
{ |
83
|
|
|
$config = $this->_defaultConfig; |
84
|
|
|
$config['host'] = '127.0.0.2'; |
85
|
|
|
$config['persistent'] = true; |
86
|
|
|
$clients = new Clients($config); |
|
|
|
|
87
|
|
|
} //}}} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* testPingError. |
91
|
|
|
* |
92
|
|
|
* @expectedException \RedisException |
93
|
|
|
* @expectedExceptionCode 0 |
94
|
|
|
* @expectedExceptionMessage Connection closed |
95
|
|
|
*/ |
96
|
|
|
public function testPingError() //{{{ |
97
|
|
|
{ |
98
|
|
|
$config = $this->_defaultConfig; |
99
|
|
|
$config['persistent'] = true; |
100
|
|
|
$clients = new Clients($config); |
101
|
|
|
$clients->close(); |
102
|
|
|
$clients->ping(); |
103
|
|
|
} //}}} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* testConnection. |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testConnection() //{{{ |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->assertTrue($this->_clients->isConnected()); |
111
|
|
|
|
112
|
|
|
$config = $this->_defaultConfig; |
113
|
|
|
$config['persistent'] = true; |
114
|
|
|
$clients = new Clients($config); |
115
|
|
|
$this->assertTrue($clients->isConnected()); |
|
|
|
|
116
|
|
|
$clients->ping(); |
117
|
|
|
$clients->close(); |
118
|
|
|
|
119
|
|
|
unset($clients); |
120
|
|
|
} //}}} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* testIsConnected. |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function testIsConnected() //{{{ |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$config = $this->_defaultConfig; |
128
|
|
|
$config['persistent'] = true; |
129
|
|
|
$clients = new Clients($config); |
130
|
|
|
$this->assertTrue($clients->isConnected()); |
131
|
|
|
$clients->close(); |
132
|
|
|
|
133
|
|
|
$this->assertFalse($clients->isConnected()); |
134
|
|
|
$clients->close(); |
135
|
|
|
} //}}} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* testSave. |
139
|
|
|
*/ |
140
|
|
|
public function testSave() //{{{ |
141
|
|
|
{ |
142
|
|
|
$this->_clients->set('key', '1'); |
143
|
|
|
$this->assertSame('1', $this->_clients->get('key')); |
144
|
|
|
} //}}} |
145
|
|
|
} //}}} |
146
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.