diff --git a/composer.json b/composer.json index 3111686..9213724 100644 --- a/composer.json +++ b/composer.json @@ -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" } } 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 @@ -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..d3a81f9 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -1,10 +1,13 @@ 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); } @@ -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); } @@ -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); } @@ -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); @@ -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); } @@ -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); } @@ -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; - - } } diff --git a/tests/StreamingClientTest.php b/tests/StreamingClientTest.php index 58a5fc5..d9d437b 100644 --- a/tests/StreamingClientTest.php +++ b/tests/StreamingClientTest.php @@ -1,5 +1,7 @@ stream->emit('data', array('message')); } + /** + * @doesNotPerformAssertions + */ public function testDefaultCtor() { $client = new StreamingClient($this->stream); @@ -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() @@ -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() @@ -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); } @@ -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')); } diff --git a/tests/bootstrap.php b/tests/TestCase.php similarity index 60% rename from tests/bootstrap.php rename to tests/TestCase.php index 3083fbc..81aeda4 100644 --- a/tests/bootstrap.php +++ b/tests/TestCase.php @@ -1,55 +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) @@ -81,11 +71,3 @@ protected function expectPromiseReject($promise) return $promise; } } - -class CallableStub -{ - public function __invoke() - { - } -} -