Skip to content

Commit

Permalink
Update test suite and remove legacy PHPUnit workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed May 31, 2024
1 parent 5293e14 commit add4324
Show file tree
Hide file tree
Showing 24 changed files with 511 additions and 548 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"react/stream": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7",
"phpunit/phpunit": "^9.6 || ^7.5",
"react/async": "^4 || ^3",
"react/promise-stream": "^1.4",
"react/promise-timer": "^1.10"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.legacy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- PHPUnit configuration file with old format for legacy PHPUnit -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
Expand Down
5 changes: 3 additions & 2 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace React\Tests\Socket;

use React\EventLoop\LoopInterface;
use React\Socket\Connection;

class ConnectionTest extends TestCase
{
public function testCloseConnectionWillCloseSocketResource()
{
$resource = fopen('php://memory', 'r+');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$connection = new Connection($resource, $loop);
$connection->close();
Expand All @@ -20,7 +21,7 @@ public function testCloseConnectionWillCloseSocketResource()
public function testCloseConnectionWillRemoveResourceFromLoopBeforeClosingResource()
{
$resource = fopen('php://memory', 'r+');
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$loop->expects($this->once())->method('addWriteStream')->with($resource);

$onRemove = null;
Expand Down
51 changes: 27 additions & 24 deletions tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace React\Tests\Socket;

use React\Socket\Connector;
use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\LoopInterface;
use React\Promise\Promise;
use React\Socket\Connector;
use React\Socket\ConnectorInterface;

class ConnectorTest extends TestCase
{
Expand All @@ -19,12 +22,12 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
$ref->setAccessible(true);
$loop = $ref->getValue($connectors['tcp']);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
$this->assertInstanceOf(LoopInterface::class, $loop);
}

public function testConstructWithLoopAssignsGivenLoop()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$connector = new Connector([], $loop);

Expand All @@ -36,12 +39,12 @@ public function testConstructWithLoopAssignsGivenLoop()
$ref->setAccessible(true);
$loop = $ref->getValue($connectors['tcp']);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
$this->assertInstanceOf(LoopInterface::class, $loop);
}

public function testConstructWithContextAssignsGivenContext()
{
$tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$tcp = $this->createMock(ConnectorInterface::class);

$connector = new Connector([
'tcp' => $tcp,
Expand All @@ -58,10 +61,10 @@ public function testConstructWithContextAssignsGivenContext()

public function testConnectorUsesTcpAsDefaultScheme()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$promise = new Promise(function () { });
$tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$tcp = $this->createMock(ConnectorInterface::class);
$tcp->expects($this->once())->method('connect')->with('127.0.0.1:80')->willReturn($promise);

$connector = new Connector([
Expand All @@ -73,10 +76,10 @@ public function testConnectorUsesTcpAsDefaultScheme()

public function testConnectorPassedThroughHostnameIfDnsIsDisabled()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$promise = new Promise(function () { });
$tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$tcp = $this->createMock(ConnectorInterface::class);
$tcp->expects($this->once())->method('connect')->with('tcp://google.com:80')->willReturn($promise);

$connector = new Connector([
Expand All @@ -89,88 +92,88 @@ public function testConnectorPassedThroughHostnameIfDnsIsDisabled()

public function testConnectorWithUnknownSchemeAlwaysFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$connector = new Connector([], $loop);

$promise = $connector->connect('unknown://google.com:80');

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
\RuntimeException::class,
'No connector available for URI scheme "unknown" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
));
}

public function testConnectorWithDisabledTcpDefaultSchemeAlwaysFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$connector = new Connector([
'tcp' => false
], $loop);

$promise = $connector->connect('google.com:80');

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
\RuntimeException::class,
'No connector available for URI scheme "tcp" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
));
}

public function testConnectorWithDisabledTcpSchemeAlwaysFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$connector = new Connector([
'tcp' => false
], $loop);

$promise = $connector->connect('tcp://google.com:80');

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
\RuntimeException::class,
'No connector available for URI scheme "tcp" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
));
}

public function testConnectorWithDisabledTlsSchemeAlwaysFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$connector = new Connector([
'tls' => false
], $loop);

$promise = $connector->connect('tls://google.com:443');

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
\RuntimeException::class,
'No connector available for URI scheme "tls" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
));
}

public function testConnectorWithDisabledUnixSchemeAlwaysFails()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);
$connector = new Connector([
'unix' => false
], $loop);

$promise = $connector->connect('unix://demo.sock');

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
\RuntimeException::class,
'No connector available for URI scheme "unix" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
));
}

public function testConnectorUsesGivenResolverInstance()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$promise = new Promise(function () { });
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver = $this->createMock(ResolverInterface::class);
$resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);

$connector = new Connector([
Expand All @@ -183,14 +186,14 @@ public function testConnectorUsesGivenResolverInstance()

public function testConnectorUsesResolvedHostnameIfDnsIsUsed()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop = $this->createMock(LoopInterface::class);

$promise = new Promise(function ($resolve) { $resolve('127.0.0.1'); });
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver = $this->createMock(ResolverInterface::class);
$resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn($promise);

$promise = new Promise(function () { });
$tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$tcp = $this->createMock(ConnectorInterface::class);
$tcp->expects($this->once())->method('connect')->with('tcp://127.0.0.1:80?hostname=google.com')->willReturn($promise);

$connector = new Connector([
Expand Down
Loading

0 comments on commit add4324

Please sign in to comment.