From 9e15ad353f52ee83023365752bfb94bc54e3c2de Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 28 May 2020 12:21:18 +0200 Subject: [PATCH] Clean up test suite --- composer.json | 3 +++ phpunit.xml.dist | 2 +- tests/ClientTest.php | 10 +++++---- tests/FunctionalTest.php | 30 ++++++++++++++------------- tests/ServerTest.php | 20 ++++++++++-------- tests/StreamReaderTest.php | 18 +++++++++------- tests/{bootstrap.php => TestCase.php} | 26 ++++++----------------- 7 files changed, 53 insertions(+), 56 deletions(-) rename tests/{bootstrap.php => TestCase.php} (75%) diff --git a/composer.json b/composer.json index 759a252..0386680 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,9 @@ "autoload": { "psr-4": {"Clue\\React\\Socks\\": "src/"} }, + "autoload-dev": { + "psr-4": { "Clue\\Tests\\React\\Socks\\": "tests/" } + }, "require": { "php": ">=5.3", "react/promise": "^2.1 || ^1.2", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d451dff..35de217 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ - + ./tests/ diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2ddc7a0..2ff81ab 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,5 +1,7 @@ loop = React\EventLoop\Factory::create(); + $this->loop = \React\EventLoop\Factory::create(); $this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $this->client = new Client('127.0.0.1:1080', $this->connector); } @@ -162,7 +164,7 @@ public function testCreateWithInvalidPortDoesNotConnect() public function testConnectorRejectsWillRejectConnection() { - $promise = \React\Promise\reject(new RuntimeException()); + $promise = \React\Promise\reject(new \RuntimeException()); $this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise); @@ -261,7 +263,7 @@ public function testEmitConnectionErrorDuringSessionWillRejectConnection() $promise = $this->client->connect('google.com:80'); - $stream->emit('error', array(new RuntimeException())); + $stream->emit('error', array(new \RuntimeException())); $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', @@ -549,7 +551,7 @@ public function testConnectionErrorShouldNotCreateGarbageCycles() gc_collect_cycles(); $promise = $this->client->connect('google.com:80'); - $deferred->reject(new RuntimeException()); + $deferred->reject(new \RuntimeException()); unset($deferred, $promise); $this->assertEquals(0, gc_collect_cycles()); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index bce25f7..5a47b42 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -1,5 +1,7 @@ loop = React\EventLoop\Factory::create(); + $this->loop = \React\EventLoop\Factory::create(); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $address = $socket->getAddress(); if (strpos($address, '://') === false) { $address = 'tcp://' . $address; @@ -196,7 +198,7 @@ public function testConnectionAuthenticationFromUri() { $this->server = new Server($this->loop, null, array('name' => 'pass')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -219,7 +221,7 @@ public function testConnectionAuthenticationCallback() return true; }); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -239,7 +241,7 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen return true; }); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -254,7 +256,7 @@ public function testConnectionAuthenticationFromUriEncoded() { $this->server = new Server($this->loop, null, array('name' => 'p@ss:w0rd')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -268,7 +270,7 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword() { $this->server = new Server($this->loop, null, array('empty' => '')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -282,7 +284,7 @@ public function testConnectionAuthenticationEmptyPassword() { $this->server = new Server($this->loop, null, array('user' => '')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -303,7 +305,7 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4() { $this->server = new Server($this->loop, null, array('name' => 'pass')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -316,7 +318,7 @@ public function testConnectionInvalidNoAuthentication() { $this->server = new Server($this->loop, null, array('name' => 'pass')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -329,7 +331,7 @@ public function testConnectionInvalidAuthenticationMismatch() { $this->server = new Server($this->loop, null, array('name' => 'pass')); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -344,7 +346,7 @@ public function testConnectionInvalidAuthenticatorReturnsFalse() return false; }); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -359,7 +361,7 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal return \React\Promise\resolve(false); }); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -374,7 +376,7 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseRejected() return \React\Promise\reject(); }); - $socket = new React\Socket\Server(0, $this->loop); + $socket = new \React\Socket\Server(0, $this->loop); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index b53b357..4b6196a 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -1,5 +1,7 @@ getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end', 'write'))->getMock(); - $promise = \React\Promise\reject(new RuntimeException('Connection refused')); + $promise = \React\Promise\reject(new \RuntimeException('Connection refused')); $this->connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise); diff --git a/tests/StreamReaderTest.php b/tests/StreamReaderTest.php index a2a0d95..458cbbe 100644 --- a/tests/StreamReaderTest.php +++ b/tests/StreamReaderTest.php @@ -1,5 +1,7 @@ reader->readByteAssert(0x01)->then($this->expectCallableOnce(0x01)); + $this->reader->readByteAssert(0x01)->then($this->expectCallableOnceWith(0x01)); $this->reader->write("\x01"); } @@ -27,14 +29,14 @@ public function testReadByteAssertInvalid() public function testReadStringNull() { - $this->reader->readStringNull()->then($this->expectCallableOnce('hello')); + $this->reader->readStringNull()->then($this->expectCallableOnceWith('hello')); $this->reader->write("hello\x00"); } public function testReadStringLength() { - $this->reader->readLength(5)->then($this->expectCallableOnce('hello')); + $this->reader->readLength(5)->then($this->expectCallableOnceWith('hello')); $this->reader->write('he'); $this->reader->write('ll'); @@ -47,17 +49,17 @@ public function testReadBuffered() { $this->reader->write('hello'); - $this->reader->readLength(5)->then($this->expectCallableOnce('hello')); + $this->reader->readLength(5)->then($this->expectCallableOnceWith('hello')); $this->assertEquals('', $this->reader->getBuffer()); } public function testSequence() { - $this->reader->readByte()->then($this->expectCallableOnce(ord('h'))); - $this->reader->readByteAssert(ord('e'))->then($this->expectCallableOnce(ord('e'))); - $this->reader->readLength(4)->then($this->expectCallableOnce('llo ')); - $this->reader->readBinary(array('w'=>'C', 'o' => 'C'))->then($this->expectCallableOnce(array('w' => ord('w'), 'o' => ord('o')))); + $this->reader->readByte()->then($this->expectCallableOnceWith(ord('h'))); + $this->reader->readByteAssert(ord('e'))->then($this->expectCallableOnceWith(ord('e'))); + $this->reader->readLength(4)->then($this->expectCallableOnceWith('llo ')); + $this->reader->readBinary(array('w'=>'C', 'o' => 'C'))->then($this->expectCallableOnceWith(array('w' => ord('w'), 'o' => ord('o')))); $this->reader->write('hello world'); diff --git a/tests/bootstrap.php b/tests/TestCase.php similarity index 75% rename from tests/bootstrap.php rename to tests/TestCase.php index e320699..d2f1a17 100644 --- a/tests/bootstrap.php +++ b/tests/TestCase.php @@ -1,23 +1,16 @@ 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; } @@ -59,7 +52,7 @@ protected function expectCallableOnceWithException($class, $message, $code) */ protected function createCallableMock() { - return $this->getMockBuilder('CallableStub')->getMock(); + return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); } protected function expectPromiseResolve($promise) @@ -91,10 +84,3 @@ protected function expectPromiseReject($promise) return $promise; } } - -class CallableStub -{ - public function __invoke() - { - } -}