Skip to content

Commit d661290

Browse files
committed
Clean up test suite (namespaces etc.)
1 parent f33c521 commit d661290

File tree

6 files changed

+46
-63
lines changed

6 files changed

+46
-63
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
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",
2629
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"

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: 2 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

tests/FunctionalTest.php

Lines changed: 22 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
}
@@ -73,8 +76,8 @@ public function testInvalidCommand()
7376

7477
public function testMultiExecEmpty()
7578
{
76-
$this->client->multi()->then($this->expectCallableOnce('OK'));
77-
$promise = $this->client->exec()->then($this->expectCallableOnce(array()));
79+
$this->client->multi()->then($this->expectCallableOnceWith('OK'));
80+
$promise = $this->client->exec()->then($this->expectCallableOnceWith(array()));
7881

7982
Block\await($promise, $this->loop);
8083
}
@@ -83,12 +86,12 @@ public function testMultiExecQueuedExecHasValues()
8386
{
8487
$client = $this->client;
8588

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)));
89+
$client->multi()->then($this->expectCallableOnceWith('OK'));
90+
$client->set('b', 10)->then($this->expectCallableOnceWith('QUEUED'));
91+
$client->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED'));
92+
$client->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED'));
93+
$client->ttl('b')->then($this->expectCallableOnceWith('QUEUED'));
94+
$promise = $client->exec()->then($this->expectCallableOnceWith(array('OK', 1, 12, 20)));
9295

9396
Block\await($promise, $this->loop);
9497
}
@@ -107,19 +110,19 @@ public function testPubSub()
107110
$consumer->subscribe($channel)->then($this->expectCallableOnce());
108111

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

112115
// expect "message" event to take no longer than 0.1s
113116
Block\await($deferred->promise(), $this->loop, 0.1);
114117
}
115118

116119
public function testClose()
117120
{
118-
$this->client->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
121+
$this->client->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnceWith());
119122

120123
$this->client->close();
121124

122-
$this->client->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
125+
$this->client->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnceWith());
123126
}
124127

125128
public function testInvalidProtocol()
@@ -142,7 +145,7 @@ public function testInvalidServerRepliesWithDuplicateMessages()
142145
$client->on('error', $this->expectCallableOnce());
143146
$client->on('close', $this->expectCallableOnce());
144147

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

147150
Block\await($promise, $this->loop);
148151
}
@@ -162,15 +165,8 @@ protected function createClientResponse($response)
162165
fwrite($fp, $response);
163166
fseek($fp, 0);
164167

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

167170
return new StreamingClient($stream);
168171
}
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-
}
176172
}

tests/StreamingClientTest.php

Lines changed: 5 additions & 3 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;
@@ -90,7 +92,7 @@ public function testPingPong()
9092
$this->client->handleMessage(new BulkReply('PONG'));
9193

9294
$this->expectPromiseResolve($promise);
93-
$promise->then($this->expectCallableOnce('PONG'));
95+
$promise->then($this->expectCallableOnceWith('PONG'));
9496
}
9597

9698
public function testMonitorCommandIsNotSupported()
@@ -109,7 +111,7 @@ public function testErrorReply()
109111
$this->client->handleMessage($err);
110112

111113
$this->expectPromiseReject($promise);
112-
$promise->then(null, $this->expectCallableOnce($err));
114+
$promise->then(null, $this->expectCallableOnceWith($err));
113115
}
114116

115117
public function testClosingClientRejectsAllRemainingRequests()
@@ -149,7 +151,7 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
149151
$this->assertEquals(0, $closed);
150152

151153
$this->client->handleMessage(new BulkReply('PONG'));
152-
$promise->then($this->expectCallableOnce('PONG'));
154+
$promise->then($this->expectCallableOnceWith('PONG'));
153155
$this->assertEquals(1, $closed);
154156
}
155157

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

3-
use PHPUnit\Framework\TestCase as BaseTestCase;
3+
namespace Clue\Tests\React\Redis;
44

5-
(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);
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
66

77
class TestCase extends BaseTestCase
88
{
99
protected function expectCallableOnce()
1010
{
1111
$mock = $this->createCallableMock();
12-
13-
14-
if (func_num_args() > 0) {
15-
$mock
16-
->expects($this->once())
17-
->method('__invoke')
18-
->with($this->equalTo(func_get_arg(0)));
19-
} else {
20-
$mock
21-
->expects($this->once())
22-
->method('__invoke');
23-
}
12+
$mock
13+
->expects($this->once())
14+
->method('__invoke');
2415

2516
return $mock;
2617
}
2718

28-
protected function expectCallableNever()
19+
protected function expectCallableOnceWith($argument)
2920
{
3021
$mock = $this->createCallableMock();
3122
$mock
32-
->expects($this->never())
33-
->method('__invoke');
23+
->expects($this->once())
24+
->method('__invoke')
25+
->with($argument);
3426

3527
return $mock;
3628
}
3729

38-
protected function expectCallableOnceParameter($type)
30+
protected function expectCallableNever()
3931
{
4032
$mock = $this->createCallableMock();
4133
$mock
42-
->expects($this->once())
43-
->method('__invoke')
44-
->with($this->isInstanceOf($type));
34+
->expects($this->never())
35+
->method('__invoke');
4536

4637
return $mock;
4738
}
4839

49-
/**
50-
* @link https://github.com/reactphp/react/blob/master/tests/React/Tests/Socket/TestCase.php (taken from reactphp/react)
51-
*/
5240
protected function createCallableMock()
5341
{
54-
return $this->getMockBuilder('CallableStub')->getMock();
42+
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
5543
}
5644

5745
protected function expectPromiseResolve($promise)
@@ -83,11 +71,3 @@ protected function expectPromiseReject($promise)
8371
return $promise;
8472
}
8573
}
86-
87-
class CallableStub
88-
{
89-
public function __invoke()
90-
{
91-
}
92-
}
93-

0 commit comments

Comments
 (0)