Skip to content

Commit

Permalink
Clean up test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 28, 2020
1 parent b3441f7 commit 9e15ad3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 56 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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 colors="true" bootstrap="./tests/bootstrap.php">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Socks Test Suite">
<directory>./tests/</directory>
Expand Down
10 changes: 6 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Client;
use React\Promise\Promise;
use Clue\React\Socks\Server;
Expand All @@ -16,7 +18,7 @@ class ClientTest extends TestCase

public function setUp()
{
$this->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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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());
Expand Down
30 changes: 16 additions & 14 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Client;
use Clue\React\Socks\Server;
use Clue\React\Block;
Expand All @@ -20,9 +22,9 @@ class FunctionalTest extends TestCase

public function setUp()
{
$this->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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
20 changes: 11 additions & 9 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Server;
use React\Promise\Promise;
use React\Promise\Timer\TimeoutException;
Expand Down Expand Up @@ -138,43 +140,43 @@ public function provideConnectionErrors()
{
return array(
array(
new RuntimeException('', SOCKET_EACCES),
new \RuntimeException('', SOCKET_EACCES),
Server::ERROR_NOT_ALLOWED_BY_RULESET
),
array(
new RuntimeException('', SOCKET_ENETUNREACH),
new \RuntimeException('', SOCKET_ENETUNREACH),
Server::ERROR_NETWORK_UNREACHABLE
),
array(
new RuntimeException('', SOCKET_EHOSTUNREACH),
new \RuntimeException('', SOCKET_EHOSTUNREACH),
Server::ERROR_HOST_UNREACHABLE,
),
array(
new RuntimeException('', SOCKET_ECONNREFUSED),
new \RuntimeException('', SOCKET_ECONNREFUSED),
Server::ERROR_CONNECTION_REFUSED
),
array(
new RuntimeException('Connection refused'),
new \RuntimeException('Connection refused'),
Server::ERROR_CONNECTION_REFUSED
),
array(
new RuntimeException('', SOCKET_ETIMEDOUT),
new \RuntimeException('', SOCKET_ETIMEDOUT),
Server::ERROR_TTL
),
array(
new TimeoutException(1.0),
Server::ERROR_TTL
),
array(
new RuntimeException(),
new \RuntimeException(),
Server::ERROR_GENERAL
)
);
}

/**
* @dataProvider provideConnectionErrors
* @param Exception $error
* @param \Exception $error
* @param int $expectedCode
*/
public function testConnectWillReturnMappedSocks5ErrorCodeFromConnector($error, $expectedCode)
Expand Down Expand Up @@ -342,7 +344,7 @@ public function testHandleSocks5ConnectionWithConnectorRefusedWillReturnReturnRe
{
$connection = $this->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);

Expand Down
18 changes: 10 additions & 8 deletions tests/StreamReaderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\StreamReader;

class StreamReaderTest extends TestCase
Expand All @@ -13,7 +15,7 @@ public function setUp()

public function testReadByteAssertCorrect()
{
$this->reader->readByteAssert(0x01)->then($this->expectCallableOnce(0x01));
$this->reader->readByteAssert(0x01)->then($this->expectCallableOnceWith(0x01));

$this->reader->write("\x01");
}
Expand All @@ -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');
Expand All @@ -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');

Expand Down
26 changes: 6 additions & 20 deletions tests/bootstrap.php → tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<?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\Socks;

class TestCase extends PHPUnit\Framework\TestCase
class TestCase extends \PHPUnit\Framework\TestCase
{
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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -91,10 +84,3 @@ protected function expectPromiseReject($promise)
return $promise;
}
}

class CallableStub
{
public function __invoke()
{
}
}

0 comments on commit 9e15ad3

Please sign in to comment.