Skip to content

Commit c3017db

Browse files
committed
Merge pull request clue#37 from clue-labs/travis
First class support for PHP 5.3 through PHP 7 and HHVM
2 parents db30417 + 8e1d0ef commit c3017db

File tree

4 files changed

+74
-102
lines changed

4 files changed

+74
-102
lines changed

.travis.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
language: php
2+
23
php:
34
- 5.3
45
- 5.4
6+
- 5.5
7+
- 5.6
8+
- 7
59
- hhvm
10+
611
services:
712
- redis-server
8-
before_script:
9-
- composer install --prefer-source --no-interaction
13+
14+
sudo: false
15+
16+
install:
17+
- composer install --no-interaction
18+
1019
script:
1120
- phpunit --coverage-text

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
},
2121
"autoload": {
2222
"psr-4": { "Clue\\React\\Redis\\": "src/" }
23+
},
24+
"require-dev": {
25+
"clue/block-react": "^1.1"
2326
}
2427
}

tests/FunctionalTest.php

Lines changed: 44 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
<?php
22

33
use React\Stream\Stream;
4-
54
use React\Stream\ReadableStream;
6-
75
use Clue\React\Redis\Factory;
8-
96
use Clue\React\Redis\StreamingClient;
7+
use React\Promise\Deferred;
8+
use Clue\React\Block;
109

1110
class FunctionalTest extends TestCase
1211
{
13-
protected static $loop;
14-
protected static $factory;
12+
private $loop;
13+
private $factory;
14+
private $client;
1515

16-
public static function setUpBeforeClass()
16+
public function setUp()
1717
{
18-
self::$loop = new React\EventLoop\StreamSelectLoop();
19-
self::$factory = new Factory(self::$loop);
18+
$this->loop = new React\EventLoop\StreamSelectLoop();
19+
$this->factory = new Factory($this->loop);
20+
$this->client = $this->createClient();
2021
}
2122

2223
public function testPing()
2324
{
24-
$client = $this->createClient();
25+
$client = $this->client;
2526

2627
$promise = $client->ping();
2728
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
@@ -36,7 +37,7 @@ public function testPing()
3637

3738
public function testMgetIsNotInterpretedAsSubMessage()
3839
{
39-
$client = $this->createClient();
40+
$client = $this->client;
4041

4142
$client->mset('message', 'message', 'channel', 'channel', 'payload', 'payload');
4243

@@ -46,14 +47,9 @@ public function testMgetIsNotInterpretedAsSubMessage()
4647
$this->waitFor($client);
4748
}
4849

49-
/**
50-
*
51-
* @param StreamingClient $client
52-
* @depends testPing
53-
*/
54-
public function testPipeline(StreamingClient $client)
50+
public function testPipeline()
5551
{
56-
$this->assertFalse($client->isBusy());
52+
$client = $this->client;
5753

5854
$client->set('a', 1)->then($this->expectCallableOnce('OK'));
5955
$client->incr('a')->then($this->expectCallableOnce(2));
@@ -63,46 +59,27 @@ public function testPipeline(StreamingClient $client)
6359
$this->assertTrue($client->isBusy());
6460

6561
$this->waitFor($client);
66-
67-
return $client;
6862
}
6963

70-
/**
71-
*
72-
* @param StreamingClient $client
73-
* @depends testPipeline
74-
*/
75-
public function testInvalidCommand(StreamingClient $client)
64+
public function testInvalidCommand()
7665
{
77-
$client->doesnotexist(1, 2, 3)->then($this->expectCallableNever());
78-
79-
$this->waitFor($client);
66+
$this->client->doesnotexist(1, 2, 3)->then($this->expectCallableNever());
8067

81-
return $client;
68+
$this->waitFor($this->client);
8269
}
8370

84-
/**
85-
*
86-
* @param StreamingClient $client
87-
* @depends testInvalidCommand
88-
*/
89-
public function testMultiExecEmpty(StreamingClient $client)
71+
public function testMultiExecEmpty()
9072
{
91-
$client->multi()->then($this->expectCallableOnce('OK'));
92-
$client->exec()->then($this->expectCallableOnce(array()));
93-
94-
$this->waitFor($client);
73+
$this->client->multi()->then($this->expectCallableOnce('OK'));
74+
$this->client->exec()->then($this->expectCallableOnce(array()));
9575

96-
return $client;
76+
$this->waitFor($this->client);
9777
}
9878

99-
/**
100-
*
101-
* @param StreamingClient $client
102-
* @depends testMultiExecEmpty
103-
*/
104-
public function testMultiExecQueuedExecHasValues(StreamingClient $client)
79+
public function testMultiExecQueuedExecHasValues()
10580
{
81+
$client = $this->client;
82+
10683
$client->multi()->then($this->expectCallableOnce('OK'));
10784
$client->set('b', 10)->then($this->expectCallableOnce('QUEUED'));
10885
$client->expire('b', 20)->then($this->expectCallableOnce('QUEUED'));
@@ -111,17 +88,12 @@ public function testMultiExecQueuedExecHasValues(StreamingClient $client)
11188
$client->exec()->then($this->expectCallableOnce(array('OK', 1, 12, 20)));
11289

11390
$this->waitFor($client);
114-
115-
return $client;
11691
}
11792

118-
/**
119-
*
120-
* @param StreamingClient $client
121-
* @depends testPipeline
122-
*/
123-
public function testMonitorPing(StreamingClient $client)
93+
public function testMonitorPing()
12494
{
95+
$client = $this->client;
96+
12597
$client->on('monitor', $this->expectCallableOnce());
12698

12799
$client->monitor()->then($this->expectCallableOnce('OK'));
@@ -132,29 +104,33 @@ public function testMonitorPing(StreamingClient $client)
132104

133105
public function testPubSub()
134106
{
135-
$consumer = $this->createClient();
107+
$consumer = $this->client;
136108
$producer = $this->createClient();
137109

138-
$that = $this;
110+
$channel = 'channel:test:' . mt_rand();
139111

140-
$producer->publish('channel:test', 'nobody sees this')->then($this->expectCallableOnce(0));
112+
// consumer receives a single message
113+
$deferred = new Deferred();
114+
$consumer->on('message', $this->expectCallableOnce());
115+
$consumer->on('message', array($deferred, 'resolve'));
116+
$consumer->subscribe($channel)->then($this->expectCallableOnce());
117+
$this->waitFor($consumer);
141118

119+
// producer sends a single message
120+
$producer->publish($channel, 'hello world')->then($this->expectCallableOnce());
142121
$this->waitFor($producer);
143122

144-
$consumer->subscribe('channel:test')->then(function () {
145-
// ?
146-
});
123+
// expect "message" event to take no longer than 0.1s
124+
Block\await($deferred->promise(), $this->loop, 0.1);
147125
}
148126

149127
public function testClose()
150128
{
151-
$client = $this->createClient();
129+
$this->client->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
152130

153-
$client->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
131+
$this->client->close();
154132

155-
$client->close();
156-
157-
$client->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
133+
$this->client->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
158134
}
159135

160136
public function testInvalidProtocol()
@@ -186,24 +162,7 @@ public function testInvalidServerRepliesWithDuplicateMessages()
186162
*/
187163
protected function createClient()
188164
{
189-
$client = null;
190-
$exception = null;
191-
192-
self::$factory->createClient()->then(function ($c) use (&$client) {
193-
$client = $c;
194-
}, function($error) use (&$exception) {
195-
$exception = $error;
196-
});
197-
198-
while ($client === null && $exception === null) {
199-
self::$loop->tick();
200-
}
201-
202-
if ($exception !== null) {
203-
throw $exception;
204-
}
205-
206-
return $client;
165+
return Block\await($this->factory->createClient(), $this->loop);
207166
}
208167

209168
protected function createClientResponse($response)
@@ -212,7 +171,7 @@ protected function createClientResponse($response)
212171
fwrite($fp, $response);
213172
fseek($fp, 0);
214173

215-
$stream = new Stream($fp, self::$loop);
174+
$stream = new Stream($fp, $this->loop);
216175

217176
return new StreamingClient($stream);
218177
}
@@ -229,7 +188,7 @@ protected function waitFor(StreamingClient $client)
229188
$this->assertTrue($client->isBusy());
230189

231190
while ($client->isBusy()) {
232-
self::$loop->tick();
191+
$this->loop->tick();
233192
}
234193
}
235194
}

