Skip to content

Commit 708cb32

Browse files
committed
Add tests for Client
Originally part of f87e0d5
1 parent a3f392d commit 708cb32

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

tests/ClientTest.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
use Clue\React\Redis\Client;
4+
use Clue\Redis\Protocol\Parser\ParserException;
5+
use Clue\Redis\Protocol\Model\IntegerReply;
6+
use Clue\Redis\Protocol\Model\BulkReply;
7+
use Clue\Redis\Protocol\Model\ErrorReply;
8+
9+
class ClientTest extends TestCase
10+
{
11+
private $stream;
12+
private $parser;
13+
private $serializer;
14+
private $client;
15+
16+
public function setUp()
17+
{
18+
$this->stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->setMethods(array('write', 'close', 'resume', 'pause'))->getMock();
19+
$this->parser = $this->getMock('Clue\Redis\Protocol\Parser\ParserInterface');
20+
$this->serializer = $this->getMock('Clue\Redis\Protocol\Serializer\SerializerInterface');
21+
22+
$this->client = new Client($this->stream, $this->parser, $this->serializer);
23+
}
24+
25+
public function testSending()
26+
{
27+
$this->serializer->expects($this->once())->method('getRequestMessage')->with($this->equalTo('ping'))->will($this->returnValue('message'));
28+
$this->stream->expects($this->once())->method('write')->with($this->equalTo('message'));
29+
30+
$this->client->ping();
31+
}
32+
33+
public function testClosingClientEmitsEvent()
34+
{
35+
//$this->client->on('close', $this->expectCallableOnce());
36+
37+
$this->client->close();
38+
}
39+
40+
public function testClosingStreamClosesClient()
41+
{
42+
$this->client->on('close', $this->expectCallableOnce());
43+
44+
$this->stream->emit('close');
45+
}
46+
47+
public function testReceiveParseErrorEmitsErrorEvent()
48+
{
49+
$this->client->on('error', $this->expectCallableOnce());
50+
//$this->client->on('close', $this->expectCallableOnce());
51+
52+
$this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->throwException(new ParserException()));
53+
$this->stream->emit('data', array('message'));
54+
}
55+
56+
public function testReceiveMessageEmitsEvent()
57+
{
58+
$this->client->on('message', $this->expectCallableOnce());
59+
60+
$this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2))));
61+
$this->stream->emit('data', array('message'));
62+
}
63+
64+
public function testReceiveThrowMessageEmitsErrorEvent()
65+
{
66+
$this->client->on('message', $this->expectCallableOnce());
67+
$this->client->on('message', function() {
68+
throw new UnderflowException();
69+
});
70+
71+
$this->client->on('error', $this->expectCallableOnce());
72+
73+
$this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2))));
74+
$this->stream->emit('data', array('message'));
75+
}
76+
77+
public function testDefaultCtor()
78+
{
79+
$client = new Client($this->stream);
80+
}
81+
82+
public function testPingPong()
83+
{
84+
$this->serializer->expects($this->once())->method('getRequestMessage')->with($this->equalTo('ping'));
85+
86+
$promise = $this->client->ping();
87+
88+
$this->client->handleMessage(new BulkReply('PONG'));
89+
90+
$this->expectPromiseResolve($promise);
91+
$promise->then($this->expectCallableOnce('PONG'));
92+
}
93+
94+
public function testErrorReply()
95+
{
96+
$promise = $this->client->invalid();
97+
98+
$err = new ErrorReply("ERR unknown command 'invalid'");
99+
$this->client->handleMessage($err);
100+
101+
$this->expectPromiseReject($promise);
102+
$promise->then(null, $this->expectCallableOnce($err));
103+
}
104+
105+
public function testClosingClientRejectsAllRemainingRequests()
106+
{
107+
$promise = $this->client->ping();
108+
$this->assertTrue($this->client->isBusy());
109+
110+
$this->client->close();
111+
112+
$this->expectPromiseReject($promise);
113+
$this->assertFalse($this->client->isBusy());
114+
}
115+
116+
public function testClosedClientRejectsAllNewRequests()
117+
{
118+
$this->client->close();
119+
120+
$promise = $this->client->ping();
121+
122+
$this->expectPromiseReject($promise);
123+
$this->assertFalse($this->client->isBusy());
124+
}
125+
126+
public function testEndingNonBusyClosesClient()
127+
{
128+
$this->markTestIncomplete();
129+
130+
$this->assertFalse($this->client->isBusy());
131+
132+
$this->client->on('close', $this->expectCallableOnce());
133+
$this->client->end();
134+
}
135+
136+
public function testEndingBusyClosesClientWhenNotBusyAnymore()
137+
{
138+
$this->markTestIncomplete();
139+
140+
// count how often the "close" method has been called
141+
$closed = 0;
142+
$this->client->on('close', function() use (&$closed) {
143+
++$closed;
144+
});
145+
146+
$promise = $this->client->ping();
147+
$this->assertEquals(0, $closed);
148+
149+
$this->client->end();
150+
$this->assertEquals(0, $closed);
151+
152+
$this->client->handleMessage(new BulkReply('PONG'));
153+
$this->assertEquals(1, $closed);
154+
}
155+
156+
public function testReceivingUnexpectedMessageThrowsException()
157+
{
158+
$this->setExpectedException('UnderflowException');
159+
$this->client->handleMessage(new BulkReply('PONG'));
160+
}
161+
}

0 commit comments

Comments
 (0)