Skip to content

Improve test suite structure and add forward compatibility with PHPUnit 7 and PHPUnit 6 #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
"autoload": {
"psr-4": { "Clue\\React\\Redis\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" }
},
"require-dev": {
"clue/block-react": "^1.1",
"phpunit/phpunit": "^5.0 || ^4.8"
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="tests/bootstrap.php"
<phpunit bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
5 changes: 5 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Redis;

use Clue\React\Redis\Factory;
use React\Promise;

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

/**
* @doesNotPerformAssertions
*/
public function testCtor()
{
$this->factory = new Factory($this->loop);
Expand Down
56 changes: 30 additions & 26 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace Clue\Tests\React\Redis;

use Clue\React\Block;
use Clue\React\Redis\Client;
use Clue\React\Redis\Factory;
use Clue\React\Redis\StreamingClient;
use React\EventLoop\StreamSelectLoop;
use React\Promise\Deferred;
use React\Stream\Stream;
use React\Stream\DuplexResourceStream;

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

$this->loop = new React\EventLoop\StreamSelectLoop();
$this->loop = new StreamSelectLoop();
$this->factory = new Factory($this->loop);
$this->client = $this->createClient($uri);
}
Expand Down Expand Up @@ -55,10 +58,10 @@ public function testPipeline()
{
$client = $this->client;

$client->set('a', 1)->then($this->expectCallableOnce('OK'));
$client->incr('a')->then($this->expectCallableOnce(2));
$client->incr('a')->then($this->expectCallableOnce(3));
$promise = $client->get('a')->then($this->expectCallableOnce('3'));
$client->set('a', 1)->then($this->expectCallableOnceWith('OK'));
$client->incr('a')->then($this->expectCallableOnceWith(2));
$client->incr('a')->then($this->expectCallableOnceWith(3));
$promise = $client->get('a')->then($this->expectCallableOnceWith('3'));

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

$this->setExpectedException('Exception');
if (method_exists($this, 'expectException')) {
$this->expectException('Exception');
} else {
$this->setExpectedException('Exception');
}
Block\await($promise, $this->loop);
}

public function testMultiExecEmpty()
{
$this->client->multi()->then($this->expectCallableOnce('OK'));
$promise = $this->client->exec()->then($this->expectCallableOnce(array()));
$this->client->multi()->then($this->expectCallableOnceWith('OK'));
$promise = $this->client->exec()->then($this->expectCallableOnceWith(array()));

Block\await($promise, $this->loop);
}
Expand All @@ -83,12 +90,12 @@ public function testMultiExecQueuedExecHasValues()
{
$client = $this->client;

$client->multi()->then($this->expectCallableOnce('OK'));
$client->set('b', 10)->then($this->expectCallableOnce('QUEUED'));
$client->expire('b', 20)->then($this->expectCallableOnce('QUEUED'));
$client->incrBy('b', 2)->then($this->expectCallableOnce('QUEUED'));
$client->ttl('b')->then($this->expectCallableOnce('QUEUED'));
$promise = $client->exec()->then($this->expectCallableOnce(array('OK', 1, 12, 20)));
$client->multi()->then($this->expectCallableOnceWith('OK'));
$client->set('b', 10)->then($this->expectCallableOnceWith('QUEUED'));
$client->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED'));
$client->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED'));
$client->ttl('b')->then($this->expectCallableOnceWith('QUEUED'));
$promise = $client->exec()->then($this->expectCallableOnceWith(array('OK', 1, 12, 20)));

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

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

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

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

$this->setExpectedException('Exception');
if (method_exists($this, 'expectException')) {
$this->expectException('Exception');
} else {
$this->setExpectedException('Exception');
}
Block\await($promise, $this->loop);
}

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

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

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

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

return new StreamingClient($stream);
}

protected function createServer($response)
{
$port = 1337;
$cmd = 'echo -e "' . str_replace("\r\n", '\r\n', $response) . '" | nc -lC ' . $port;

}
}
17 changes: 13 additions & 4 deletions tests/StreamingClientTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Redis;

use Clue\React\Redis\StreamingClient;
use Clue\Redis\Protocol\Parser\ParserException;
use Clue\Redis\Protocol\Model\IntegerReply;
Expand Down Expand Up @@ -73,6 +75,9 @@ public function testReceiveThrowMessageEmitsErrorEvent()
$this->stream->emit('data', array('message'));
}

/**
* @doesNotPerformAssertions
*/
public function testDefaultCtor()
{
$client = new StreamingClient($this->stream);
Expand All @@ -87,7 +92,7 @@ public function testPingPong()
$this->client->handleMessage(new BulkReply('PONG'));

$this->expectPromiseResolve($promise);
$promise->then($this->expectCallableOnce('PONG'));
$promise->then($this->expectCallableOnceWith('PONG'));
}

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

$this->expectPromiseReject($promise);
$promise->then(null, $this->expectCallableOnce($err));
$promise->then(null, $this->expectCallableOnceWith($err));
}

public function testClosingClientRejectsAllRemainingRequests()
Expand Down Expand Up @@ -146,7 +151,7 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
$this->assertEquals(0, $closed);

$this->client->handleMessage(new BulkReply('PONG'));
$promise->then($this->expectCallableOnce('PONG'));
$promise->then($this->expectCallableOnceWith('PONG'));
$this->assertEquals(1, $closed);
}

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

public function testReceivingUnexpectedMessageThrowsException()
{
$this->setExpectedException('UnderflowException');
if (method_exists($this, 'expectException')) {
$this->expectException('UnderflowException');
} else {
$this->setExpectedException('UnderflowException');
}
$this->client->handleMessage(new BulkReply('PONG'));
}

Expand Down
48 changes: 15 additions & 33 deletions tests/bootstrap.php → tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
<?php

(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);
namespace Clue\Tests\React\Redis;

class TestCase extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
protected function expectCallableOnce()
{
$mock = $this->createCallableMock();


if (func_num_args() > 0) {
$mock
->expects($this->once())
->method('__invoke')
->with($this->equalTo(func_get_arg(0)));
} else {
$mock
->expects($this->once())
->method('__invoke');
}
$mock
->expects($this->once())
->method('__invoke');

return $mock;
}

protected function expectCallableNever()
protected function expectCallableOnceWith($argument)
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
->expects($this->once())
->method('__invoke')
->with($argument);

return $mock;
}

protected function expectCallableOnceParameter($type)
protected function expectCallableNever()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf($type));
->expects($this->never())
->method('__invoke');

return $mock;
}

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

protected function expectPromiseResolve($promise)
Expand Down Expand Up @@ -81,11 +71,3 @@ protected function expectPromiseReject($promise)
return $promise;
}
}

class CallableStub
{
public function __invoke()
{
}
}