Skip to content

Commit 2856fe2

Browse files
authored
Merge pull request #84 from clue-labs/phpunit
Improve test suite structure and add forward compatibility with PHPUnit 7 and PHPUnit 6
2 parents c1232de + 2a37657 commit 2856fe2

File tree

6 files changed

+68
-65
lines changed

6 files changed

+68
-65
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
"autoload": {
2222
"psr-4": { "Clue\\React\\Redis\\": "src/" }
2323
},
24+
"autoload-dev": {
25+
"psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" }
26+
},
2427
"require-dev": {
2528
"clue/block-react": "^1.1",
26-
"phpunit/phpunit": "^5.0 || ^4.8"
29+
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
2730
}
2831
}

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="tests/bootstrap.php"
3+
<phpunit bootstrap="vendor/autoload.php"
44
colors="true"
55
convertErrorsToExceptions="true"
66
convertNoticesToExceptions="true"

tests/FactoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Clue\Tests\React\Redis;
4+
35
use Clue\React\Redis\Factory;
46
use React\Promise;
57

@@ -16,6 +18,9 @@ public function setUp()
1618
$this->factory = new Factory($this->loop, $this->connector);
1719
}
1820

21+
/**
22+
* @doesNotPerformAssertions
23+
*/
1924
public function testCtor()
2025
{
2126
$this->factory = new Factory($this->loop);

tests/FunctionalTest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22

3+
namespace Clue\Tests\React\Redis;
4+
35
use Clue\React\Block;
6+
use Clue\React\Redis\Client;
47
use Clue\React\Redis\Factory;
58
use Clue\React\Redis\StreamingClient;
9+
use React\EventLoop\StreamSelectLoop;
610
use React\Promise\Deferred;
7-
use React\Stream\Stream;
811
use React\Stream\DuplexResourceStream;
912

1013
class FunctionalTest extends TestCase
@@ -20,7 +23,7 @@ public function setUp()
2023
$this->markTestSkipped('No REDIS_URI environment variable given');
2124
}
2225

23-
$this->loop = new React\EventLoop\StreamSelectLoop();
26+
$this->loop = new StreamSelectLoop();
2427
$this->factory = new Factory($this->loop);
2528
$this->client = $this->createClient($uri);
2629
}
@@ -55,10 +58,10 @@ public function testPipeline()
5558
{
5659
$client = $this->client;
5760

58-
$client->set('a', 1)->then($this->expectCallableOnce('OK'));
59-
$client->incr('a')->then($this->expectCallableOnce(2));
60-
$client->incr('a')->then($this->expectCallableOnce(3));
61-
$promise = $client->get('a')->then($this->expectCallableOnce('3'));
61+
$client->set('a', 1)->then($this->expectCallableOnceWith('OK'));
62+
$client->incr('a')->then($this->expectCallableOnceWith(2));
63+
$client->incr('a')->then($this->expectCallableOnceWith(3));
64+
$promise = $client->get('a')->then($this->expectCallableOnceWith('3'));
6265

6366
Block\await($promise, $this->loop);
6467
}
@@ -67,14 +70,18 @@ public function testInvalidCommand()
6770
{
6871
$promise = $this->client->doesnotexist(1, 2, 3);
6972

70-
$this->setExpectedException('Exception');
73+
if (method_exists($this, 'expectException')) {
74+
$this->expectException('Exception');
75+
} else {
76+
$this->setExpectedException('Exception');
77+
}
7178
Block\await($promise, $this->loop);
7279
}
7380

