forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryLazyClientTest.php
More file actions
180 lines (141 loc) · 7.76 KB
/
FactoryLazyClientTest.php
File metadata and controls
180 lines (141 loc) · 7.76 KB
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
179
180
<?php
namespace Clue\Tests\React\Redis;
use Clue\React\Redis\Factory;
use React\Promise;
class FactoryLazyClientTest extends TestCase
{
private $loop;
private $connector;
private $factory;
/**
* @before
*/
public function setUpFactory()
{
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->factory = new Factory($this->loop, $this->connector);
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$factory = new Factory();
$ref = new \ReflectionProperty($factory, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($factory);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testContructorThrowsExceptionForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
new Factory('loop');
}
public function testContructorThrowsExceptionForInvalidConnector()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
new Factory(null, 'connector');
}
public function testContructorThrowsExceptionForInvalidProtocolFactory()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($protocol) expected null|Clue\Redis\Protocol\Factory');
new Factory(null, null, 'protocol');
}
public function testWillConnectWithDefaultPort()
{
$this->connector->expects($this->never())->method('connect');
$this->factory->createLazyClient('redis.example.com');
}
public function testWillConnectToLocalhost()
{
$this->connector->expects($this->never())->method('connect');
$this->factory->createLazyClient('localhost:1337');
}
public function testWillResolveIfConnectorResolves()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write');
$this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream));
$redis = $this->factory->createLazyClient('localhost');
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
}
public function testWillWriteSelectCommandIfTargetContainsPath()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$4\r\ndemo\r\n");
$this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://127.0.0.1/demo');
}
public function testWillWriteSelectCommandIfTargetContainsDbQueryParameter()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$1\r\n4\r\n");
$this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://127.0.0.1?db=4');
}
public function testWillWriteAuthCommandIfRedisUriContainsUserInfo()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
$this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://hello:world@example.com');
}
public function testWillWriteAuthCommandIfRedisUriContainsEncodedUserInfo()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nh@llo\r\n");
$this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://:h%40llo@example.com');
}
public function testWillWriteAuthCommandIfTargetContainsPasswordQueryParameter()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$6\r\nsecret\r\n");
$this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://example.com?password=secret');
}
public function testWillWriteAuthCommandIfTargetContainsEncodedPasswordQueryParameter()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nh@llo\r\n");
$this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis://example.com?password=h%40llo');
}
public function testWillWriteAuthCommandIfRedissUriContainsUserInfo()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
$this->connector->expects($this->never())->method('connect')->with('tls://example.com:6379')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('rediss://hello:world@example.com');
}
public function testWillWriteAuthCommandIfRedisUnixUriContainsPasswordQueryParameter()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
$this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis+unix:///tmp/redis.sock?password=world');
}
public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
$this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis+unix://hello:world@/tmp/redis.sock');
}
public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$4\r\ndemo\r\n");
$this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream));
$this->factory->createLazyClient('redis+unix:///tmp/redis.sock?db=demo');
}
public function testWillRejectIfConnectorRejects()
{
$this->connector->expects($this->never())->method('connect');
$redis = $this->factory->createLazyClient('redis://127.0.0.1:2');
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
}
public function testWillRejectIfTargetIsInvalid()
{
$redis = $this->factory->createLazyClient('http://invalid target');
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
}
}