From add432403565a050303492a568ef567cfcb32f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 23 May 2024 22:14:44 +0200 Subject: [PATCH] Update test suite and remove legacy PHPUnit workarounds --- composer.json | 2 +- phpunit.xml.legacy | 2 +- tests/ConnectionTest.php | 5 +- tests/ConnectorTest.php | 51 ++-- tests/DnsConnectorTest.php | 80 +++---- tests/FdServerTest.php | 79 +++---- tests/FixedUriConnectorTest.php | 3 +- tests/FunctionalConnectorTest.php | 4 +- tests/FunctionalSecureServerTest.php | 42 ++-- tests/FunctionalTcpServerTest.php | 44 ++-- tests/HappyEyeBallsConnectionBuilderTest.php | 231 ++++++++++--------- tests/HappyEyeBallsConnectorTest.php | 88 +++---- tests/IntegrationTest.php | 18 +- tests/LimitingServerTest.php | 30 +-- tests/SecureConnectorTest.php | 64 ++--- tests/SecureServerTest.php | 51 ++-- tests/SocketServerTest.php | 27 +-- tests/Stub/CallableStub.php | 10 - tests/TcpConnectorTest.php | 57 +++-- tests/TcpServerTest.php | 28 +-- tests/TestCase.php | 53 +---- tests/TimeoutConnectorTest.php | 48 ++-- tests/UnixConnectorTest.php | 9 +- tests/UnixServerTest.php | 33 ++- 24 files changed, 511 insertions(+), 548 deletions(-) delete mode 100644 tests/Stub/CallableStub.php diff --git a/composer.json b/composer.json index 87bd8a1b..3c7690a4 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy index a018d7ab..00868603 100644 --- a/phpunit.xml.legacy +++ b/phpunit.xml.legacy @@ -2,7 +2,7 @@ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 49d53e26..2ef1e5ce 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Socket\Connection; class ConnectionTest extends TestCase @@ -9,7 +10,7 @@ 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(); @@ -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; diff --git a/tests/ConnectorTest.php b/tests/ConnectorTest.php index 039d7cb1..dd2f87bf 100644 --- a/tests/ConnectorTest.php +++ b/tests/ConnectorTest.php @@ -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 { @@ -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); @@ -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, @@ -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([ @@ -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([ @@ -89,13 +92,13 @@ 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) )); @@ -103,7 +106,7 @@ public function testConnectorWithUnknownSchemeAlwaysFails() public function testConnectorWithDisabledTcpDefaultSchemeAlwaysFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new Connector([ 'tcp' => false ], $loop); @@ -111,7 +114,7 @@ public function testConnectorWithDisabledTcpDefaultSchemeAlwaysFails() $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) )); @@ -119,7 +122,7 @@ public function testConnectorWithDisabledTcpDefaultSchemeAlwaysFails() public function testConnectorWithDisabledTcpSchemeAlwaysFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new Connector([ 'tcp' => false ], $loop); @@ -127,7 +130,7 @@ public function testConnectorWithDisabledTcpSchemeAlwaysFails() $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) )); @@ -135,7 +138,7 @@ public function testConnectorWithDisabledTcpSchemeAlwaysFails() public function testConnectorWithDisabledTlsSchemeAlwaysFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new Connector([ 'tls' => false ], $loop); @@ -143,7 +146,7 @@ public function testConnectorWithDisabledTlsSchemeAlwaysFails() $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) )); @@ -151,7 +154,7 @@ public function testConnectorWithDisabledTlsSchemeAlwaysFails() public function testConnectorWithDisabledUnixSchemeAlwaysFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new Connector([ 'unix' => false ], $loop); @@ -159,7 +162,7 @@ public function testConnectorWithDisabledUnixSchemeAlwaysFails() $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) )); @@ -167,10 +170,10 @@ public function testConnectorWithDisabledUnixSchemeAlwaysFails() 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([ @@ -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([ diff --git a/tests/DnsConnectorTest.php b/tests/DnsConnectorTest.php index da2df865..11d1f2f0 100644 --- a/tests/DnsConnectorTest.php +++ b/tests/DnsConnectorTest.php @@ -2,8 +2,10 @@ namespace React\Tests\Socket; +use React\Dns\Resolver\ResolverInterface; use React\Promise\Deferred; use React\Promise\Promise; +use React\Socket\ConnectorInterface; use React\Socket\DnsConnector; use function React\Promise\reject; use function React\Promise\resolve; @@ -19,8 +21,8 @@ class DnsConnectorTest extends TestCase */ public function setUpMocks() { - $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); - $this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $this->tcp = $this->createMock(ConnectorInterface::class); + $this->resolver = $this->createMock(ResolverInterface::class); $this->connector = new DnsConnector($this->tcp, $this->resolver); } @@ -28,7 +30,7 @@ public function setUpMocks() public function testPassByResolverIfGivenIp() { $this->resolver->expects($this->never())->method('resolve'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->once())->method('connect')->with('127.0.0.1:80')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('127.0.0.1:80'); @@ -37,8 +39,8 @@ public function testPassByResolverIfGivenIp() public function testPassThroughResolverIfGivenHost() { - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn(resolve('1.2.3.4')); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=google.com')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('google.com:80'); @@ -47,8 +49,8 @@ public function testPassThroughResolverIfGivenHost() public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() { - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(resolve('::1'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn(resolve('::1')); + $this->tcp->expects($this->once())->method('connect')->with('[::1]:80?hostname=google.com')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('google.com:80'); @@ -58,7 +60,7 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() public function testPassByResolverIfGivenCompleteUri() { $this->resolver->expects($this->never())->method('resolve'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->once())->method('connect')->with('scheme://127.0.0.1:80/path?query#fragment')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment'); @@ -67,8 +69,8 @@ public function testPassByResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenCompleteUri() { - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn(resolve('1.2.3.4')); + $this->tcp->expects($this->once())->method('connect')->with('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://google.com:80/path?query#fragment'); @@ -77,8 +79,8 @@ public function testPassThroughResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenExplicitHost() { - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->once())->method('resolve')->with('google.com')->willReturn(resolve('1.2.3.4')); + $this->tcp->expects($this->once())->method('connect')->with('scheme://1.2.3.4:80/?hostname=google.de')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de'); @@ -93,7 +95,7 @@ public function testRejectsImmediatelyIfUriIsInvalid() $promise = $this->connector->connect('////'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "////" is invalid (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); @@ -113,7 +115,7 @@ public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithRuntimeExce }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage()); $this->assertEquals(42, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -134,7 +136,7 @@ public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithInvalidArgu }); assert($exception instanceof \InvalidArgumentException); - $this->assertInstanceOf('InvalidArgumentException', $exception); + $this->assertInstanceOf(\InvalidArgumentException::class, $exception); $this->assertEquals('Invalid', $exception->getMessage()); $this->assertEquals(42, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -155,10 +157,10 @@ public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfT }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tcp://example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage()); $this->assertEquals(42, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } @@ -176,7 +178,7 @@ public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnec }); assert($exception instanceof \InvalidArgumentException); - $this->assertInstanceOf('InvalidArgumentException', $exception); + $this->assertInstanceOf(\InvalidArgumentException::class, $exception); $this->assertEquals('Invalid', $exception->getMessage()); $this->assertEquals(42, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -186,7 +188,7 @@ public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnec public function testSkipConnectionIfDnsFails() { $promise = reject(new \RuntimeException('DNS error')); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn($promise); + $this->resolver->expects($this->once())->method('resolve')->with('example.invalid')->willReturn($promise); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.invalid:80'); @@ -197,10 +199,10 @@ public function testSkipConnectionIfDnsFails() }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } @@ -208,7 +210,7 @@ public function testRejectionExceptionUsesPreviousExceptionIfDnsFails() { $exception = new \RuntimeException(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn(reject($exception)); + $this->resolver->expects($this->once())->method('resolve')->with('example.invalid')->willReturn(reject($exception)); $promise = $this->connector->connect('example.invalid:80'); @@ -220,7 +222,7 @@ public function testRejectionExceptionUsesPreviousExceptionIfDnsFails() public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection() { $pending = new Promise(function () { }, $this->expectCallableOnce()); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue($pending)); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($pending); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.com:80'); @@ -232,7 +234,7 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection() }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -243,7 +245,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp() { $pending = new Promise(function () { }, $this->expectCallableOnce()); $this->resolver->expects($this->never())->method('resolve'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($pending); $promise = $this->connector->connect('1.2.3.4:80'); $promise->cancel(); @@ -252,8 +254,8 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp() public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved() { $pending = new Promise(function () { }, $this->expectCallableOnce()); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(resolve('1.2.3.4')); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(resolve('1.2.3.4')); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($pending); $promise = $this->connector->connect('example.com:80'); $promise->cancel(); @@ -262,14 +264,14 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResol public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectionAfterDnsIsResolved() { $first = new Deferred(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($first->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($first->promise()); $pending = new Promise(function () { }, function () { throw new \RuntimeException( 'Connection cancelled', defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 ); }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($pending); $promise = $this->connector->connect('example.com:80'); $first->resolve('1.2.3.4'); @@ -282,10 +284,10 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectio }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tcp://example.com:80 failed: Connection cancelled', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } @@ -300,7 +302,7 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences( } $dns = new Deferred(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($dns->promise()); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.com:80'); @@ -324,10 +326,10 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences() } $dns = new Deferred(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($dns->promise()); $tcp = new Deferred(); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise()); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($tcp->promise()); $promise = $this->connector->connect('example.com:80'); @@ -351,13 +353,13 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg } $dns = new Deferred(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($dns->promise()); $tcp = new Deferred(); $dns->promise()->then(function () use ($tcp) { $tcp->reject(new \RuntimeException('Connection failed')); }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise()); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($tcp->promise()); $promise = $this->connector->connect('example.com:80'); @@ -383,7 +385,7 @@ public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences() $dns = new Deferred(function () { throw new \RuntimeException(); }); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($dns->promise()); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.com:80'); @@ -405,11 +407,11 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences } $dns = new Deferred(); - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); + $this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($dns->promise()); $tcp = new Promise(function () { }, function () { throw new \RuntimeException('Connection cancelled'); }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($tcp); $promise = $this->connector->connect('example.com:80'); $dns->resolve('1.2.3.4'); diff --git a/tests/FdServerTest.php b/tests/FdServerTest.php index e7d3c679..4ecd81e4 100644 --- a/tests/FdServerTest.php +++ b/tests/FdServerTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\FdServer; @@ -20,7 +21,7 @@ public function testCtorAddsResourceToLoop() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); new FdServer($fd, $loop); @@ -28,27 +29,23 @@ public function testCtorAddsResourceToLoop() public function testCtorThrowsForInvalidFd() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid FD number given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid FD number given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new FdServer(-1, $loop); } public function testCtorThrowsForInvalidUrl() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid FD number given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid FD number given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new FdServer('tcp://127.0.0.1:8080', $loop); } @@ -60,7 +57,7 @@ public function testCtorThrowsForUnknownFdWithoutCallingCustomErrorHandler() $fd = self::getNextFreeFd(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); $error = null; @@ -68,11 +65,9 @@ public function testCtorThrowsForUnknownFdWithoutCallingCustomErrorHandler() $error = $errstr; }); - $this->setExpectedException( - 'RuntimeException', - 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EBADF) . ' (EBADF)' : 'Bad file descriptor'), - defined('SOCKET_EBADF') ? SOCKET_EBADF : 9 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EBADF) . ' (EBADF)' : 'Bad file descriptor')); + $this->expectExceptionCode(defined('SOCKET_EBADF') ? SOCKET_EBADF : 9); try { new FdServer($fd, $loop); @@ -96,14 +91,12 @@ public function testCtorThrowsIfFdIsAFileAndNotASocket() $tmpfile = tmpfile(); assert($tmpfile !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); - $this->setExpectedException( - 'RuntimeException', - 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_ENOTSOCK) : 'Not a socket') . ' (ENOTSOCK)', - defined('SOCKET_ENOTSOCK') ? SOCKET_ENOTSOCK : 88 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_ENOTSOCK) : 'Not a socket') . ' (ENOTSOCK)'); + $this->expectExceptionCode(defined('SOCKET_ENOTSOCK') ? SOCKET_ENOTSOCK : 88); new FdServer($fd, $loop); } @@ -119,14 +112,12 @@ public function testCtorThrowsIfFdIsAConnectedSocketInsteadOfServerSocket() $client = stream_socket_client('tcp://' . stream_socket_get_name($socket, false)); assert($client !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addReadStream'); - $this->setExpectedException( - 'RuntimeException', - 'Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EISCONN) : 'Socket is connected') . ' (EISCONN)', - defined('SOCKET_EISCONN') ? SOCKET_EISCONN : 106 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Failed to listen on FD ' . $fd . ': ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EISCONN) : 'Socket is connected') . ' (EISCONN)'); + $this->expectExceptionCode(defined('SOCKET_EISCONN') ? SOCKET_EISCONN : 106); new FdServer($fd, $loop); } @@ -139,7 +130,7 @@ public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv4Socket() $fd = self::getNextFreeFd(); $socket = stream_socket_server('127.0.0.1:0'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new FdServer($fd, $loop); @@ -155,7 +146,7 @@ public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv4SocketGiv $fd = self::getNextFreeFd(); $socket = stream_socket_server('127.0.0.1:0'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new FdServer('php://fd/' . $fd, $loop); @@ -174,7 +165,7 @@ public function testGetAddressReturnsSameAddressAsOriginalSocketForIpv6Socket() $this->markTestSkipped('Listening on IPv6 not supported'); } - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new FdServer($fd, $loop); @@ -197,7 +188,7 @@ public function testGetAddressReturnsSameAddressAsOriginalSocketForUnixDomainSoc assert(is_resource($socket)); unlink(str_replace('unix://', '', stream_socket_get_name($socket, false))); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new FdServer($fd, $loop); @@ -214,7 +205,7 @@ public function testGetAddressReturnsNullAfterClose() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new FdServer($fd, $loop); $server->close(); @@ -232,7 +223,7 @@ public function testCloseRemovesResourceFromLoop() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new FdServer($fd, $loop); @@ -249,7 +240,7 @@ public function testCloseTwiceRemovesResourceFromLoopOnce() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new FdServer($fd, $loop); @@ -267,7 +258,7 @@ public function testResumeWithoutPauseIsNoOp() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); $server = new FdServer($fd, $loop); @@ -284,7 +275,7 @@ public function testPauseRemovesResourceFromLoop() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new FdServer($fd, $loop); @@ -301,7 +292,7 @@ public function testPauseAfterPauseIsNoOp() $socket = stream_socket_server('127.0.0.1:0'); assert($socket !== false); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new FdServer($fd, $loop); @@ -331,7 +322,7 @@ public function testServerEmitsConnectionEventForNewConnection() /** * @var ConnectionInterface $connection */ - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); fclose($client); $connection->close(); @@ -345,7 +336,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa } $listener = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream')->with($this->anything(), $this->callback(function ($cb) use (&$listener) { $listener = $cb; return true; @@ -379,7 +370,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa $this->assertLessThan(1, $time); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage()); diff --git a/tests/FixedUriConnectorTest.php b/tests/FixedUriConnectorTest.php index bdc1d770..b649b61a 100644 --- a/tests/FixedUriConnectorTest.php +++ b/tests/FixedUriConnectorTest.php @@ -2,13 +2,14 @@ namespace React\Tests\Socket; +use React\Socket\ConnectorInterface; use React\Socket\FixedUriConnector; class FixedUriConnectorTest extends TestCase { public function testWillInvokeGivenConnector() { - $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $base = $this->createMock(ConnectorInterface::class); $base->expects($this->once())->method('connect')->with('test')->willReturn('ret'); $connector = new FixedUriConnector('test', $base); diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index 117e827f..ad1aa992 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -30,7 +30,7 @@ public function connectionToTcpServerShouldSucceedWithLocalhost() $server->close(); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); $connection->close(); } @@ -149,7 +149,7 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo await(timeout($promise, self::TIMEOUT)); $this->fail(); } catch (\Exception $e) { - $this->assertInstanceOf('RuntimeException', $e); + $this->assertInstanceOf(\RuntimeException::class, $e); $this->assertEquals('Connection to ' . $uri . ' cancelled during TLS handshake (ECONNABORTED)', $e->getMessage()); } } diff --git a/tests/FunctionalSecureServerTest.php b/tests/FunctionalSecureServerTest.php index 4c91faec..b30bbbb5 100644 --- a/tests/FunctionalSecureServerTest.php +++ b/tests/FunctionalSecureServerTest.php @@ -3,6 +3,7 @@ namespace React\Tests\Socket; use React\Promise\Promise; +use React\Socket\Connection; use React\Socket\ConnectionInterface; use React\Socket\SecureConnector; use React\Socket\ServerInterface; @@ -32,7 +33,7 @@ public function testClientCanConnectToServer() /* @var ConnectionInterface $client */ $client = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $client); + $this->assertInstanceOf(ConnectionInterface::class, $client); $this->assertEquals($server->getAddress(), $client->getRemoteAddress()); $client->close(); @@ -61,7 +62,7 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() /* @var ConnectionInterface $client */ $client = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\Connection', $client); + $this->assertInstanceOf(Connection::class, $client); $this->assertTrue(isset($client->stream)); $meta = stream_get_meta_data($client->stream); @@ -96,7 +97,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien /* @var ConnectionInterface $client */ $client = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\Connection', $client); + $this->assertInstanceOf(Connection::class, $client); $this->assertTrue(isset($client->stream)); $meta = stream_get_meta_data($client->stream); @@ -123,7 +124,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe /* @var ConnectionInterface $client */ $client = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\Connection', $client); + $this->assertInstanceOf(Connection::class, $client); $this->assertTrue(isset($client->stream)); $meta = stream_get_meta_data($client->stream); @@ -161,7 +162,7 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien $this->markTestSkipped('TLS 1.0 not available on this system (' . $e->getMessage() . ')'); } - $this->assertInstanceOf('React\Socket\Connection', $client); + $this->assertInstanceOf(Connection::class, $client); $this->assertTrue(isset($client->stream)); $meta = stream_get_meta_data($client->stream); @@ -195,8 +196,8 @@ public function testServerEmitsConnectionForClientConnection() // both ends of the connection are represented by different instances of ConnectionInterface $this->assertCount(2, $both); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $both[0]); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $both[1]); + $this->assertInstanceOf(ConnectionInterface::class, $both[0]); + $this->assertInstanceOf(ConnectionInterface::class, $both[1]); $this->assertNotSame($both[0], $both[1]); // server side end has local server address and client end has remote server address @@ -484,7 +485,8 @@ public function testEmitsErrorForClientWithTlsVersionMismatch() ]); $promise = $connector->connect($server->getAddress()); - $this->setExpectedException('RuntimeException', 'handshake'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('handshake'); try { await(timeout($promise, self::TIMEOUT)); @@ -515,7 +517,7 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat $connection = await(timeout($peer, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); $server->close(); $connection->close(); @@ -533,7 +535,8 @@ public function testClientRejectsWithErrorForServerWithInvalidCertificate() ]); $promise = $connector->connect($server->getAddress()); - $this->setExpectedException('RuntimeException', 'handshake'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('handshake'); try { await(timeout($promise, self::TIMEOUT)); @@ -569,7 +572,8 @@ public function testServerEmitsErrorForClientWithInvalidCertificate() // ignore client-side exception } - $this->setExpectedException('RuntimeException', 'handshake'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('handshake'); try { await(timeout($peer, self::TIMEOUT)); @@ -598,7 +602,8 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase ]); $promise = $connector->connect($server->getAddress()); - $this->setExpectedException('RuntimeException', 'handshake'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('handshake'); try { await(timeout($promise, self::TIMEOUT)); @@ -628,7 +633,8 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph ]); $promise = $connector->connect($server->getAddress()); - $this->setExpectedException('RuntimeException', 'handshake'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('handshake'); try { await(timeout($promise, self::TIMEOUT)); @@ -703,7 +709,7 @@ public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() $error = await(timeout($errorEvent, self::TIMEOUT)); // Connection from tcp://127.0.0.1:39528 failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET) - $this->assertInstanceOf('RuntimeException', $error); + $this->assertInstanceOf(\RuntimeException::class, $error); $this->assertStringStartsWith('Connection from tcp://', $error->getMessage()); $this->assertStringEndsWith('failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET)', $error->getMessage()); $this->assertEquals(defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104, $error->getCode()); @@ -731,7 +737,7 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() $error = await(timeout($errorEvent, self::TIMEOUT)); // Connection from tcp://127.0.0.1:39528 failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET) - $this->assertInstanceOf('RuntimeException', $error); + $this->assertInstanceOf(\RuntimeException::class, $error); $this->assertStringStartsWith('Connection from tcp://', $error->getMessage()); $this->assertStringEndsWith('failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET)', $error->getMessage()); $this->assertEquals(defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104, $error->getCode()); @@ -753,7 +759,7 @@ public function testEmitsNothingIfPlaintextConnectionIsIdle() $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $connection = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -779,7 +785,7 @@ public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() $error = await(timeout($errorEvent, self::TIMEOUT)); - $this->assertInstanceOf('RuntimeException', $error); + $this->assertInstanceOf(\RuntimeException::class, $error); // OpenSSL error messages are version/platform specific // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:http request @@ -808,7 +814,7 @@ public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandsh $error = await(timeout($errorEvent, self::TIMEOUT)); - $this->assertInstanceOf('RuntimeException', $error); + $this->assertInstanceOf(\RuntimeException::class, $error); // OpenSSL error messages are version/platform specific // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:unknown protocol diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index c612e327..3a08b48c 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -99,7 +99,7 @@ public function testEmitsConnectionWithRemoteIp() $peer = await(timeout($peer, self::TIMEOUT)); await(sleep(0.0)); - $this->assertContainsString('127.0.0.1:', $peer); + $this->assertStringContainsString('127.0.0.1:', $peer); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -126,7 +126,7 @@ public function testEmitsConnectionWithLocalIp() $local = await(timeout($peer, self::TIMEOUT)); await(sleep(0.0)); - $this->assertContainsString('127.0.0.1:', $local); + $this->assertStringContainsString('127.0.0.1:', $local); $this->assertEquals($server->getAddress(), $local); $server->close(); @@ -156,7 +156,7 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $local = await(timeout($peer, self::TIMEOUT)); await(sleep(0.0)); - $this->assertContainsString('127.0.0.1:', $local); + $this->assertStringContainsString('127.0.0.1:', $local); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -182,7 +182,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() $peer = await(timeout($peer, self::TIMEOUT)); - $this->assertContainsString('127.0.0.1:', $peer); + $this->assertStringContainsString('127.0.0.1:', $peer); $server->close(); } @@ -288,7 +288,7 @@ public function testEmitsConnectionWithRemoteIpv6() $peer = await(timeout($peer, self::TIMEOUT)); await(sleep(0.0)); - $this->assertContainsString('[::1]:', $peer); + $this->assertStringContainsString('[::1]:', $peer); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -318,7 +318,7 @@ public function testEmitsConnectionWithLocalIpv6() $local = await(timeout($peer, self::TIMEOUT)); await(sleep(0.0)); - $this->assertContainsString('[::1]:', $local); + $this->assertStringContainsString('[::1]:', $local); $this->assertEquals($server->getAddress(), $local); $server->close(); @@ -389,41 +389,33 @@ public function testEmitsConnectionWithInheritedContextOptions() public function testFailsToListenOnInvalidUri() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI "tcp://///" given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI "tcp://///" given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new TcpServer('///'); } public function testFailsToListenOnUriWithoutPort() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI "tcp://127.0.0.1" given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI "tcp://127.0.0.1" given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new TcpServer('127.0.0.1'); } public function testFailsToListenOnUriWithWrongScheme() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI "udp://127.0.0.1:0" given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI "udp://127.0.0.1:0" given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new TcpServer('udp://127.0.0.1:0'); } public function testFailsToListenOnUriWIthHostname() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Given URI "tcp://localhost:8080" does not contain a valid host IP (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Given URI "tcp://localhost:8080" does not contain a valid host IP (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new TcpServer('localhost:8080'); } } diff --git a/tests/HappyEyeBallsConnectionBuilderTest.php b/tests/HappyEyeBallsConnectionBuilderTest.php index b63a623f..60b5ead4 100644 --- a/tests/HappyEyeBallsConnectionBuilderTest.php +++ b/tests/HappyEyeBallsConnectionBuilderTest.php @@ -2,10 +2,15 @@ namespace React\Tests\Socket; -use React\Promise\Promise; -use React\Socket\HappyEyeBallsConnectionBuilder; use React\Dns\Model\Message; +use React\Dns\Resolver\ResolverInterface; +use React\EventLoop\LoopInterface; +use React\EventLoop\TimerInterface; use React\Promise\Deferred; +use React\Promise\Promise; +use React\Promise\PromiseInterface; +use React\Socket\ConnectorInterface; +use React\Socket\HappyEyeBallsConnectionBuilder; use function React\Promise\reject; use function React\Promise\resolve; @@ -13,13 +18,13 @@ class HappyEyeBallsConnectionBuilderTest extends TestCase { public function testConnectWillResolveTwiceViaResolver() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -36,13 +41,13 @@ public function testConnectWillResolveTwiceViaResolver() public function testConnectWillRejectWhenBothDnsLookupsReject() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -63,24 +68,24 @@ public function testConnectWillRejectWhenBothDnsLookupsReject() $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 failed during DNS lookup: DNS lookup error', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testConnectWillRejectWhenBothDnsLookupsRejectWithDifferentMessages() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); $deferred = new Deferred(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -103,24 +108,24 @@ public function testConnectWillRejectWhenBothDnsLookupsRejectWithDifferentMessag $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 failed during DNS lookup. Last error for IPv6: DNS6 error. Previous error for IPv4: DNS4 error', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testConnectWillStartDelayTimerWhenIpv4ResolvesAndIpv6IsPending() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.05, $this->anything()); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -140,14 +145,14 @@ public function testConnectWillStartDelayTimerWhenIpv4ResolvesAndIpv6IsPending() public function testConnectWillStartConnectingWithAttemptTimerButWithoutResolutionTimerWhenIpv6ResolvesAndIpv4IsPending() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything()); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn(new Promise(function () { })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -168,17 +173,17 @@ public function testConnectWillStartConnectingWithAttemptTimerButWithoutResoluti public function testConnectWillStartConnectingAndWillStartNextConnectionWithNewAttemptTimerWhenNextAttemptTimerFiresWithIpv4StillPending() { $timer = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->exactly(2))->method('addTimer')->with(0.1, $this->callback(function ($cb) use (&$timer) { $timer = $cb; return true; })); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->willReturn(new Promise(function () { })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -202,17 +207,17 @@ public function testConnectWillStartConnectingAndWillStartNextConnectionWithNewA public function testConnectWillStartConnectingAndWillDoNothingWhenNextAttemptTimerFiresWithNoOtherIps() { $timer = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->callback(function ($cb) use (&$timer) { $timer = $cb; return true; })); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn(new Promise(function () { })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -235,16 +240,16 @@ public function testConnectWillStartConnectingAndWillDoNothingWhenNextAttemptTim public function testConnectWillStartConnectingWithAttemptTimerButWithoutResolutionTimerWhenIpv6ResolvesAndWillCancelAttemptTimerWhenIpv4Rejects() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn(new Promise(function () { })); $deferred = new Deferred(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -265,13 +270,13 @@ public function testConnectWillStartConnectingWithAttemptTimerButWithoutResoluti public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->withConsecutive( ['tcp://[::1]:80?hostname=reactphp.org'], ['tcp://127.0.0.1:80?hostname=reactphp.org'] @@ -280,7 +285,7 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4Res new Promise(function () { }) ); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -302,13 +307,13 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4Res public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenResolverReturnsMultipleIPAdresses() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(4))->method('connect')->withConsecutive( ['tcp://[::1]:80?hostname=reactphp.org'], ['tcp://127.0.0.1:80?hostname=reactphp.org'], @@ -321,7 +326,7 @@ public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenReso new Promise(function () { }) ); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -343,12 +348,12 @@ public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenReso public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->withConsecutive( ['tcp://[::1]:80?hostname=reactphp.org'], ['tcp://[::1]:80?hostname=reactphp.org'] @@ -357,7 +362,7 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6Resolv new Promise(function () { }) ); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -378,18 +383,18 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6Resolv public function testConnectWillStartConnectingAndWillStartNextConnectionWithoutNewAttemptTimerWhenNextAttemptTimerFiresAfterIpv4Rejected() { $timer = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->callback(function ($cb) use (&$timer) { $timer = $cb; return true; })); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->willReturn(new Promise(function () { })); $deferred = new Deferred(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -413,20 +418,20 @@ public function testConnectWillStartConnectingAndWillStartNextConnectionWithoutN public function testConnectWillStartAndCancelResolutionTimerAndStartAttemptTimerWhenIpv4ResolvesAndIpv6ResolvesAfterwardsAndStartConnectingToIpv6() { - $timerDelay = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $timerAttempt = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timerDelay = $this->createMock(TimerInterface::class); + $timerAttempt = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->exactly(2))->method('addTimer')->withConsecutive( [0.05, $this->anything()], [0.1, $this->anything()] )->willReturnOnConsecutiveCalls($timerDelay, $timerAttempt); $loop->expects($this->once())->method('cancelTimer')->with($timerDelay); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn(new Promise(function () { })); $deferred = new Deferred(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -447,16 +452,16 @@ public function testConnectWillStartAndCancelResolutionTimerAndStartAttemptTimer public function testConnectWillRejectWhenOnlyTcp6ConnectionRejectsAndCancelNextAttemptTimerImmediately() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn($deferred->promise()); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -482,24 +487,24 @@ public function testConnectWillRejectWhenOnlyTcp6ConnectionRejectsAndCancelNextA $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 failed: Last error for IPv6: Connection refused (ECONNREFUSED). Previous error for IPv4: DNS failed', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testConnectWillRejectWhenOnlyTcp4ConnectionRejectsAndWillNeverStartNextAttemptTimer() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://127.0.0.1:80?hostname=reactphp.org')->willReturn($deferred->promise()); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -525,26 +530,26 @@ public function testConnectWillRejectWhenOnlyTcp4ConnectionRejectsAndWillNeverSt $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 failed: Last error for IPv4: Connection refused (ECONNREFUSED). Previous error for IPv6: DNS failed', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testConnectWillRejectWhenAllConnectionsRejectAndCancelNextAttemptTimerImmediately() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->willReturn($deferred->promise()); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -570,23 +575,23 @@ public function testConnectWillRejectWhenAllConnectionsRejectAndCancelNextAttemp $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 failed: Connection refused (ECONNREFUSED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testConnectWillRejectWithMessageWithoutHostnameWhenAllConnectionsRejectAndCancelNextAttemptTimerImmediately() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->willReturnOnConsecutiveCalls( $deferred->promise(), reject(new \RuntimeException( @@ -595,7 +600,7 @@ public function testConnectWillRejectWithMessageWithoutHostnameWhenAllConnection )) ); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['localhost', Message::TYPE_AAAA], ['localhost', Message::TYPE_A] @@ -621,24 +626,24 @@ public function testConnectWillRejectWithMessageWithoutHostnameWhenAllConnection $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://localhost:80 failed: Last error for IPv4: Connection to tcp://127.0.0.1:80 failed: Connection refused (ECONNREFUSED). Previous error for IPv6: Connection to tcp://[::1]:80 failed: Connection refused (ECONNREFUSED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); } public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); $cancelled = 0; - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -669,7 +674,7 @@ public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups() $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 cancelled during DNS lookup (ECONNABORTED)', $exception->getMessage()); @@ -678,15 +683,15 @@ public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups() public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6LookupAndCancelDelayTimer() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->never())->method('connect'); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -711,7 +716,7 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6LookupAndC $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 cancelled during DNS lookup (ECONNABORTED)', $exception->getMessage()); @@ -720,19 +725,19 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6LookupAndC public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6ConnectionAttemptAndPendingIpv4LookupAndCancelAttemptTimer() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $cancelled = 0; - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80?hostname=reactphp.org')->willReturn(new Promise(function () { }, function () use (&$cancelled) { ++$cancelled; throw new \RuntimeException('Ignored message'); })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive( ['reactphp.org', Message::TYPE_AAAA], ['reactphp.org', Message::TYPE_A] @@ -757,7 +762,7 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6Connection $exception = $e; }); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertEquals('Connection to tcp://reactphp.org:80 cancelled (ECONNABORTED)', $exception->getMessage()); @@ -766,11 +771,11 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6Connection public function testResolveWillReturnResolvedPromiseWithEmptyListWhenDnsResolverFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->once())->method('resolveAll')->with('reactphp.org', Message::TYPE_A)->willReturn(reject(new \RuntimeException())); $uri = 'tcp://reactphp.org:80'; @@ -781,18 +786,18 @@ public function testResolveWillReturnResolvedPromiseWithEmptyListWhenDnsResolver $promise = $builder->resolve(Message::TYPE_A, $this->expectCallableNever()); - $this->assertInstanceof('React\Promise\PromiseInterface', $promise); + $this->assertInstanceof(PromiseInterface::class, $promise); $promise->then($this->expectCallableOnceWith([]), $this->expectCallableNever()); } public function testAttemptConnectionWillConnectViaConnectorToGivenIpWithPortAndHostnameFromUriParts() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://10.1.1.1:80?hostname=reactphp.org')->willReturn(new Promise(function () { })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->never())->method('resolveAll'); $uri = 'tcp://reactphp.org:80'; @@ -806,12 +811,12 @@ public function testAttemptConnectionWillConnectViaConnectorToGivenIpWithPortAnd public function testAttemptConnectionWillConnectViaConnectorToGivenIpv6WithAllUriParts() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80/path?test=yes&hostname=reactphp.org#start')->willReturn(new Promise(function () { })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->never())->method('resolveAll'); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; @@ -825,12 +830,12 @@ public function testAttemptConnectionWillConnectViaConnectorToGivenIpv6WithAllUr public function testCheckCallsRejectFunctionImmediateWithoutLeavingDanglingPromiseWhenConnectorRejectsImmediately() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80/path?test=yes&hostname=reactphp.org#start')->willReturn(reject(new \RuntimeException())); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->never())->method('resolveAll'); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; @@ -854,15 +859,15 @@ public function testCheckCallsRejectFunctionImmediateWithoutLeavingDanglingPromi public function testCleanUpCancelsAllPendingConnectionAttempts() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->exactly(2))->method('connect')->with('tcp://[::1]:80/path?test=yes&hostname=reactphp.org#start')->willReturnOnConsecutiveCalls( new Promise(function () { }, $this->expectCallableOnce()), new Promise(function () { }, $this->expectCallableOnce()) ); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->never())->method('resolveAll'); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; @@ -883,14 +888,14 @@ public function testCleanUpCancelsAllPendingConnectionAttempts() public function testCleanUpCancelsAllPendingConnectionAttemptsWithoutStartingNewAttemptsDueToCancellationRejection() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('tcp://[::1]:80/path?test=yes&hostname=reactphp.org#start')->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); })); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $resolver = $this->createMock(ResolverInterface::class); $resolver->expects($this->never())->method('resolveAll'); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; @@ -910,9 +915,9 @@ public function testCleanUpCancelsAllPendingConnectionAttemptsWithoutStartingNew public function testMixIpsIntoConnectQueueSometimesAssignsInOriginalOrder() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); + $connector = $this->createMock(ConnectorInterface::class); + $resolver = $this->createMock(ResolverInterface::class); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; $host = 'reactphp.org'; @@ -936,9 +941,9 @@ public function testMixIpsIntoConnectQueueSometimesAssignsInOriginalOrder() public function testMixIpsIntoConnectQueueSometimesAssignsInReverseOrder() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); - $resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); + $connector = $this->createMock(ConnectorInterface::class); + $resolver = $this->createMock(ResolverInterface::class); $uri = 'tcp://reactphp.org:80/path?test=yes#start'; $host = 'reactphp.org'; diff --git a/tests/HappyEyeBallsConnectorTest.php b/tests/HappyEyeBallsConnectorTest.php index 6d744f09..068aefc4 100644 --- a/tests/HappyEyeBallsConnectorTest.php +++ b/tests/HappyEyeBallsConnectorTest.php @@ -3,9 +3,13 @@ namespace React\Tests\Socket; use React\Dns\Model\Message; +use React\Dns\Resolver\ResolverInterface; +use React\EventLoop\LoopInterface; use React\EventLoop\StreamSelectLoop; use React\Promise\Deferred; use React\Promise\Promise; +use React\Socket\ConnectionInterface; +use React\Socket\ConnectorInterface; use React\Socket\HappyEyeBallsConnector; use function React\Promise\reject; use function React\Promise\resolve; @@ -24,9 +28,9 @@ class HappyEyeBallsConnectorTest extends TestCase public function setUpMocks() { $this->loop = new TimerSpeedUpEventLoop(new StreamSelectLoop()); - $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); - $this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->disableOriginalConstructor()->getMock(); - $this->connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $this->tcp = $this->createMock(ConnectorInterface::class); + $this->resolver = $this->createMock(ResolverInterface::class); + $this->connection = $this->createMock(ConnectionInterface::class); $this->connector = new HappyEyeBallsConnector($this->loop, $this->tcp, $this->resolver); } @@ -39,15 +43,15 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($connector); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testHappyFlow() { $first = new Deferred(); - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->willReturn($first->promise()); - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); - $this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(resolve($connection)); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->willReturn($first->promise()); + $connection = $this->createMock(ConnectionInterface::class); + $this->tcp->expects($this->exactly(1))->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn(resolve($connection)); $promise = $this->connector->connect('example.com:80'); $first->resolve(['1.2.3.4']); @@ -62,7 +66,7 @@ public function testHappyFlow() public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConnectionHasBeenEstablished() { - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); $lookupAttempts = [ reject(new \Exception('error')), resolve(['1.2.3.4', '5.6.7.8', '9.10.11.12']), @@ -72,12 +76,12 @@ public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConn resolve($connection), new Promise(function () {}, $this->expectCallableNever()), ]; - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->will($this->returnCallback(function () use (&$lookupAttempts) { + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->willReturnCallback(function () use (&$lookupAttempts) { return array_shift($lookupAttempts); - })); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->isType('string'))->will($this->returnCallback(function () use (&$connectionAttempts) { + }); + $this->tcp->expects($this->exactly(2))->method('connect')->with($this->isType('string'))->willReturnCallback(function () use (&$connectionAttempts) { return array_shift($connectionAttempts); - })); + }); $promise = $this->connector->connect('example.com:80'); @@ -93,7 +97,7 @@ public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConn public function testPassByResolverIfGivenIp() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(resolve(null))); + $this->tcp->expects($this->once())->method('connect')->with('127.0.0.1:80')->willReturn(resolve(null)); $this->connector->connect('127.0.0.1:80'); @@ -103,7 +107,7 @@ public function testPassByResolverIfGivenIp() public function testPassByResolverIfGivenIpv6() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->once())->method('connect')->with('[::1]:80')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('[::1]:80'); @@ -114,8 +118,8 @@ public function testPassByResolverIfGivenIpv6() public function testPassThroughResolverIfGivenHost() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(resolve(['1.2.3.4']))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('google.com', $this->anything())->willReturn(resolve(['1.2.3.4'])); + $this->tcp->expects($this->exactly(2))->method('connect')->with('1.2.3.4:80?hostname=google.com')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('google.com:80'); @@ -126,8 +130,8 @@ public function testPassThroughResolverIfGivenHost() public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(resolve(['::1']))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('google.com', $this->anything())->willReturn(resolve(['::1'])); + $this->tcp->expects($this->exactly(2))->method('connect')->with('[::1]:80?hostname=google.com')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('google.com:80'); @@ -139,7 +143,7 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() public function testPassByResolverIfGivenCompleteUri() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->once())->method('connect')->with('scheme://127.0.0.1:80/path?query#fragment')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment'); @@ -150,8 +154,8 @@ public function testPassByResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenCompleteUri() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(resolve(['1.2.3.4']))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('google.com', $this->anything())->willReturn(resolve(['1.2.3.4'])); + $this->tcp->expects($this->exactly(2))->method('connect')->with('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://google.com:80/path?query#fragment'); @@ -162,8 +166,8 @@ public function testPassThroughResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenExplicitHost() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(resolve(['1.2.3.4']))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('google.com', $this->anything())->willReturn(resolve(['1.2.3.4'])); + $this->tcp->expects($this->exactly(2))->method('connect')->with('scheme://1.2.3.4:80/?hostname=google.de')->willReturn(reject(new \Exception('reject'))); $promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de'); @@ -186,7 +190,7 @@ public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $i $this->returnValue(resolve($ipv6)), $this->returnValue($deferred->promise()) ); - $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->willReturn(reject(new \Exception('reject'))); $this->connector->connect('scheme://google.com:80/?hostname=google.com'); @@ -211,7 +215,7 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip $this->returnValue($deferred->promise()), $this->returnValue(resolve($ipv4)) ); - $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(reject(new \Exception('reject')))); + $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->willReturn(reject(new \Exception('reject'))); $this->connector->connect('scheme://google.com:80/?hostname=google.com'); @@ -230,7 +234,7 @@ public function testRejectsImmediatelyIfUriIsInvalid() $promise = $this->connector->connect('////'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "////" is invalid (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); @@ -240,7 +244,7 @@ public function testRejectsWithTcpConnectorRejectionIfGivenIp() { $promise = reject(new \RuntimeException('Connection failed')); $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($promise); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise); $promise = $this->connector->connect('1.2.3.4:80'); $this->loop->addTimer(0.5, function () use ($promise) { @@ -249,13 +253,14 @@ public function testRejectsWithTcpConnectorRejectionIfGivenIp() $this->throwRejection($promise); }); - $this->setExpectedException('RuntimeException', 'Connection failed'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection failed'); $this->loop->run(); } public function testSkipConnectionIfDnsFails() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.invalid'), $this->anything())->willReturn(reject(new \RuntimeException('DNS error'))); + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.invalid', $this->anything())->willReturn(reject(new \RuntimeException('DNS error'))); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.invalid:80'); @@ -264,15 +269,16 @@ public function testSkipConnectionIfDnsFails() $this->throwRejection($promise); }); - $this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error'); $this->loop->run(); } public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection() { - $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->will($this->returnCallback(function () { + $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->willReturnCallback(function () { return new Promise(function () { }, $this->expectCallableExactly(1)); - })); + }); $this->tcp->expects($this->never())->method('connect'); $promise = $this->connector->connect('example.com:80'); @@ -282,11 +288,9 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection() $this->throwRejection($promise); }); - $this->setExpectedException( - 'RuntimeException', - 'Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)', - \defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)'); + $this->expectExceptionCode(\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103); $this->loop->run(); } @@ -294,7 +298,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp() { $pending = new Promise(function () { }, $this->expectCallableOnce()); $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending); + $this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($pending); $promise = $this->connector->connect('1.2.3.4:80'); $this->loop->addTimer(0.1, function () use ($promise) { @@ -317,7 +321,7 @@ public function throwRejection($promise) throw $ex; } - public function provideIpvAddresses() + public static function provideIpvAddresses() { $ipv6 = [ ['1:2:3:4'], @@ -330,17 +334,13 @@ public function provideIpvAddresses() ['1.2.3.4', '5.6.7.8', '9.10.11.12'] ]; - $ips = []; - foreach ($ipv6 as $v6) { foreach ($ipv4 as $v4) { - $ips[] = [ + yield [ $v6, $v4 ]; } } - - return $ips; } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index acf42c12..361443e1 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -26,7 +26,7 @@ public function gettingStuffFromGoogleShouldWork() $conn = await($connector->connect('google.com:80')); assert($conn instanceof ConnectionInterface); - $this->assertContainsString(':80', $conn->getRemoteAddress()); + $this->assertStringContainsString(':80', $conn->getRemoteAddress()); $this->assertNotEquals('google.com:80', $conn->getRemoteAddress()); $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -34,7 +34,7 @@ public function gettingStuffFromGoogleShouldWork() $response = $this->buffer($conn, self::TIMEOUT); assert(!$conn->isReadable()); - $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); + $this->assertStringMatchesFormat('HTTP/1.0%a', $response); } /** @test */ @@ -50,7 +50,7 @@ public function gettingEncryptedStuffFromGoogleShouldWork() $response = $this->buffer($conn, self::TIMEOUT); assert(!$conn->isReadable()); - $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); + $this->assertStringMatchesFormat('HTTP/1.0%a', $response); } /** @test */ @@ -74,7 +74,7 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $response = $this->buffer($conn, self::TIMEOUT); assert(!$conn->isReadable()); - $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); + $this->assertStringMatchesFormat('HTTP/1.0%a', $response); } /** @test */ @@ -85,7 +85,7 @@ public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() $conn = await($connector->connect('google.com:443')); assert($conn instanceof ConnectionInterface); - $this->assertContainsString(':443', $conn->getRemoteAddress()); + $this->assertStringContainsString(':443', $conn->getRemoteAddress()); $this->assertNotEquals('google.com:443', $conn->getRemoteAddress()); $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -93,7 +93,7 @@ public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() $response = $this->buffer($conn, self::TIMEOUT); assert(!$conn->isReadable()); - $this->assertDoesNotMatchRegExp('#^HTTP/1\.0#', $response); + $this->assertStringNotMatchesFormat('HTTP/1.0%a', $response); } public function testConnectingFailsIfConnectorUsesInvalidDnsResolverAddress() @@ -109,7 +109,7 @@ public function testConnectingFailsIfConnectorUsesInvalidDnsResolverAddress() 'dns' => $dns ]); - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); await(timeout($connector->connect('google.com:80'), self::TIMEOUT)); } @@ -365,7 +365,7 @@ public function testConnectingFailsIfTimeoutIsTooSmall() 'timeout' => 0.001 ]); - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); await(timeout($connector->connect('google.com:80'), self::TIMEOUT)); } @@ -377,7 +377,7 @@ public function testSelfSignedRejectsIfVerificationIsEnabled() ] ]); - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); await(timeout($connector->connect('tls://self-signed.badssl.com:443'), self::TIMEOUT)); } diff --git a/tests/LimitingServerTest.php b/tests/LimitingServerTest.php index 9fc634d9..96d41986 100644 --- a/tests/LimitingServerTest.php +++ b/tests/LimitingServerTest.php @@ -2,9 +2,11 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\LimitingServer; +use React\Socket\ServerInterface; use React\Socket\TcpServer; use function React\Async\await; use function React\Promise\Timer\timeout; @@ -15,7 +17,7 @@ class LimitingServerTest extends TestCase public function testGetAddressWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('getAddress')->willReturn('127.0.0.1:1234'); $server = new LimitingServer($tcp, 100); @@ -25,7 +27,7 @@ public function testGetAddressWillBePassedThroughToTcpServer() public function testPauseWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('pause'); $server = new LimitingServer($tcp, 100); @@ -35,7 +37,7 @@ public function testPauseWillBePassedThroughToTcpServer() public function testPauseTwiceWillBePassedThroughToTcpServerOnce() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('pause'); $server = new LimitingServer($tcp, 100); @@ -46,7 +48,7 @@ public function testPauseTwiceWillBePassedThroughToTcpServerOnce() public function testResumeWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('resume'); $server = new LimitingServer($tcp, 100); @@ -57,7 +59,7 @@ public function testResumeWillBePassedThroughToTcpServer() public function testResumeTwiceWillBePassedThroughToTcpServerOnce() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('resume'); $server = new LimitingServer($tcp, 100); @@ -69,7 +71,7 @@ public function testResumeTwiceWillBePassedThroughToTcpServerOnce() public function testCloseWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('close'); $server = new LimitingServer($tcp, 100); @@ -79,7 +81,7 @@ public function testCloseWillBePassedThroughToTcpServer() public function testSocketErrorWillBeForwarded() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); @@ -92,9 +94,9 @@ public function testSocketErrorWillBeForwarded() public function testSocketConnectionWillBeForwarded() { - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); @@ -109,12 +111,12 @@ public function testSocketConnectionWillBeForwarded() public function testSocketConnectionWillBeClosedOnceLimitIsReached() { - $first = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $first = $this->createMock(ConnectionInterface::class); $first->expects($this->never())->method('close'); - $second = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $second = $this->createMock(ConnectionInterface::class); $second->expects($this->once())->method('close'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); @@ -128,13 +130,13 @@ public function testSocketConnectionWillBeClosedOnceLimitIsReached() public function testPausingServerWillBePausedOnceLimitIsReached() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); $loop->expects($this->once())->method('removeReadStream'); $tcp = new TcpServer(0, $loop); - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); $server = new LimitingServer($tcp, 1, true); diff --git a/tests/SecureConnectorTest.php b/tests/SecureConnectorTest.php index 833e874f..b96d3c33 100644 --- a/tests/SecureConnectorTest.php +++ b/tests/SecureConnectorTest.php @@ -2,9 +2,15 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Promise\Deferred; use React\Promise\Promise; +use React\Promise\PromiseInterface; +use React\Socket\Connection; +use React\Socket\ConnectionInterface; +use React\Socket\ConnectorInterface; use React\Socket\SecureConnector; +use React\Socket\StreamEncryption; use function React\Promise\reject; use function React\Promise\resolve; @@ -19,8 +25,8 @@ class SecureConnectorTest extends TestCase */ public function setUpConnector() { - $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $this->loop = $this->createMock(LoopInterface::class); + $this->tcp = $this->createMock(ConnectorInterface::class); $this->connector = new SecureConnector($this->tcp, $this->loop); } @@ -36,23 +42,23 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($streamEncryption); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testConnectionWillWaitForTcpConnection() { $pending = new Promise(function () { }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->will($this->returnValue($pending)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn($pending); $promise = $this->connector->connect('example.com:80'); - $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + $this->assertInstanceOf(PromiseInterface::class, $promise); } public function testConnectionWithCompleteUriWillBePassedThroughExpectForScheme() { $pending = new Promise(function () { }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80/path?query#fragment'))->will($this->returnValue($pending)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80/path?query#fragment')->willReturn($pending); $this->connector->connect('tls://example.com:80/path?query#fragment'); } @@ -64,7 +70,7 @@ public function testConnectionToInvalidSchemeWillReject() $promise = $this->connector->connect('tcp://example.com:80'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "tcp://example.com:80" is invalid (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); @@ -85,10 +91,10 @@ public function testConnectWillRejectWithTlsUriWhenUnderlyingConnectorRejects() }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tls://example.com:80 failed: Connection refused (ECONNREFUSED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } @@ -107,7 +113,7 @@ public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorR }); assert($exception instanceof \InvalidArgumentException); - $this->assertInstanceOf('InvalidArgumentException', $exception); + $this->assertInstanceOf(\InvalidArgumentException::class, $exception); $this->assertEquals('Invalid', $exception->getMessage()); $this->assertEquals(42, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -129,7 +135,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithT 'Connection to tcp://example.com:80 cancelled (ECONNABORTED)', defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 ); }); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->will($this->returnValue($pending)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn($pending); $promise = $this->connector->connect('example.com:80'); $promise->cancel(); @@ -140,19 +146,19 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithT }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tls://example.com:80 cancelled (ECONNABORTED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode()); - $this->assertInstanceOf('RuntimeException', $exception->getPrevious()); + $this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious()); $this->assertNotEquals('', $exception->getTraceAsString()); } public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream() { - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); $connection->expects($this->once())->method('close'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(resolve($connection)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(resolve($connection)); $promise = $this->connector->connect('example.com:80'); @@ -162,7 +168,7 @@ public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream() }); assert($exception instanceof \UnexpectedValueException); - $this->assertInstanceOf('UnexpectedValueException', $exception); + $this->assertInstanceOf(\UnexpectedValueException::class, $exception); $this->assertEquals('Base connector does not use internal Connection class exposing stream resource', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -171,33 +177,33 @@ public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream() public function testStreamEncryptionWillBeEnabledAfterConnecting() { - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->with($connection)->willReturn(new Promise(function () { })); $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); $ref->setAccessible(true); $ref->setValue($this->connector, $encryption); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(resolve($connection)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(resolve($connection)); $this->connector->connect('example.com:80'); } public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConnection() { - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); $connection->expects($this->once())->method('close'); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->willReturn(reject(new \RuntimeException('TLS error', 123))); $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); $ref->setAccessible(true); $ref->setValue($this->connector, $encryption); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(resolve($connection)); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(resolve($connection)); $promise = $this->connector->connect('example.com:80'); @@ -207,7 +213,7 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tls://example.com:80 failed during TLS handshake: TLS error', $exception->getMessage()); $this->assertEquals(123, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -216,13 +222,13 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection() { - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); $connection->expects($this->once())->method('close'); $pending = new Promise(function () { }, function () { throw new \Exception('Ignored'); }); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->willReturn($pending); $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); @@ -230,7 +236,7 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec $ref->setValue($this->connector, $encryption); $deferred = new Deferred(); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn($deferred->promise()); + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn($deferred->promise()); $promise = $this->connector->connect('example.com:80'); $deferred->resolve($connection); @@ -243,7 +249,7 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec }); assert($exception instanceof \RuntimeException); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); $this->assertEquals('Connection to tls://example.com:80 cancelled during TLS handshake (ECONNABORTED)', $exception->getMessage()); $this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode()); $this->assertNull($exception->getPrevious()); @@ -283,13 +289,13 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc // collect all garbage cycles } - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); $tcp = new Deferred(); $this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise()); $tls = new Deferred(); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->willReturn($tls->promise()); $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); diff --git a/tests/SecureServerTest.php b/tests/SecureServerTest.php index cfa2e72e..ff569a6d 100644 --- a/tests/SecureServerTest.php +++ b/tests/SecureServerTest.php @@ -2,16 +2,21 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; +use React\Promise\Promise; +use React\Socket\ConnectionInterface; +use React\Socket\Connection; use React\Socket\SecureServer; +use React\Socket\ServerInterface; +use React\Socket\StreamEncryption; use React\Socket\TcpServer; -use React\Promise\Promise; use function React\Promise\reject; class SecureServerTest extends TestCase { public function testConstructWithoutLoopAssignsLoopAutomatically() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $server = new SecureServer($tcp); @@ -23,15 +28,15 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($encryption); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testGetAddressWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('getAddress')->willReturn('tcp://127.0.0.1:1234'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new SecureServer($tcp, $loop, []); @@ -40,10 +45,10 @@ public function testGetAddressWillBePassedThroughToTcpServer() public function testGetAddressWillReturnNullIfTcpServerReturnsNull() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('getAddress')->willReturn(null); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new SecureServer($tcp, $loop, []); @@ -52,10 +57,10 @@ public function testGetAddressWillReturnNullIfTcpServerReturnsNull() public function testPauseWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('pause'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new SecureServer($tcp, $loop, []); @@ -64,10 +69,10 @@ public function testPauseWillBePassedThroughToTcpServer() public function testResumeWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('resume'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new SecureServer($tcp, $loop, []); @@ -76,10 +81,10 @@ public function testResumeWillBePassedThroughToTcpServer() public function testCloseWillBePassedThroughToTcpServer() { - $tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock(); + $tcp = $this->createMock(ServerInterface::class); $tcp->expects($this->once())->method('close'); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $server = new SecureServer($tcp, $loop, []); @@ -88,11 +93,11 @@ public function testCloseWillBePassedThroughToTcpServer() public function testConnectionWillBeClosedWithErrorIfItIsNotAStream() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); $connection->expects($this->once())->method('close'); $server = new SecureServer($tcp, $loop, []); @@ -104,11 +109,11 @@ public function testConnectionWillBeClosedWithErrorIfItIsNotAStream() public function testConnectionWillTryToEnableEncryptionAndWaitForHandshake() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); $connection->expects($this->once())->method('getRemoteAddress')->willReturn('tcp://127.0.0.1:1234'); $connection->expects($this->never())->method('close'); @@ -116,7 +121,7 @@ public function testConnectionWillTryToEnableEncryptionAndWaitForHandshake() $pending = new Promise(function () { }); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->willReturn($pending); $ref = new \ReflectionProperty($server, 'encryption'); @@ -135,11 +140,11 @@ public function testConnectionWillTryToEnableEncryptionAndWaitForHandshake() public function testConnectionWillBeClosedWithErrorIfEnablingEncryptionFails() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); - $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); + $connection = $this->createMock(Connection::class); $connection->expects($this->once())->method('getRemoteAddress')->willReturn('tcp://127.0.0.1:1234'); $connection->expects($this->once())->method('close'); @@ -147,7 +152,7 @@ public function testConnectionWillBeClosedWithErrorIfEnablingEncryptionFails() $error = new \RuntimeException('Original'); - $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); + $encryption = $this->createMock(StreamEncryption::class); $encryption->expects($this->once())->method('enable')->willReturn(reject($error)); $ref = new \ReflectionProperty($server, 'encryption'); @@ -166,13 +171,13 @@ public function testConnectionWillBeClosedWithErrorIfEnablingEncryptionFails() $tcp->emit('connection', [$connection]); - $this->assertInstanceOf('RuntimeException', $error); + $this->assertInstanceOf(\RuntimeException::class, $error); $this->assertEquals('Connection from tcp://127.0.0.1:1234 failed during TLS handshake: Original', $error->getMessage()); } public function testSocketErrorWillBeForwarded() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $tcp = new TcpServer(0, $loop); diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index 3ec87c74..537a2ce5 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\SocketServer; @@ -28,7 +29,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($tcp); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testCreateServerWithZeroPortAssignsRandomPort() @@ -40,31 +41,25 @@ public function testCreateServerWithZeroPortAssignsRandomPort() public function testConstructorWithInvalidUriThrows() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI "tcp://invalid URI" given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI "tcp://invalid URI" given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new SocketServer('invalid URI'); } public function testConstructorWithInvalidUriWithPortOnlyThrows() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new SocketServer('0'); } public function testConstructorWithInvalidUriWithSchemaAndPortOnlyThrows() { - $this->setExpectedException( - 'InvalidArgumentException', - 'Invalid URI given (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URI given (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new SocketServer('tcp://0'); } diff --git a/tests/Stub/CallableStub.php b/tests/Stub/CallableStub.php deleted file mode 100644 index 1b197ebd..00000000 --- a/tests/Stub/CallableStub.php +++ /dev/null @@ -1,10 +0,0 @@ -setAccessible(true); $loop = $ref->getValue($connector); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } /** @test */ @@ -36,11 +39,9 @@ public function connectionToEmptyPortShouldFailWithoutCallingCustomErrorHandler( $error = $errstr; }); - $this->setExpectedException( - 'RuntimeException', - 'Connection to tcp://127.0.0.1:9999 failed: Connection refused' . (function_exists('socket_import_stream') ? ' (ECONNREFUSED)' : ''), - defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection to tcp://127.0.0.1:9999 failed: Connection refused' . (function_exists('socket_import_stream') ? ' (ECONNREFUSED)' : '')); + $this->expectExceptionCode(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111); try { await(timeout($promise, self::TIMEOUT)); @@ -57,7 +58,7 @@ public function connectionToEmptyPortShouldFailWithoutCallingCustomErrorHandler( /** @test */ public function connectionToTcpServerShouldAddResourceToLoop() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $server = new TcpServer(0, $loop); @@ -81,7 +82,7 @@ public function connectionToTcpServerShouldSucceed() $connection = await(timeout($connector->connect('127.0.0.1:9999'), self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); $connection->close(); $server->close(); @@ -116,8 +117,8 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded() } // dummy rejected promise to make sure autoloader has initialized all classes - class_exists('React\Socket\SocketServer', true); - class_exists('PHPUnit\Framework\Error\Warning', true); + class_exists(SocketServer::class, true); + class_exists(Warning::class, true); $promise = new Promise(function () { throw new \RuntimeException('dummy'); }); $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection unset($promise); @@ -132,7 +133,7 @@ class_exists('PHPUnit\Framework\Error\Warning', true); $fds[] = $fd; } - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); await(timeout($connector->connect('127.0.0.1:9999'), self::TIMEOUT)); } @@ -160,11 +161,9 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() $promise = $connector->connect($address); - $this->setExpectedException( - 'RuntimeException', - 'Connection to ' . $address . ' failed: ' . (function_exists('socket_strerror') ? socket_strerror($enetunreach) . ' (ENETUNREACH)' : 'Network is unreachable'), - $enetunreach - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection to ' . $address . ' failed: ' . (function_exists('socket_strerror') ? socket_strerror($enetunreach) . ' (ENETUNREACH)' : 'Network is unreachable')); + $this->expectExceptionCode($enetunreach); await(timeout($promise, self::TIMEOUT)); } @@ -194,7 +193,7 @@ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() $connection = await(timeout($connector->connect('127.0.0.1:9999'), self::TIMEOUT)); /* @var $connection ConnectionInterface */ - $this->assertContainsString('tcp://127.0.0.1:', $connection->getLocalAddress()); + $this->assertStringContainsString('tcp://127.0.0.1:', $connection->getLocalAddress()); $this->assertNotEquals('tcp://127.0.0.1:9999', $connection->getLocalAddress()); $connection->close(); @@ -267,7 +266,7 @@ public function connectionToIp6TcpServerShouldSucceed() $this->assertEquals('tcp://[::1]:9999', $connection->getRemoteAddress()); - $this->assertContainsString('tcp://[::1]:', $connection->getLocalAddress()); + $this->assertStringContainsString('tcp://[::1]:', $connection->getLocalAddress()); $this->assertNotEquals('tcp://[::1]:9999', $connection->getLocalAddress()); $connection->close(); @@ -277,13 +276,13 @@ public function connectionToIp6TcpServerShouldSucceed() /** @test */ public function connectionToHostnameShouldFailImmediately() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $promise = $connector->connect('www.google.com:80'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "tcp://www.google.com:80" does not contain a valid host IP (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); @@ -292,13 +291,13 @@ public function connectionToHostnameShouldFailImmediately() /** @test */ public function connectionToInvalidPortShouldFailImmediately() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $promise = $connector->connect('255.255.255.255:12345678'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "tcp://255.255.255.255:12345678" is invalid (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); @@ -307,7 +306,7 @@ public function connectionToInvalidPortShouldFailImmediately() /** @test */ public function connectionToInvalidSchemeShouldFailImmediately() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $connector->connect('tls://google.com:443')->then( @@ -319,7 +318,7 @@ public function connectionToInvalidSchemeShouldFailImmediately() /** @test */ public function cancellingConnectionShouldRemoveResourceFromLoopAndCloseResource() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $server = new TcpServer(0, $loop); @@ -354,11 +353,9 @@ public function cancellingConnectionShouldRejectPromise() $promise = $connector->connect($server->getAddress()); $promise->cancel(); - $this->setExpectedException( - 'RuntimeException', - 'Connection to ' . $server->getAddress() . ' cancelled during TCP/IP handshake (ECONNABORTED)', - defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Connection to ' . $server->getAddress() . ' cancelled during TCP/IP handshake (ECONNABORTED)'); + $this->expectExceptionCode(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103); try { await($promise); @@ -378,7 +375,7 @@ public function testCancelDuringConnectionShouldNotCreateAnyGarbageReferences() // collect all garbage cycles } - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $connector = new TcpConnector($loop); $promise = $connector->connect('127.0.0.1:9999'); diff --git a/tests/TcpServerTest.php b/tests/TcpServerTest.php index 7f052c3e..1adf91b8 100644 --- a/tests/TcpServerTest.php +++ b/tests/TcpServerTest.php @@ -3,7 +3,9 @@ namespace React\Tests\Socket; use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; use React\Promise\Promise; +use React\Socket\ConnectionInterface; use React\Socket\TcpServer; use React\Stream\DuplexResourceStream; use function React\Async\await; @@ -37,7 +39,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($server); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); $server->close(); } @@ -56,7 +58,7 @@ public function testServerEmitsConnectionEventForNewConnection() $connection = await(timeout($promise, self::TIMEOUT)); - $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + $this->assertInstanceOf(ConnectionInterface::class, $connection); } /** @@ -235,7 +237,7 @@ public function testConnectionDoesEndWhenClientCloses() public function testCtorAddsResourceToLoop() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); new TcpServer(0, $loop); @@ -243,7 +245,7 @@ public function testCtorAddsResourceToLoop() public function testResumeWithoutPauseIsNoOp() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); $server = new TcpServer(0, $loop); @@ -252,7 +254,7 @@ public function testResumeWithoutPauseIsNoOp() public function testPauseRemovesResourceFromLoop() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new TcpServer(0, $loop); @@ -261,7 +263,7 @@ public function testPauseRemovesResourceFromLoop() public function testPauseAfterPauseIsNoOp() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new TcpServer(0, $loop); @@ -271,7 +273,7 @@ public function testPauseAfterPauseIsNoOp() public function testCloseRemovesResourceFromLoop() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new TcpServer(0, $loop); @@ -281,7 +283,7 @@ public function testCloseRemovesResourceFromLoop() public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHandler() { $listener = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream')->with($this->anything(), $this->callback(function ($cb) use (&$listener) { $listener = $cb; return true; @@ -311,7 +313,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa $this->assertLessThan(1, $time); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage()); @@ -335,11 +337,9 @@ public function testListenOnBusyPortThrows() $this->markTestSkipped('Windows supports listening on same port multiple times'); } - $this->setExpectedException( - 'RuntimeException', - 'Failed to listen on "tcp://127.0.0.1:' . $this->port . '": ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EADDRINUSE) . ' (EADDRINUSE)' : 'Address already in use'), - defined('SOCKET_EADDRINUSE') ? SOCKET_EADDRINUSE : 0 - ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Failed to listen on "tcp://127.0.0.1:' . $this->port . '": ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EADDRINUSE) . ' (EADDRINUSE)' : 'Address already in use')); + $this->expectExceptionCode(defined('SOCKET_EADDRINUSE') ? SOCKET_EADDRINUSE : 0); new TcpServer($this->port); } diff --git a/tests/TestCase.php b/tests/TestCase.php index d707db08..1e7a79e0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -69,7 +69,14 @@ protected function expectCallableNever() protected function createCallableMock() { - return $this->getMockBuilder('React\Tests\Socket\Stub\CallableStub')->getMock(); + $builder = $this->getMockBuilder(\stdClass::class); + if (method_exists($builder, 'addMethods')) { + // PHPUnit 9+ + return $builder->addMethods(['__invoke'])->getMock(); + } else { + // legacy PHPUnit + return $builder->setMethods(['__invoke'])->getMock(); + } } protected function buffer(ReadableStreamInterface $stream, $timeout) @@ -106,17 +113,6 @@ function () use ($stream) { return $buffer; } - public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) - { - $this->expectException($exception); - if ($exceptionMessage !== '') { - $this->expectExceptionMessage($exceptionMessage); - } - if ($exceptionCode !== null) { - $this->expectExceptionCode($exceptionCode); - } - } - protected function supportsTls13() { // TLS 1.3 is supported as of OpenSSL 1.1.1 (https://www.openssl.org/blog/blog/2018/09/11/release111/) @@ -133,37 +129,4 @@ protected function supportsTls13() } return false; } - - public function assertContainsString($needle, $haystack) - { - if (method_exists($this, 'assertStringContainsString')) { - // PHPUnit 7.5+ - $this->assertStringContainsString($needle, $haystack); - } else { - // legacy PHPUnit 4- PHPUnit 7.5 - $this->assertContains($needle, $haystack); - } - } - - public function assertMatchesRegExp($pattern, $string) - { - if (method_exists($this, 'assertMatchesRegularExpression')) { - // PHPUnit 10 - $this->assertMatchesRegularExpression($pattern, $string); - } else { - // legacy PHPUnit 4 - PHPUnit 9.2 - $this->assertRegExp($pattern, $string); - } - } - - public function assertDoesNotMatchRegExp($pattern, $string) - { - if (method_exists($this, 'assertDoesNotMatchRegularExpression')) { - // PHPUnit 10 - $this->assertDoesNotMatchRegularExpression($pattern, $string); - } else { - // legacy PHPUnit 4 - PHPUnit 9.2 - $this->assertNotRegExp($pattern, $string); - } - } } diff --git a/tests/TimeoutConnectorTest.php b/tests/TimeoutConnectorTest.php index f4c5dfc1..e121cab4 100644 --- a/tests/TimeoutConnectorTest.php +++ b/tests/TimeoutConnectorTest.php @@ -3,8 +3,12 @@ namespace React\Tests\Socket; use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; +use React\EventLoop\TimerInterface; use React\Promise\Deferred; use React\Promise\Promise; +use React\Socket\ConnectionInterface; +use React\Socket\ConnectorInterface; use React\Socket\TimeoutConnector; use function React\Promise\reject; use function React\Promise\resolve; @@ -13,7 +17,7 @@ class TimeoutConnectorTest extends TestCase { public function testConstructWithoutLoopAssignsLoopAutomatically() { - $base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $base = $this->createMock(ConnectorInterface::class); $connector = new TimeoutConnector($base, 0.01); @@ -21,16 +25,16 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($connector); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testRejectsPromiseWithoutStartingTimerWhenWrappedConnectorReturnsRejectedPromise() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); $loop->expects($this->never())->method('cancelTimer'); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn(reject(new \RuntimeException('Failed', 42))); $timeout = new TimeoutConnector($connector, 5.0, $loop); @@ -49,13 +53,13 @@ public function testRejectsPromiseWithoutStartingTimerWhenWrappedConnectorReturn public function testRejectsPromiseAfterCancellingTimerWhenWrappedConnectorReturnsPendingPromiseThatRejects() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($deferred->promise()); $timeout = new TimeoutConnector($connector, 5.0, $loop); @@ -76,12 +80,12 @@ public function testRejectsPromiseAfterCancellingTimerWhenWrappedConnectorReturn public function testResolvesPromiseWithoutStartingTimerWhenWrappedConnectorReturnsResolvedPromise() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer'); $loop->expects($this->never())->method('cancelTimer'); - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn(resolve($connection)); $timeout = new TimeoutConnector($connector, 5.0, $loop); @@ -98,20 +102,20 @@ public function testResolvesPromiseWithoutStartingTimerWhenWrappedConnectorRetur public function testResolvesPromiseAfterCancellingTimerWhenWrappedConnectorReturnsPendingPromiseThatResolves() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $deferred = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($deferred->promise()); $timeout = new TimeoutConnector($connector, 5.0, $loop); $promise = $timeout->connect('example.com:80'); - $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); + $connection = $this->createMock(ConnectionInterface::class); $deferred->resolve($connection); $resolved = null; @@ -125,8 +129,8 @@ public function testResolvesPromiseAfterCancellingTimerWhenWrappedConnectorRetur public function testRejectsPromiseAndCancelsPendingConnectionWhenTimeoutTriggers() { $timerCallback = null; - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.01, $this->callback(function ($callback) use (&$timerCallback) { $timerCallback = $callback; return true; @@ -134,7 +138,7 @@ public function testRejectsPromiseAndCancelsPendingConnectionWhenTimeoutTriggers $loop->expects($this->once())->method('cancelTimer')->with($timer); $cancelled = 0; - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn(new Promise(function () { }, function () use (&$cancelled) { ++$cancelled; throw new \RuntimeException(); @@ -163,13 +167,13 @@ public function testRejectsPromiseAndCancelsPendingConnectionWhenTimeoutTriggers public function testCancellingPromiseWillCancelPendingConnectionAndRejectPromise() { - $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $timer = $this->createMock(TimerInterface::class); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addTimer')->with(0.01, $this->anything())->willReturn($timer); $loop->expects($this->once())->method('cancelTimer')->with($timer); $cancelled = 0; - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn(new Promise(function () { }, function () use (&$cancelled) { ++$cancelled; throw new \RuntimeException('Cancelled'); @@ -206,7 +210,7 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences } $connection = new Deferred(); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); $timeout = new TimeoutConnector($connector, 0.01); @@ -234,7 +238,7 @@ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences() $connection = new Deferred(function () { throw new \RuntimeException('Connection cancelled'); }); - $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $connector = $this->createMock(ConnectorInterface::class); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); $timeout = new TimeoutConnector($connector, 0); diff --git a/tests/UnixConnectorTest.php b/tests/UnixConnectorTest.php index d7e314a4..ad6b757a 100644 --- a/tests/UnixConnectorTest.php +++ b/tests/UnixConnectorTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Socket; +use React\EventLoop\LoopInterface; use React\Socket\ConnectionInterface; use React\Socket\UnixConnector; @@ -15,7 +16,7 @@ class UnixConnectorTest extends TestCase */ public function setUpConnector() { - $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->loop = $this->createMock(LoopInterface::class); $this->connector = new UnixConnector($this->loop); } @@ -27,7 +28,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($connector); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); } public function testInvalid() @@ -35,7 +36,7 @@ public function testInvalid() $promise = $this->connector->connect('google.com:80'); $promise->then(null, $this->expectCallableOnceWithException( - 'RuntimeException' + \RuntimeException::class )); } @@ -44,7 +45,7 @@ public function testInvalidScheme() $promise = $this->connector->connect('tcp://google.com:80'); $promise->then(null, $this->expectCallableOnceWithException( - 'InvalidArgumentException', + \InvalidArgumentException::class, 'Given URI "tcp://google.com:80" is invalid (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) )); diff --git a/tests/UnixServerTest.php b/tests/UnixServerTest.php index 6d972b9f..0e71cef7 100644 --- a/tests/UnixServerTest.php +++ b/tests/UnixServerTest.php @@ -3,6 +3,7 @@ namespace React\Tests\Socket; use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; use React\Socket\UnixServer; use React\Stream\DuplexResourceStream; use function React\Async\await; @@ -42,7 +43,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $ref->setAccessible(true); $loop = $ref->getValue($server); - $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + $this->assertInstanceOf(LoopInterface::class, $loop); $server->close(); } @@ -241,7 +242,7 @@ public function testCtorAddsResourceToLoop() unlink(str_replace('unix://', '', $this->uds)); $this->uds = $this->getRandomSocketUri(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); new UnixServer($this->uds, $loop); @@ -249,26 +250,24 @@ public function testCtorAddsResourceToLoop() public function testCtorThrowsForInvalidAddressScheme() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); - $this->setExpectedException( - 'InvalidArgumentException', - 'Given URI "tcp://localhost:0" is invalid (EINVAL)', - defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22) - ); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Given URI "tcp://localhost:0" is invalid (EINVAL)'); + $this->expectExceptionCode(defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)); new UnixServer('tcp://localhost:0', $loop); } public function testCtorThrowsWhenPathIsNotWritableWithoutCallingCustomErrorHandler() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $error = null; set_error_handler(function ($_, $errstr) use (&$error) { $error = $errstr; }); - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); try { new UnixServer('/dev/null', $loop); @@ -287,7 +286,7 @@ public function testResumeWithoutPauseIsNoOp() unlink(str_replace('unix://', '', $this->uds)); $this->uds = $this->getRandomSocketUri(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream'); $server = new UnixServer($this->uds, $loop); @@ -299,7 +298,7 @@ public function testPauseRemovesResourceFromLoop() unlink(str_replace('unix://', '', $this->uds)); $this->uds = $this->getRandomSocketUri(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new UnixServer($this->uds, $loop); @@ -311,7 +310,7 @@ public function testPauseAfterPauseIsNoOp() unlink(str_replace('unix://', '', $this->uds)); $this->uds = $this->getRandomSocketUri(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new UnixServer($this->uds, $loop); @@ -324,7 +323,7 @@ public function testCloseRemovesResourceFromLoop() unlink(str_replace('unix://', '', $this->uds)); $this->uds = $this->getRandomSocketUri(); - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('removeReadStream'); $server = new UnixServer($this->uds, $loop); @@ -337,7 +336,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa $this->uds = $this->getRandomSocketUri(); $listener = null; - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop = $this->createMock(LoopInterface::class); $loop->expects($this->once())->method('addReadStream')->with($this->anything(), $this->callback(function ($cb) use (&$listener) { $listener = $cb; return true; @@ -367,7 +366,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa $this->assertLessThan(1, $time); - $this->assertInstanceOf('RuntimeException', $exception); + $this->assertInstanceOf(\RuntimeException::class, $exception); assert($exception instanceof \RuntimeException); $this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage()); @@ -391,7 +390,7 @@ public function testListenOnBusyPortThrows() $this->markTestSkipped('Windows supports listening on same port multiple times'); } - $this->setExpectedException('RuntimeException'); + $this->expectException(\RuntimeException::class); new UnixServer($this->uds); }