From 81f3d73d8cc15ca9ce12ad2c597e32ff89bc6fc1 Mon Sep 17 00:00:00 2001 From: Holger Woltersdorf Date: Thu, 15 Jun 2017 23:28:16 +0200 Subject: [PATCH] Replaces flsockopen with stream_socket_client, closes #9 Changed * Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection` * The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket` Only the socket path must be passed. * Replaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm. --- CHANGELOG.md | 11 +++++ README.md | 18 ++++--- bin/fcgiget | 6 +-- composer.json | 4 +- composer.lock | 2 +- src/Client.php | 2 +- src/Interfaces/ConfiguresSocketConnection.php | 6 +-- src/Requests/AbstractRequest.php | 30 ++++++------ src/Responses/Response.php | 2 +- src/Socket.php | 16 ++----- src/SocketConnections/NetworkSocket.php | 11 ++--- src/SocketConnections/UnixDomainSocket.php | 19 ++------ tests/Integration/NetworkSocketTest.php | 24 +++++----- tests/Integration/UnixDomainSocketTest.php | 48 +++++++++---------- tests/Unit/ClientTest.php | 22 ++++----- .../Encoders/NameValuePairEncoderTest.php | 5 +- tests/Unit/Encoders/PacketEncoderTest.php | 5 +- tests/Unit/Requests/AbstractRequestTest.php | 10 ++-- tests/Unit/Requests/DeleteRequestTest.php | 2 +- tests/Unit/Requests/GetRequestTest.php | 2 +- tests/Unit/Requests/PatchRequestTest.php | 2 +- tests/Unit/Requests/PostRequestTest.php | 2 +- tests/Unit/Requests/PutRequestTest.php | 2 +- tests/Unit/Responses/ResponseTest.php | 6 +-- .../SocketConnections/NetworkSocketTest.php | 17 ++++--- .../UnixDomainSocketTest.php | 23 +++++---- tests/Unit/SocketTest.php | 20 ++++---- tests/bootstrap.php | 2 +- 28 files changed, 157 insertions(+), 162 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bac863..0b672ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com). +## [2.3.0] - 2017-06-15 + +### Changed + +* Replaced methods `getHost()` and `getPort()` with `getSocketAddress()` in interface `hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection` - [#9] +* The transport protocol `unix://` must be omitted for the first parameter of `hollodotme\FastCGI\SocketConnections\UnixDomainSocket` + Only the socket path must be passed. - [#9] +* Replaced `fsockopen()` with `stream_socket_client()` for connecting to php-fpm. - [#9] + ## [2.2.0] - 2017-04-15 ### Added @@ -98,6 +107,7 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http * Getters/Setters for connect timeout, read/write timeout, keep alive, socket persistence from `Client` (now part of the socket connection) * Method `Client->getValues()` +[2.3.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.2.0...v2.3.0 [2.2.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.1.0...v2.2.0 [2.1.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.0.1...v2.1.0 [2.0.1]: https://github.com/hollodotme/fast-cgi-client/compare/v2.0.0...v2.0.1 @@ -108,3 +118,4 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http [#2]: https://github.com/hollodotme/fast-cgi-client/issues/2 [#5]: https://github.com/hollodotme/fast-cgi-client/issues/5 [#6]: https://github.com/hollodotme/fast-cgi-client/issues/6 +[#9]: https://github.com/hollodotme/fast-cgi-client/issues/9 diff --git a/README.md b/README.md index 7a41b4b..1c90c7b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ You can find an experimental use-case in my related blog posts: * [Experimental async PHP vol. 1](http://bit.ly/eapv1) * [Experimental async PHP vol. 2](http://bit.ly/eapv2) +You can also find slides of my talks about this project on [speakerdeck.com](https://speakerdeck.com/hollodotme). + --- ## Installation @@ -41,7 +43,7 @@ The following examples assume a that the content of `/path/to/target/script.php` ```php 'value']); $request = new PostRequest('/path/to/target/script.php', $content); @@ -652,4 +656,6 @@ Run a call through a network socket: Run a call through a Unix Domain Socket - bin/fcgiget unix:/var/run/php/php7.0-fpm.sock/status + bin/fcgiget /var/run/php/php7.1-fpm.sock/status + +This shows the response of the php-fpm status page, if enabled. diff --git a/bin/fcgiget b/bin/fcgiget index e9a1c34..dc61358 100755 --- a/bin/fcgiget +++ b/bin/fcgiget @@ -50,7 +50,7 @@ if ( $_SERVER['argc'] < 2 ) { echo 'Usage: ' . $_SERVER['argv'][0] . " URI\n\n"; echo 'Ex: ' . $_SERVER['argv'][0] . " localhost:9000/status\n"; - echo 'Ex: ' . $_SERVER['argv'][0] . " unix:/var/run/php-fpm/web.sock/status\n"; + echo 'Ex: ' . $_SERVER['argv'][0] . " /var/run/php-fpm/web.sock/status\n"; exit( 1 ); } @@ -92,8 +92,8 @@ else } if ( $sock ) { - $client = new Client( new UnixDomainSocket( "unix://{$sock}" ) ); - echo "Call: {$uri} on UDS {$sock}\n\n"; + $client = new Client( new UnixDomainSocket( (string)$sock ) ); + echo "Call: {$uri} on UDS unix://{$sock}\n\n"; } else { diff --git a/composer.json b/composer.json index 769492a..1458832 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "extra": { "tools": { "phpunit": { - "url": "https://phar.phpunit.de/phpunit-6.1.0.phar", + "url": "https://phar.phpunit.de/phpunit-6.2.2.phar", "only-dev": true }, "coveralls": { @@ -49,7 +49,7 @@ "only-dev": true }, "phpmetrics": { - "url": "https://github.com/phpmetrics/PhpMetrics/releases/download/v2.1.0/phpmetrics.phar", + "url": "https://github.com/phpmetrics/PhpMetrics/releases/download/v2.2.0/phpmetrics.phar", "only-dev": true } } diff --git a/composer.lock b/composer.lock index 7073a5b..cc1aa2f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "303afd44e66d7bc86272d3572d6e50b3", + "content-hash": "3032c15309aa343f31cb885d1b7493e3", "packages": [], "packages-dev": [ { diff --git a/src/Client.php b/src/Client.php index 2771db4..779ece0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -137,7 +137,7 @@ private function getSocketWithId( int $requestId ) : Socket return $this->sockets[ $requestId ]; } - private function guardSocketExists( int $requestId ) + private function guardSocketExists( int $requestId ) : void { if ( !isset( $this->sockets[ $requestId ] ) ) { diff --git a/src/Interfaces/ConfiguresSocketConnection.php b/src/Interfaces/ConfiguresSocketConnection.php index bad90b3..90edc30 100644 --- a/src/Interfaces/ConfiguresSocketConnection.php +++ b/src/Interfaces/ConfiguresSocketConnection.php @@ -1,4 +1,4 @@ -serverSoftware; } - public function setServerSoftware( string $serverSoftware ) + public function setServerSoftware( string $serverSoftware ) : void { $this->serverSoftware = $serverSoftware; } @@ -101,7 +101,7 @@ public function getRemoteAddress() : string return $this->remoteAddress; } - public function setRemoteAddress( string $remoteAddress ) + public function setRemoteAddress( string $remoteAddress ) : void { $this->remoteAddress = $remoteAddress; } @@ -111,7 +111,7 @@ public function getRemotePort() : int return $this->remotePort; } - public function setRemotePort( int $remotePort ) + public function setRemotePort( int $remotePort ) : void { $this->remotePort = $remotePort; } @@ -121,7 +121,7 @@ public function getServerAddress() : string return $this->serverAddress; } - public function setServerAddress( string $serverAddress ) + public function setServerAddress( string $serverAddress ) : void { $this->serverAddress = $serverAddress; } @@ -131,7 +131,7 @@ public function getServerPort() : int return $this->serverPort; } - public function setServerPort( int $serverPort ) + public function setServerPort( int $serverPort ) : void { $this->serverPort = $serverPort; } @@ -141,7 +141,7 @@ public function getServerName() : string return $this->serverName; } - public function setServerName( string $serverName ) + public function setServerName( string $serverName ) : void { $this->serverName = $serverName; } @@ -151,7 +151,7 @@ public function getServerProtocol() : string return $this->serverProtocol; } - public function setServerProtocol( string $serverProtocol ) + public function setServerProtocol( string $serverProtocol ) : void { $this->serverProtocol = $serverProtocol; } @@ -161,7 +161,7 @@ public function getContentType() : string return $this->contentType; } - public function setContentType( string $contentType ) + public function setContentType( string $contentType ) : void { $this->contentType = $contentType; } @@ -171,23 +171,23 @@ public function getContent() : string return $this->content; } - public function setContent( string $content ) + public function setContent( string $content ) : void { $this->content = $content; $this->contentLength = strlen( $content ); } - public function setCustomVar( string $key, $value ) + public function setCustomVar( string $key, $value ) : void { $this->customVars[ $key ] = $value; } - public function addCustomVars( array $vars ) + public function addCustomVars( array $vars ) : void { $this->customVars = array_merge( $this->customVars, $vars ); } - public function resetCustomVars() + public function resetCustomVars() : void { $this->customVars = []; } @@ -239,7 +239,7 @@ public function getRequestUri() : string return $this->requestUri; } - public function setRequestUri( string $requestUri ) + public function setRequestUri( string $requestUri ) : void { $this->requestUri = $requestUri; } @@ -249,7 +249,7 @@ public function getResponseCallbacks() : array return $this->responseCallbacks; } - public function addResponseCallbacks( callable ...$callbacks ) + public function addResponseCallbacks( callable ...$callbacks ) : void { $this->responseCallbacks = array_merge( $this->responseCallbacks, $callbacks ); } @@ -259,7 +259,7 @@ public function getFailureCallbacks() : array return $this->failureCallbacks; } - public function addFailureCallbacks( callable ...$callbacks ) + public function addFailureCallbacks( callable ...$callbacks ) : void { $this->failureCallbacks = array_merge( $this->failureCallbacks, $callbacks ); } diff --git a/src/Responses/Response.php b/src/Responses/Response.php index 9fd8f1f..4c89a1e 100644 --- a/src/Responses/Response.php +++ b/src/Responses/Response.php @@ -59,7 +59,7 @@ public function __construct( int $requestId, string $rawResponse, float $duratio $this->parseHeadersAndBody(); } - private function parseHeadersAndBody() + private function parseHeadersAndBody() : void { $lines = explode( PHP_EOL, $this->rawResponse ); $offset = 0; diff --git a/src/Socket.php b/src/Socket.php index 480b283..5501445 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -148,18 +148,12 @@ public function sendRequest( ProvidesRequestData $request ) : void $this->startTime = microtime( true ); } - private function connect() + private function connect() : void { - if ( is_resource( $this->resource ) ) - { - return; - } - try { - $this->resource = @fsockopen( - $this->connection->getHost(), - $this->connection->getPort(), + $this->resource = @stream_socket_client( + $this->connection->getSocketAddress(), $errorNumber, $errorString, $this->connection->getConnectTimeout() / 1000 @@ -351,7 +345,7 @@ private function readPacket() : ?array return null; } - private function handleNullPacket( $packet ) + private function handleNullPacket( $packet ) : void { if ( $packet === null ) { @@ -371,7 +365,7 @@ private function handleNullPacket( $packet ) } } - private function guardRequestCompleted( int $flag ) + private function guardRequestCompleted( int $flag ) : void { switch ( $flag ) { diff --git a/src/SocketConnections/NetworkSocket.php b/src/SocketConnections/NetworkSocket.php index d067e9e..7397a84 100644 --- a/src/SocketConnections/NetworkSocket.php +++ b/src/SocketConnections/NetworkSocket.php @@ -1,4 +1,4 @@ -readWriteTimeout = $readWriteTimeout; } - public function getHost() : string + public function getSocketAddress() : string { - return $this->host; - } - - public function getPort() : int - { - return $this->port; + return sprintf( 'tcp://%s:%d', $this->host, $this->port ); } public function getConnectTimeout() : int diff --git a/src/SocketConnections/UnixDomainSocket.php b/src/SocketConnections/UnixDomainSocket.php index 28b0002..d85d5a3 100644 --- a/src/SocketConnections/UnixDomainSocket.php +++ b/src/SocketConnections/UnixDomainSocket.php @@ -1,4 +1,4 @@ -host = $socketPath; - $this->port = -1; + $this->socketPath = $socketPath; $this->connectTimeout = $connectTimeout; $this->readWriteTimeout = $readWriteTimeout; } - public function getHost() : string - { - return $this->host; - } - - public function getPort() : int + public function getSocketAddress() : string { - return $this->port; + return 'unix://' . $this->socketPath; } public function getConnectTimeout() : int diff --git a/tests/Integration/NetworkSocketTest.php b/tests/Integration/NetworkSocketTest.php index d873b97..167d6dd 100644 --- a/tests/Integration/NetworkSocketTest.php +++ b/tests/Integration/NetworkSocketTest.php @@ -36,7 +36,7 @@ */ final class NetworkSocketTest extends TestCase { - public function testCanSendAsyncRequestAndReceiveRequestId() + public function testCanSendAsyncRequestAndReceiveRequestId() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -49,7 +49,7 @@ public function testCanSendAsyncRequestAndReceiveRequestId() $this->assertLessThanOrEqual( 65535, $requestId ); } - public function testCanSendAsyncRequestAndReadResponse() + public function testCanSendAsyncRequestAndReadResponse() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -67,7 +67,7 @@ public function testCanSendAsyncRequestAndReadResponse() $this->assertSame( $requestId, $response->getRequestId() ); } - public function testCanSendSyncRequestAndReceiveResponse() + public function testCanSendSyncRequestAndReceiveResponse() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -86,7 +86,7 @@ public function testCanSendSyncRequestAndReceiveResponse() $this->assertLessThanOrEqual( 65535, $response->getRequestId() ); } - public function testCanReceiveResponseInCallback() + public function testCanReceiveResponseInCallback() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -106,7 +106,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) $client->waitForResponses(); } - public function testCanHandleExceptionsInFailureCallback() + public function testCanHandleExceptionsInFailureCallback() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -134,7 +134,7 @@ function ( \Throwable $throwable ) use ( $unitTest ) $client->waitForResponses(); } - public function testCanCheckForRequestIdsHavingResponses() + public function testCanCheckForRequestIdsHavingResponses() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -149,7 +149,7 @@ public function testCanCheckForRequestIdsHavingResponses() $this->assertEquals( [ $requestId ], $client->getRequestIdsHavingResponse() ); } - public function testCanReadResponses() + public function testCanReadResponses() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -185,7 +185,7 @@ public function testCanReadResponses() /** * @expectedException \hollodotme\FastCGI\Exceptions\TimedoutException */ - public function testReadingSyncResponseCanTimeOut() + public function testReadingSyncResponseCanTimeOut() : void { $connection = new NetworkSocket( '127.0.0.1', 9000, Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); @@ -202,7 +202,7 @@ public function testReadingSyncResponseCanTimeOut() $client->sendRequest( $request ); } - public function testCanHandleReadyResponses() + public function testCanHandleReadyResponses() : void { $connection = new NetworkSocket( '127.0.0.1', 9000, Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); @@ -226,7 +226,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) } } - public function testCanReadReadyResponses() + public function testCanReadReadyResponses() : void { $connection = new NetworkSocket( '127.0.0.1', 9000, Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); @@ -246,7 +246,7 @@ public function testCanReadReadyResponses() $this->expectOutputString( 'unit' ); } - public function testCanWaitForResponse() + public function testCanWaitForResponse() : void { $connection = new NetworkSocket( '127.0.0.1', 9000, Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); @@ -267,7 +267,7 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) $client->waitForResponse( $requestId ); } - public function testReadResponsesSkipsUnknownRequestIds() + public function testReadResponsesSkipsUnknownRequestIds() : void { $connection = new NetworkSocket( '127.0.0.1', 9000, Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); diff --git a/tests/Integration/UnixDomainSocketTest.php b/tests/Integration/UnixDomainSocketTest.php index dec68d6..58c1efc 100644 --- a/tests/Integration/UnixDomainSocketTest.php +++ b/tests/Integration/UnixDomainSocketTest.php @@ -36,9 +36,9 @@ */ final class UnixDomainSocketTest extends TestCase { - public function testCanSendAsyncRequestAndReceiveRequestId() + public function testCanSendAsyncRequestAndReceiveRequestId() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -49,9 +49,9 @@ public function testCanSendAsyncRequestAndReceiveRequestId() $this->assertLessThanOrEqual( 65535, $requestId ); } - public function testCanSendAsyncRequestAndReadResponse() + public function testCanSendAsyncRequestAndReadResponse() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -67,9 +67,9 @@ public function testCanSendAsyncRequestAndReadResponse() $this->assertSame( $requestId, $response->getRequestId() ); } - public function testCanSendSyncRequestAndReceiveResponse() + public function testCanSendSyncRequestAndReceiveResponse() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -86,9 +86,9 @@ public function testCanSendSyncRequestAndReceiveResponse() $this->assertLessThanOrEqual( 65535, $response->getRequestId() ); } - public function testCanReceiveResponseInCallback() + public function testCanReceiveResponseInCallback() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -106,9 +106,9 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) $client->waitForResponses(); } - public function testCanHandleExceptionsInFailureCallback() + public function testCanHandleExceptionsInFailureCallback() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -134,9 +134,9 @@ function ( \Throwable $throwable ) use ( $unitTest ) $client->waitForResponses(); } - public function testCanCheckForRequestIdsHavingResponses() + public function testCanCheckForRequestIdsHavingResponses() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -149,9 +149,9 @@ public function testCanCheckForRequestIdsHavingResponses() $this->assertEquals( [ $requestId ], $client->getRequestIdsHavingResponse() ); } - public function testCanReadResponses() + public function testCanReadResponses() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -185,9 +185,9 @@ public function testCanReadResponses() /** * @expectedException \hollodotme\FastCGI\Exceptions\TimedoutException */ - public function testReadingSyncResponseCanTimeOut() + public function testReadingSyncResponseCanTimeOut() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock', Defaults::CONNECT_TIMEOUT, 1000 ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock', Defaults::CONNECT_TIMEOUT, 1000 ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/sleepWorker.php', $content ); @@ -202,9 +202,9 @@ public function testReadingSyncResponseCanTimeOut() $client->sendRequest( $request ); } - public function testCanHandleReadyResponses() + public function testCanHandleReadyResponses() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -226,9 +226,9 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) } } - public function testCanReadReadyResponses() + public function testCanReadReadyResponses() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -246,9 +246,9 @@ public function testCanReadReadyResponses() $this->expectOutputString( 'unit' ); } - public function testCanWaitForResponse() + public function testCanWaitForResponse() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); @@ -267,9 +267,9 @@ function ( ProvidesResponseData $response ) use ( $unitTest ) $client->waitForResponse( $requestId ); } - public function testReadResponsesSkipsUnknownRequestIds() + public function testReadResponsesSkipsUnknownRequestIds() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); $client = new Client( $connection ); $content = http_build_query( [ 'test-key' => 'unit' ] ); $request = new PostRequest( __DIR__ . '/Workers/worker.php', $content ); diff --git a/tests/Unit/ClientTest.php b/tests/Unit/ClientTest.php index 825e078..66f9a25 100644 --- a/tests/Unit/ClientTest.php +++ b/tests/Unit/ClientTest.php @@ -38,9 +38,9 @@ final class ClientTest extends TestCase /** * @expectedException \hollodotme\FastCGI\Exceptions\ConnectException */ - public function testConnectAttemptToNotExistingSocketThrowsException() + public function testConnectAttemptToNotExistingSocketThrowsException() : void { - $connection = new UnixDomainSocket( 'unix:///tmp/not/existing.sock', 2000, 2000, true, true ); + $connection = new UnixDomainSocket( '/tmp/not/existing.sock', 2000, 2000, true, true ); $client = new Client( $connection ); $client->sendRequest( new PostRequest( '/path/to/script.php', '' ) ); @@ -49,11 +49,11 @@ public function testConnectAttemptToNotExistingSocketThrowsException() /** * @expectedException \hollodotme\FastCGI\Exceptions\ConnectException */ - public function testConnectAttemptToInvalidSocketThrowsException() + public function testConnectAttemptToInvalidSocketThrowsException() : void { $testSocket = realpath( __DIR__ . '/Fixtures/test.sock' ); - $connection = new UnixDomainSocket( 'unix://' . $testSocket ); + $connection = new UnixDomainSocket( '' . $testSocket ); $client = new Client( $connection ); $client->sendRequest( new PostRequest( '/path/to/script.php', '' ) ); @@ -63,7 +63,7 @@ public function testConnectAttemptToInvalidSocketThrowsException() * @expectedException \hollodotme\FastCGI\Exceptions\ReadFailedException * @expectedExceptionMessage Socket not found for request ID: 12345 */ - public function testWaitingForUnknownRequestThrowsException() + public function testWaitingForUnknownRequestThrowsException() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -75,7 +75,7 @@ public function testWaitingForUnknownRequestThrowsException() * @expectedException \hollodotme\FastCGI\Exceptions\ReadFailedException * @expectedExceptionMessage No pending requests found. */ - public function testWaitingForResponsesWithoutRequestsThrowsException() + public function testWaitingForResponsesWithoutRequestsThrowsException() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -87,7 +87,7 @@ public function testWaitingForResponsesWithoutRequestsThrowsException() * @expectedException \hollodotme\FastCGI\Exceptions\ReadFailedException * @expectedExceptionMessage Socket not found for request ID: 12345 */ - public function testHandlingUnknownRequestThrowsException() + public function testHandlingUnknownRequestThrowsException() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -99,7 +99,7 @@ public function testHandlingUnknownRequestThrowsException() * @expectedException \hollodotme\FastCGI\Exceptions\ReadFailedException * @expectedExceptionMessage Socket not found for request ID: 12345 */ - public function testHandlingUnknownRequestsThrowsException() + public function testHandlingUnknownRequestsThrowsException() : void { $connection = new NetworkSocket( '127.0.0.1', 9000 ); $client = new Client( $connection ); @@ -109,11 +109,11 @@ public function testHandlingUnknownRequestsThrowsException() /** * @expectedException \hollodotme\FastCGI\Exceptions\ConnectException - * @expectedExceptionMessageRegExp #^Unable to connect to FastCGI application# + * @expectedExceptionMessageRegExp #.*unable to connect to.*#i */ - public function testConnectAttemptToRestrictedUnixDomainSocketThrowsException() + public function testConnectAttemptToRestrictedUnixDomainSocketThrowsException() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php7.1-ruds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php7.1-ruds.sock' ); $client = new Client( $connection ); $client->sendRequest( new PostRequest( '/path/to/script.php', '' ) ); diff --git a/tests/Unit/Encoders/NameValuePairEncoderTest.php b/tests/Unit/Encoders/NameValuePairEncoderTest.php index 8c156ba..4b26edf 100644 --- a/tests/Unit/Encoders/NameValuePairEncoderTest.php +++ b/tests/Unit/Encoders/NameValuePairEncoderTest.php @@ -24,15 +24,16 @@ namespace hollodotme\FastCGI\Tests\Unit\Encoders; use hollodotme\FastCGI\Encoders\NameValuePairEncoder; +use PHPUnit\Framework\TestCase; -class NameValuePairEncoderTest extends \PHPUnit\Framework\TestCase +class NameValuePairEncoderTest extends TestCase { /** * @param array $pairs * * @dataProvider pairProvider */ - public function testCanEncodeAndDecodePairs( array $pairs ) + public function testCanEncodeAndDecodePairs( array $pairs ) : void { $nameValuePairEncoder = new NameValuePairEncoder(); diff --git a/tests/Unit/Encoders/PacketEncoderTest.php b/tests/Unit/Encoders/PacketEncoderTest.php index 81ec2ad..2f69ee0 100644 --- a/tests/Unit/Encoders/PacketEncoderTest.php +++ b/tests/Unit/Encoders/PacketEncoderTest.php @@ -24,8 +24,9 @@ namespace hollodotme\FastCGI\Tests\Unit\Encoders; use hollodotme\FastCGI\Encoders\PacketEncoder; +use PHPUnit\Framework\TestCase; -class PacketEncoderTest extends \PHPUnit\Framework\TestCase +class PacketEncoderTest extends TestCase { /** * @param int $type @@ -35,7 +36,7 @@ class PacketEncoderTest extends \PHPUnit\Framework\TestCase * * @dataProvider packetContentProvider */ - public function testCanEncodeAndDecodePacket( int $type, string $content, int $requestId, array $expectedHeader ) + public function testCanEncodeAndDecodePacket( int $type, string $content, int $requestId, array $expectedHeader ) : void { $packetEncoder = new PacketEncoder(); diff --git a/tests/Unit/Requests/AbstractRequestTest.php b/tests/Unit/Requests/AbstractRequestTest.php index 6468e82..6bc5bfe 100644 --- a/tests/Unit/Requests/AbstractRequestTest.php +++ b/tests/Unit/Requests/AbstractRequestTest.php @@ -33,7 +33,7 @@ final class AbstractRequestTest extends TestCase * * @dataProvider requestMethodProvider */ - public function testCanGetDefaultValues( string $requestMethod ) + public function testCanGetDefaultValues( string $requestMethod ) : void { $request = $this->getRequest( $requestMethod, '/path/to/script.php', 'Unit-Test' ); @@ -90,7 +90,7 @@ public function requestMethodProvider() : array * * @dataProvider requestMethodProvider */ - public function testCanGetParametersArray( string $requestMethod ) + public function testCanGetParametersArray( string $requestMethod ) : void { $request = $this->getRequest( $requestMethod, '/path/to/script.php', 'Unit-Test' ); $request->setCustomVar( 'UNIT', 'Test' ); @@ -116,7 +116,7 @@ public function testCanGetParametersArray( string $requestMethod ) $this->assertSame( $expectedParams, $request->getParams() ); } - public function testContentLengthChangesWithContent() + public function testContentLengthChangesWithContent() : void { $request = $this->getRequest( 'GET', '/path/to/script.php', 'Some content' ); @@ -127,7 +127,7 @@ public function testContentLengthChangesWithContent() $this->assertSame( 16, $request->getContentLength() ); } - public function testCanOverwriteVars() + public function testCanOverwriteVars() : void { $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); $request->setRemoteAddress( '10.100.10.1' ); @@ -166,7 +166,7 @@ public function testCanOverwriteVars() $this->assertSame( $expectedParams, $request->getParams() ); } - public function testCanResetCustomVars() + public function testCanResetCustomVars() : void { $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); $request->setCustomVar( 'UNIT', 'Test' ); diff --git a/tests/Unit/Requests/DeleteRequestTest.php b/tests/Unit/Requests/DeleteRequestTest.php index 11538d8..963399a 100644 --- a/tests/Unit/Requests/DeleteRequestTest.php +++ b/tests/Unit/Requests/DeleteRequestTest.php @@ -28,7 +28,7 @@ final class DeleteRequestTest extends TestCase { - public function testRequestMethodIsGet() + public function testRequestMethodIsGet() : void { $request = new DeleteRequest( '/path/to/script.php', 'Unit-Test' ); diff --git a/tests/Unit/Requests/GetRequestTest.php b/tests/Unit/Requests/GetRequestTest.php index 4f3a047..5b88920 100644 --- a/tests/Unit/Requests/GetRequestTest.php +++ b/tests/Unit/Requests/GetRequestTest.php @@ -28,7 +28,7 @@ final class GetRequestTest extends TestCase { - public function testRequestMethodIsGet() + public function testRequestMethodIsGet() : void { $request = new GetRequest( '/path/to/script.php', 'Unit-Test' ); diff --git a/tests/Unit/Requests/PatchRequestTest.php b/tests/Unit/Requests/PatchRequestTest.php index 2a62009..e50d852 100644 --- a/tests/Unit/Requests/PatchRequestTest.php +++ b/tests/Unit/Requests/PatchRequestTest.php @@ -28,7 +28,7 @@ final class PatchRequestTest extends TestCase { - public function testRequestMethodIsGet() + public function testRequestMethodIsGet() : void { $request = new PatchRequest( '/path/to/script.php', 'Unit-Test' ); diff --git a/tests/Unit/Requests/PostRequestTest.php b/tests/Unit/Requests/PostRequestTest.php index a97d09f..847013d 100644 --- a/tests/Unit/Requests/PostRequestTest.php +++ b/tests/Unit/Requests/PostRequestTest.php @@ -28,7 +28,7 @@ final class PostRequestTest extends TestCase { - public function testRequestMethodIsPost() + public function testRequestMethodIsPost() : void { $request = new PostRequest( '/path/to/script.php', 'Unit-Test' ); diff --git a/tests/Unit/Requests/PutRequestTest.php b/tests/Unit/Requests/PutRequestTest.php index 3016f34..f3e8d4e 100644 --- a/tests/Unit/Requests/PutRequestTest.php +++ b/tests/Unit/Requests/PutRequestTest.php @@ -28,7 +28,7 @@ final class PutRequestTest extends TestCase { - public function testRequestMethodIsPut() + public function testRequestMethodIsPut() : void { $request = new PutRequest( '/path/to/script.php', 'Unit-Test' ); diff --git a/tests/Unit/Responses/ResponseTest.php b/tests/Unit/Responses/ResponseTest.php index b453db7..b81dd50 100644 --- a/tests/Unit/Responses/ResponseTest.php +++ b/tests/Unit/Responses/ResponseTest.php @@ -28,7 +28,7 @@ final class ResponseTest extends TestCase { - public function testCanGetHeaders() + public function testCanGetHeaders() : void { $rawResponse = "X-Powered-By: PHP/7.1.0\r\n" . "X-Custom: Header\r\n" @@ -51,7 +51,7 @@ public function testCanGetHeaders() $this->assertSame( 'text/html; charset=UTF-8', $response->getHeader( 'Content-type' ) ); } - public function testCanGetBody() + public function testCanGetBody() : void { $rawResponse = "X-Powered-By: PHP/7.1.0\r\n" . "X-Custom: Header\r\n" @@ -68,7 +68,7 @@ public function testCanGetBody() $this->assertSame( $expectedBody, $response->getBody() ); } - public function testCanGetRawResponse() + public function testCanGetRawResponse() : void { $rawResponse = "X-Powered-By: PHP/7.1.0\r\n" . "X-Custom: Header\r\n" diff --git a/tests/Unit/SocketConnections/NetworkSocketTest.php b/tests/Unit/SocketConnections/NetworkSocketTest.php index 8b6a9e6..5d81173 100644 --- a/tests/Unit/SocketConnections/NetworkSocketTest.php +++ b/tests/Unit/SocketConnections/NetworkSocketTest.php @@ -1,4 +1,4 @@ -assertInstanceOf( ConfiguresSocketConnection::class, $connection ); } - public function testCanGetDefaultValues() + public function testCanGetDefaultValues() : void { $connection = new NetworkSocket( 'localhost', 9000 ); - $this->assertSame( 'localhost', $connection->getHost() ); - $this->assertSame( 9000, $connection->getPort() ); + $this->assertSame( 'tcp://localhost:9000', $connection->getSocketAddress() ); $this->assertSame( Defaults::CONNECT_TIMEOUT, $connection->getConnectTimeout() ); $this->assertSame( Defaults::READ_WRITE_TIMEOUT, $connection->getReadWriteTimeout() ); } - public function testCanGetSetValues() + public function testCanGetSetValues() : void { $connection = new NetworkSocket( '127.0.0.1', 9001, 2000, 3000 ); - $this->assertSame( '127.0.0.1', $connection->getHost() ); - $this->assertSame( 9001, $connection->getPort() ); + $this->assertSame( 'tcp://127.0.0.1:9001', $connection->getSocketAddress() ); $this->assertSame( 2000, $connection->getConnectTimeout() ); $this->assertSame( 3000, $connection->getReadWriteTimeout() ); } diff --git a/tests/Unit/SocketConnections/UnixDomainSocketTest.php b/tests/Unit/SocketConnections/UnixDomainSocketTest.php index a3f1ee4..474f132 100644 --- a/tests/Unit/SocketConnections/UnixDomainSocketTest.php +++ b/tests/Unit/SocketConnections/UnixDomainSocketTest.php @@ -1,4 +1,4 @@ -assertInstanceOf( ConfiguresSocketConnection::class, $connection ); } - public function testCanGetDefaultValues() + public function testCanGetDefaultValues() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php/php7.0-fpm.sock' ); + $connection = new UnixDomainSocket( '/var/run/php/php7.1-fpm.sock' ); - $this->assertSame( 'unix:///var/run/php/php7.0-fpm.sock', $connection->getHost() ); - $this->assertSame( -1, $connection->getPort() ); + $this->assertSame( 'unix:///var/run/php/php7.1-fpm.sock', $connection->getSocketAddress() ); $this->assertSame( Defaults::CONNECT_TIMEOUT, $connection->getConnectTimeout() ); $this->assertSame( Defaults::READ_WRITE_TIMEOUT, $connection->getReadWriteTimeout() ); } - public function testCanGetSetValues() + public function testCanGetSetValues() : void { - $connection = new UnixDomainSocket( 'unix:///var/run/php/php7.1-fpm.sock', 2000, 3000 ); + $connection = new UnixDomainSocket( '/var/run/php/php7.1-fpm.sock', 2000, 3000 ); - $this->assertSame( 'unix:///var/run/php/php7.1-fpm.sock', $connection->getHost() ); - $this->assertSame( -1, $connection->getPort() ); + $this->assertSame( 'unix:///var/run/php/php7.1-fpm.sock', $connection->getSocketAddress() ); $this->assertSame( 2000, $connection->getConnectTimeout() ); $this->assertSame( 3000, $connection->getReadWriteTimeout() ); } diff --git a/tests/Unit/SocketTest.php b/tests/Unit/SocketTest.php index 02fa87c..2396a94 100644 --- a/tests/Unit/SocketTest.php +++ b/tests/Unit/SocketTest.php @@ -37,7 +37,7 @@ */ final class SocketTest extends TestCase { - public function testCanGetIdAfterConstruction() + public function testCanGetIdAfterConstruction() : void { $socket = $this->getSocket(); @@ -49,17 +49,17 @@ private function getSocket() : Socket { $nameValuePairEncoder = new NameValuePairEncoder(); $packetEncoder = new PacketEncoder(); - $connection = new UnixDomainSocket( 'unix:///var/run/php-uds.sock' ); + $connection = new UnixDomainSocket( '/var/run/php-uds.sock' ); return new Socket( $connection, $packetEncoder, $nameValuePairEncoder ); } - public function testCanSendRequestAndFetchResponse() + public function testCanSendRequestAndFetchResponse() : void { $socket = $this->getSocket(); $data = [ 'test-key' => 'unit' ]; $request = new PostRequest( - realpath( __DIR__ . '/../Integration/Workers/worker.php' ), + dirname( __DIR__ ) . '/Integration/Workers/worker.php', http_build_query( $data ) ); @@ -74,13 +74,13 @@ public function testCanSendRequestAndFetchResponse() $this->assertSame( $response, $response2 ); } - public function testCanCollectResource() + public function testCanCollectResource() : void { $resources = []; $socket = $this->getSocket(); $data = [ 'test-key' => 'unit' ]; $request = new PostRequest( - realpath( __DIR__ . '/../Integration/Workers/worker.php' ), + dirname( __DIR__ ) . '/Integration/Workers/worker.php', http_build_query( $data ) ); @@ -95,12 +95,12 @@ public function testCanCollectResource() $this->assertInternalType( 'resource', $resources[ $socket->getId() ] ); } - public function testCanNotifyResponseCallback() + public function testCanNotifyResponseCallback() : void { $socket = $this->getSocket(); $data = [ 'test-key' => 'unit' ]; $request = new PostRequest( - realpath( __DIR__ . '/../Integration/Workers/worker.php' ), + dirname( __DIR__ ) . '/Integration/Workers/worker.php', http_build_query( $data ) ); $request->addResponseCallbacks( @@ -117,12 +117,12 @@ function ( ProvidesResponseData $response ) $this->expectOutputString( 'unit' ); } - public function testCanNotifyFailureCallback() + public function testCanNotifyFailureCallback() : void { $socket = $this->getSocket(); $data = [ 'test-key' => 'unit' ]; $request = new PostRequest( - realpath( __DIR__ . '/../Integration/Workers/worker.php' ), + dirname( __DIR__ ) . '/Integration/Workers/worker.php', http_build_query( $data ) ); $request->addFailureCallbacks( diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e7eb1da..65f9788 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -21,4 +21,4 @@ * SOFTWARE. */ -require(__DIR__ . '/../vendor/autoload.php'); +require __DIR__ . '/../vendor/autoload.php';