7481
public function testMultiExecEmpty()
7582
{
76-
$this->client->multi()->then($this->expectCallableOnce('OK'));
77-
$promise = $this->client->exec()->then($this->expectCallableOnce(array()));
83+
$this->client->multi()->then($this->expectCallableOnceWith('OK'));
84+
$promise = $this->client->exec()->then($this->expectCallableOnceWith(array()));
7885

7986
Block\await($promise, $this->loop);
8087
}
@@ -83,12 +90,12 @@ public function testMultiExecQueuedExecHasValues()
8390
{
8491
$client = $this->client;
8592

86-
$client->multi()->then($this->expectCallableOnce('OK'));
87-
$client->set('b', 10)->then($this->expectCallableOnce('QUEUED'));
88-
$client->expire('b', 20)->then($this->expectCallableOnce('QUEUED'));
89-
$client->incrBy('b', 2)->then($this->expectCallableOnce('QUEUED'));
90-
$client->ttl('b')->then($this->expectCallableOnce('QUEUED'));
91-
$promise = $client->exec()->then($this->expectCallableOnce(array('OK', 1, 12, 20)));
93+
$client->multi()->then($this->expectCallableOnceWith('OK'));
94+
$client->set('b', 10)->then($this->expectCallableOnceWith('QUEUED'));
95+
$client->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED'));
96+
$client->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED'));
97+
$client->ttl('b')->then($this->expectCallableOnceWith('QUEUED'));
98+
$promise = $client->exec()->then($this->expectCallableOnceWith(array('OK', 1, 12, 20)));
9299

93100
Block\await($promise, $this->loop);
94101
}
@@ -107,7 +114,7 @@ public function testPubSub()
107114
$consumer->subscribe($channel)->then($this->expectCallableOnce());
108115

109116
// producer sends a single message
110-
$producer->publish($channel, 'hello world')->then($this->expectCallableOnce(1));
117+
$producer->publish($channel, 'hello world')->then($this->expectCallableOnceWith(1));
111118

112119
// expect "message" event to take no longer than 0.1s
113120
Block\await($deferred->promise(), $this->loop, 0.1);
@@ -131,7 +138,11 @@ public function testInvalidProtocol()
131138

132139
$promise = $client->get('willBeRejectedDueToClosing');
133140

134-
$this->setExpectedException('Exception');
141+
if (method_exists($this, 'expectException')) {
142+
$this->expectException('Exception');
143+
} else {
144+
$this->setExpectedException('Exception');
145+
}
135146
Block\await($promise, $this->loop);
136147
}
137148

@@ -142,7 +153,7 @@ public function testInvalidServerRepliesWithDuplicateMessages()
142153
$client->on('error', $this->expectCallableOnce());
143154
$client->on('close', $this->expectCallableOnce());
144155

145-
$promise = $client->set('a', 0)->then($this->expectCallableOnce('OK'));
156+
$promise = $client->set('a', 0)->then($this->expectCallableOnceWith('OK'));
146157

147158
Block\await($promise, $this->loop);
148159
}
@@ -162,15 +173,8 @@ protected function createClientResponse($response)
162173
fwrite($fp, $response);
163174
fseek($fp, 0);
164175

165-
$stream = class_exists('React\Stream\DuplexResourceStream') ? new DuplexResourceStream($fp, $this->loop) : new Stream($fp, $this->loop);
176+
$stream = new DuplexResourceStream($fp, $this->loop);
166177

167178
return new StreamingClient($stream);
168179
}
169-
170-
protected function createServer($response)
171-
{
172-
$port = 1337;
173-
$cmd = 'echo -e "' . str_replace("\r\n", '\r\n', $response) . '" | nc -lC ' . $port;
174-
175-
}
176180
}

tests/StreamingClientTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Clue\Tests\React\Redis;
4+
35
use Clue\React\Redis\StreamingClient;
46
use Clue\Redis\Protocol\Parser\ParserException;
57
use Clue\Redis\Protocol\Model\IntegerReply;
@@ -73,6 +75,9 @@ public function testReceiveThrowMessageEmitsErrorEvent()
7375
$this->stream->emit('data', array('message'));
7476
}
7577