tests/StreamingClientTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Clue\React\Redis\Client;
1010
use Clue\Redis\Protocol\Model\StatusReply;
1111

12-
class ClientTest extends TestCase
12+
class StreamingClientTest extends TestCase
1313
{
1414
private $stream;
1515
private $parser;
@@ -112,29 +112,30 @@ public function testMonitor()
112112

113113
$this->expectPromiseResolve($promise);
114114
$promise->then($this->expectCallableOnce('OK'));
115-
116-
return $this->client;
117115
}
118116

119-
/**
120-
* @depends testMonitor
121-
* @param StreamingClient $client
122-
*/
123-
public function testMonitorEvent(StreamingClient $client)
117+
public function testMonitorEventFromOtherConnection()
124118
{
125-
$client->on('monitor', $this->expectCallableOnce());
119+
// enter MONITOR mode
120+
$client = $this->client;
121+
$client->monitor();
122+
$client->handleMessage(new StatusReply('OK'));
126123

124+
// expect a single "monitor" event when a matching status reply comes in
125+
$client->on('monitor', $this->expectCallableOnce());
127126
$client->handleMessage(new StatusReply('1409171800.312243 [0 127.0.0.1:58542] "ping"'));
128127
}
129128

130-
/**
131-
* @depends testMonitor
132-
* @param StreamingClient $client
133-
*/
134-
public function testMonitorPing(StreamingClient $client)
129+
public function testMonitorEventFromPingMessage()
135130
{
136-
$client->on('monitor', $this->expectCallableOnce());
131+
// enter MONITOR mode
132+
$client = $this->client;
133+
$client->monitor();
134+
$client->handleMessage(new StatusReply('OK'));
137135

136+
// expect a single "monitor" event when a matching status reply comes in
137+
// ignore the status reply for the command executed (ping)
138+
$client->on('monitor', $this->expectCallableOnce());
138139
$client->ping();
139140
$client->handleMessage(new StatusReply('1409171800.312243 [0 127.0.0.1:58542] "ping"'));
140141
$client->handleMessage(new StatusReply('PONG'));

0 commit comments

Comments
 (0)