From f7a6221503e4d71dac93bc4a86f7447f3ada024c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 10 Feb 2019 21:53:01 +0100 Subject: [PATCH 1/2] Forward compatibility with PHPUnit 7 and PHPUnit 6 --- composer.json | 2 +- tests/FactoryTest.php | 3 +++ tests/FunctionalTest.php | 12 ++++++++++-- tests/StreamingClientTest.php | 9 ++++++++- tests/bootstrap.php | 4 +++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 3111686..1e95d7f 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,6 @@ }, "require-dev": { "clue/block-react": "^1.1", - "phpunit/phpunit": "^5.0 || ^4.8" + "phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35" } } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 5d89786..5882678 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -16,6 +16,9 @@ public function setUp() $this->factory = new Factory($this->loop, $this->connector); } + /** + * @doesNotPerformAssertions + */ public function testCtor() { $this->factory = new Factory($this->loop); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 379c2cf..78d2368 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -67,7 +67,11 @@ 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); } @@ -131,7 +135,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); } diff --git a/tests/StreamingClientTest.php b/tests/StreamingClientTest.php index 58a5fc5..66e8ad8 100644 --- a/tests/StreamingClientTest.php +++ b/tests/StreamingClientTest.php @@ -73,6 +73,9 @@ public function testReceiveThrowMessageEmitsErrorEvent() $this->stream->emit('data', array('message')); } + /** + * @doesNotPerformAssertions + */ public function testDefaultCtor() { $client = new StreamingClient($this->stream); @@ -160,7 +163,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')); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3083fbc..b77e034 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,8 +1,10 @@ Date: Sat, 23 Feb 2019 11:44:35 +0100 Subject: [PATCH 2/2] Clean up test suite (namespaces etc.) --- composer.json | 3 ++ phpunit.xml.dist | 2 +- tests/FactoryTest.php | 2 ++ tests/FunctionalTest.php | 44 ++++++++++++------------- tests/StreamingClientTest.php | 8 +++-- tests/{bootstrap.php => TestCase.php} | 46 ++++++++------------------- 6 files changed, 44 insertions(+), 61 deletions(-) rename tests/{bootstrap.php => TestCase.php} (63%) diff --git a/composer.json b/composer.json index 1e95d7f..9213724 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,9 @@ "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": "^7.0 || ^6.0 || ^5.0 || ^4.8.35" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c3fc9c0..75fec35 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ -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); } @@ -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); } @@ -77,8 +80,8 @@ public function testInvalidCommand() 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); } @@ -87,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); } @@ -111,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); @@ -150,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); } @@ -170,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; - - } } diff --git a/tests/StreamingClientTest.php b/tests/StreamingClientTest.php index 66e8ad8..d9d437b 100644 --- a/tests/StreamingClientTest.php +++ b/tests/StreamingClientTest.php @@ -1,5 +1,7 @@ client->handleMessage(new BulkReply('PONG')); $this->expectPromiseResolve($promise); - $promise->then($this->expectCallableOnce('PONG')); + $promise->then($this->expectCallableOnceWith('PONG')); } public function testMonitorCommandIsNotSupported() @@ -109,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() @@ -149,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); } diff --git a/tests/bootstrap.php b/tests/TestCase.php similarity index 63% rename from tests/bootstrap.php rename to tests/TestCase.php index b77e034..81aeda4 100644 --- a/tests/bootstrap.php +++ b/tests/TestCase.php @@ -1,57 +1,45 @@ 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) @@ -83,11 +71,3 @@ protected function expectPromiseReject($promise) return $promise; } } - -class CallableStub -{ - public function __invoke() - { - } -} -