78+
/**
79+
* @doesNotPerformAssertions
80+
*/
7681
public function testDefaultCtor()
7782
{
7883
$client = new StreamingClient($this->stream);
@@ -87,7 +92,7 @@ public function testPingPong()
8792
$this->client->handleMessage(new BulkReply('PONG'));
8893

8994
$this->expectPromiseResolve($promise);
90-
$promise->then($this->expectCallableOnce('PONG'));
95+
$promise->then($this->expectCallableOnceWith('PONG'));
9196
}
9297

9398
public function testMonitorCommandIsNotSupported()
@@ -106,7 +111,7 @@ public function testErrorReply()
106111
$this->client->handleMessage($err);
107112

108113
$this->expectPromiseReject($promise);
109-
$promise->then(null, $this->expectCallableOnce($err));
114+
$promise->then(null, $this->expectCallableOnceWith($err));
110115
}
111116

112117
public function testClosingClientRejectsAllRemainingRequests()
@@ -146,7 +151,7 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
146151
$this->assertEquals(0, $closed);
147152

148153
$this->client->handleMessage(new BulkReply('PONG'));
149-
$promise->then($this->expectCallableOnce('PONG'));
154+
$promise->then($this->expectCallableOnceWith('PONG'));
150155
$this->assertEquals(1, $closed);
151156
}
152157

@@ -160,7 +165,11 @@ public function testClosingMultipleTimesEmitsOnce()
160165

161166
public function testReceivingUnexpectedMessageThrowsException()
162167
{
163-
$this->setExpectedException('UnderflowException');
168+
if (method_exists($this, 'expectException')) {
169+
$this->expectException('UnderflowException');
170+
} else {
171+
$this->setExpectedException('UnderflowException');
172+
}
164173
$this->client->handleMessage(new BulkReply('PONG'));
165174
}
166175

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,45 @@
11
<?php
22

3-
(include_once __DIR__ . '/../vendor/autoload.php') OR die(PHP_EOL . 'ERROR: composer autoloader not found, run "composer install" or see README for instructions' . PHP_EOL);
3+
namespace Clue\Tests\React\Redis;
44

5-
class TestCase extends PHPUnit_Framework_TestCase
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
7+
class TestCase extends BaseTestCase
68
{
79
protected function expectCallableOnce()
810
{
911
$mock = $this->createCallableMock();
10-
11-
12-
if (func_num_args() > 0) {
13-
$mock
14-
->expects($this->once())
15-
->method('__invoke')
16-
->with($this->equalTo(func_get_arg(0)));
17-
} else {
18-
$mock
19-
->expects($this->once())
20-
->method('__invoke');
21-
}
12+
$mock
13+
->expects($this->once())
14+
->method('__invoke');
2215

2316
return $mock;
2417
}
2518

26-
protected function expectCallableNever()
19+
protected function expectCallableOnceWith($argument)
2720
{
2821
$mock = $this->createCallableMock();
2922
$mock
30-
->expects($this->never())
31-
->method('__invoke');
23+
->expects($this->once())
24+
->method('__invoke')
25+
->with($argument);
3226

3327
return $mock;
3428
}
3529

36-
protected function expectCallableOnceParameter($type)
30+
protected function expectCallableNever()
3731
{
3832
$mock = $this->createCallableMock();
3933
$mock
40-
->expects($this->once())
41-
->method('__invoke')
42-
->with($this->isInstanceOf($type));
34+
->expects($this->never())
35+
->method('__invoke');
4336

4437
return $mock;
4538
}
4639

47-
/**
48-
* @link https://github.com/reactphp/react/blob/master/tests/React/Tests/Socket/TestCase.php (taken from reactphp/react)
49-
*/
5040
protected function createCallableMock()
5141
{
52-
return $this->getMockBuilder('CallableStub')->getMock();
42+
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
5343
}
5444

5545
protected function expectPromiseResolve($promise)
@@ -81,11 +71,3 @@ protected function expectPromiseReject($promise)
8171
return $promise;
8272
}
8373
}
84-
85-
class CallableStub
86-
{
87-
public function __invoke()
88-
{
89-
}
90-
}
91-

0 commit comments

Comments
 (0)