diff --git a/CHANGELOG.md b/CHANGELOG.md index 79a7456..1410bc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ 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). +## [4.0.0] - t.b.d + +* `AbstractRequest::content` now takes an instance of ComposesRequestContent. Strings can no longer be passed directly as content. Select one of the implementations: JsonData, MultipartFormData or UrlEncodedFormData, or create your own by implementing the interface. +* `AbstractRequest::content` is now nullable. You can create a request with just a scriptFileName and no content. +* `AbstractRequest->setContent()` is removed. Making the request content immutable once set in the constructor. + + ## [3.1.7] - 2021-12-07 * Make sure length values are within valid bounds diff --git a/README.md b/README.md index ac226b7..11b79bf 100644 --- a/README.md +++ b/README.md @@ -656,7 +656,7 @@ $urlEncodedContent = new UrlEncodedFormData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $urlEncodedContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $urlEncodedContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` @@ -713,7 +713,7 @@ $multipartContent = new MultipartFormData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $multipartContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $multipartContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` @@ -812,7 +812,7 @@ $jsonContent = new JsonData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $jsonContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $jsonContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` diff --git a/bin/examples.php b/bin/examples.php index bd57a02..fc5db9f 100644 --- a/bin/examples.php +++ b/bin/examples.php @@ -39,8 +39,6 @@ function printResponse( ProvidesResponseData $response ) $workerPath = __DIR__ . '/exampleWorker.php'; -$request = new PostRequest( $workerPath ); - printLine( "\n" ); printLine( 'hollodotme/fast-cgi-client examples', 'blue', true ); printLine( 'Worker script: ' . $workerPath, 'blue' ); @@ -60,7 +58,7 @@ function printResponse( ProvidesResponseData $response ) printLine( 'CODE: $client->sendRequest( $request );', 'red' ); printLine( "\n" ); -$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single synchronous request'] ) ); +$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single synchronous request'] ) ); sleep( 2 ); @@ -76,7 +74,7 @@ function printResponse( ProvidesResponseData $response ) printLine( 'CODE: $client->sendAsyncRequest( $request );', 'red' ); printLine( "\n" ); -$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request'] ) ); +$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request'] ) ); sleep( 2 ); @@ -110,7 +108,7 @@ function printResponse( ProvidesResponseData $response ) printLine( ' );', 'red' ); printLine( "\n" ); -$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request with callback'] ) ); +$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request with callback'] ) ); $request->addResponseCallbacks( static function ( ProvidesResponseData $response ) { diff --git a/src/Interfaces/ComposesRequestContent.php b/src/Interfaces/ComposesRequestContent.php index 22b4345..75eefad 100644 --- a/src/Interfaces/ComposesRequestContent.php +++ b/src/Interfaces/ComposesRequestContent.php @@ -6,5 +6,5 @@ interface ComposesRequestContent { public function getContentType() : string; - public function getContent() : string; + public function toString() : string; } \ No newline at end of file diff --git a/src/RequestContents/JsonData.php b/src/RequestContents/JsonData.php index 4b5796a..957f810 100644 --- a/src/RequestContents/JsonData.php +++ b/src/RequestContents/JsonData.php @@ -37,7 +37,7 @@ public function getContentType() : string * @return string * @throws RuntimeException */ - public function getContent() : string + public function toString() : string { $json = json_encode( $this->data, $this->encodingOptions, $this->encodingDepth ); diff --git a/src/RequestContents/MultipartFormData.php b/src/RequestContents/MultipartFormData.php index f4432bb..0a6acca 100644 --- a/src/RequestContents/MultipartFormData.php +++ b/src/RequestContents/MultipartFormData.php @@ -63,7 +63,7 @@ public function getContentType() : string return 'multipart/form-data; boundary=' . self::BOUNDARY_ID; } - public function getContent() : string + public function toString() : string { $data = []; diff --git a/src/RequestContents/UrlEncodedFormData.php b/src/RequestContents/UrlEncodedFormData.php index 6c7c1be..a792a63 100644 --- a/src/RequestContents/UrlEncodedFormData.php +++ b/src/RequestContents/UrlEncodedFormData.php @@ -23,7 +23,7 @@ public function getContentType() : string return 'application/x-www-form-urlencoded'; } - public function getContent() : string + public function toString() : string { return http_build_query( $this->formData ); } diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 01523c3..e0d83e9 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -33,8 +33,6 @@ abstract class AbstractRequest implements ProvidesRequestData private string $contentType = 'application/x-www-form-urlencoded'; - private int $contentLength = 0; - private ?ComposesRequestContent $content; /** @var array */ @@ -54,10 +52,10 @@ abstract class AbstractRequest implements ProvidesRequestData public function __construct( string $scriptFilename, ?ComposesRequestContent $content = null ) { $this->scriptFilename = $scriptFilename; + $this->content = $content; if (null !== $content) { - $this->setContent( $content ); - $this->setContentType( $content->getContentType() ); + $this->contentType = $content->getContentType(); } } @@ -131,11 +129,6 @@ public function setServerProtocol( string $serverProtocol ) : void $this->serverProtocol = $serverProtocol; } - public function getContentType() : string - { - return $this->contentType; - } - public function setContentType( string $contentType ) : void { $this->contentType = $contentType; @@ -146,17 +139,17 @@ public function getContent() : ?ComposesRequestContent return $this->content; } - public function setContent( ComposesRequestContent $content ) : void - { - $this->content = $content; - $this->contentLength = strlen( $content->getContent() ); - } + public function getContentLength() : int + { + return $this->content ? strlen($this->content->toString()) : 0; + } - /** - * @param string $key - * @param mixed $value - */ - public function setCustomVar( string $key, $value ) : void + public function getContentType() : string + { + return $this->contentType; + } + + public function setCustomVar( string $key, mixed $value ) : void { $this->customVars[ $key ] = $value; } @@ -192,11 +185,6 @@ public function getScriptFilename() : string return $this->scriptFilename; } - public function getContentLength() : int - { - return $this->contentLength; - } - /** * @return array */ diff --git a/src/Sockets/Socket.php b/src/Sockets/Socket.php index 7198a15..659933b 100644 --- a/src/Sockets/Socket.php +++ b/src/Sockets/Socket.php @@ -346,7 +346,7 @@ private function getRequestPackets( ProvidesRequestData $request ) : string $requestPackets .= $this->packetEncoder->encodePacket( self::STDIN, substr( - $request->getContent()->getContent(), + $request->getContent()->toString(), $offset, self::REQ_MAX_CONTENT_SIZE ), diff --git a/tests/Integration/Async/AsyncRequestsTest.php b/tests/Integration/Async/AsyncRequestsTest.php index 92f7990..8122712 100644 --- a/tests/Integration/Async/AsyncRequestsTest.php +++ b/tests/Integration/Async/AsyncRequestsTest.php @@ -45,17 +45,17 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC $results = []; $expectedResults = range( 0, $limit - 1 ); - $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' ); - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$results ) - { - $results[] = (int)$response->getBody(); - } - ); + for ( $i = 0; $i < $limit; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$results ) + { + $results[] = (int)$response->getBody(); + } + ); $client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request ); } @@ -105,17 +105,15 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC $results = []; $expectedResults = range( 0, $limit - 1 ); - $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' ); - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$results ) - { - $results[] = (int)$response->getBody(); - } - ); - for ( $i = 0; $i < $limit; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$results ) + { + $results[] = (int)$response->getBody(); + } + ); $client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request ); } @@ -161,11 +159,10 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil $results = []; $expectedResults = range( 0, $limit - 1 ); - $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' ); for ( $i = 0; $i < $limit; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) ); $client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request ); } @@ -203,17 +200,17 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil $results = []; $expectedResults = range( 0, $limit - 1 ); - $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' ); - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$results ) - { - $results[] = (int)$response->getBody(); - } - ); + for ( $i = 0; $i < $limit; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$results ) + { + $results[] = (int)$response->getBody(); + } + ); $client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request ); } diff --git a/tests/Integration/FileUpload/FileUploadTest.php b/tests/Integration/FileUpload/FileUploadTest.php index bf45c16..4ab4b65 100644 --- a/tests/Integration/FileUpload/FileUploadTest.php +++ b/tests/Integration/FileUpload/FileUploadTest.php @@ -61,7 +61,7 @@ public function testCanUploadFiles( array $files ) : void ]; $multipartFormData = new MultipartFormData( $formData, $files ); - $postRequest = PostRequest::newWithRequestContent( + $postRequest = new PostRequest( dirname( __DIR__ ) . '/Workers/fileUploadWorker.php', $multipartFormData ); diff --git a/tests/Integration/NetworkSocket/NetworkSocketTest.php b/tests/Integration/NetworkSocket/NetworkSocketTest.php index abfe6ff..922e2b8 100644 --- a/tests/Integration/NetworkSocket/NetworkSocketTest.php +++ b/tests/Integration/NetworkSocket/NetworkSocketTest.php @@ -206,7 +206,7 @@ public function testCanReadResponses() : void $socketIdOne = $this->client->sendAsyncRequest( $this->connection, $request ); - $request->setContent( new UrlEncodedFormData( ['test-key' => 'test'] ) ); + $request = new PostRequest( $this->getWorkerPath( 'worker.php' ), new UrlEncodedFormData( ['test-key' => 'test'] ) ); $socketIdTwo = $this->client->sendAsyncRequest( $this->connection, $request ); @@ -418,12 +418,12 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter ) */ public function testCanGetLengthOfSentContent( int $length ) : void { - $content = str_repeat( 'a', $length ); + $content = new UrlEncodedFormData(['test' => str_repeat( 'a', $length )]); $request = new PostRequest( $this->getWorkerPath( 'lengthWorker.php' ), $content ); $request->setContentType( '*/*' ); $result = $this->client->sendRequest( $this->connection, $request ); - self::assertEquals( $length, $result->getBody() ); + self::assertEquals( $length + 5, $result->getBody() ); } /** diff --git a/tests/Integration/Signals/SignaledWorkersTest.php b/tests/Integration/Signals/SignaledWorkersTest.php index 1472aaa..71630d2 100644 --- a/tests/Integration/Signals/SignaledWorkersTest.php +++ b/tests/Integration/Signals/SignaledWorkersTest.php @@ -50,27 +50,25 @@ private function getWorkerPath( string $workerFile ) : string public function testFailureCallbackGetsCalledIfOneProcessGetsInterruptedOnNetworkSocket( int $signal ) : void { $client = new Client(); - $request = new PostRequest( $this->getWorkerPath( 'worker.php' ) ); $success = []; $failures = []; - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$success ) - { - $success[] = (int)$response->getBody(); - } - ); - - $request->addFailureCallbacks( - static function ( Throwable $e ) use ( &$failures ) - { - $failures[] = $e; - } - ); - for ( $i = 0; $i < 3; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( $this->getWorkerPath( 'worker.php'), new UrlEncodedFormData( ['test-key' => $i] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$success ) + { + $success[] = (int)$response->getBody(); + } + ); + + $request->addFailureCallbacks( + static function ( Throwable $e ) use ( &$failures ) + { + $failures[] = $e; + } + ); $client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request ); } @@ -166,27 +164,25 @@ private function killPoolWorker( int $PID, int $signal ) : void public function testFailureCallbackGetsCalledIfOneProcessGetsInterruptedOnUnixDomainSocket( int $signal ) : void { $client = new Client(); - $request = new PostRequest( $this->getWorkerPath( 'worker.php' ) ); $success = []; $failures = []; - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$success ) - { - $success[] = (int)$response->getBody(); - } - ); - - $request->addFailureCallbacks( - static function ( Throwable $e ) use ( &$failures ) - { - $failures[] = $e; - } - ); - for ( $i = 0; $i < 3; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) ); + $request = new PostRequest( $this->getWorkerPath( 'worker.php' ), new UrlEncodedFormData( ['test-key' => $i] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$success ) + { + $success[] = (int)$response->getBody(); + } + ); + + $request->addFailureCallbacks( + static function ( Throwable $e ) use ( &$failures ) + { + $failures[] = $e; + } + ); $client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request ); } @@ -225,27 +221,25 @@ private function getUnixDomainSocketConnection() : UnixDomainSocket public function testFailureCallbackGetsCalledIfAllProcessesGetInterruptedOnNetworkSocket( int $signal ) : void { $client = new Client(); - $request = new PostRequest( $this->getWorkerPath( 'sleepWorker.php' ) ); $success = []; $failures = []; - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$success ) - { - $success[] = (int)$response->getBody(); - } - ); - - $request->addFailureCallbacks( - static function ( Throwable $e ) use ( &$failures ) - { - $failures[] = $e; - } - ); - for ( $i = 0; $i < 3; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i, 'sleep' => 2] ) ); + $request = new PostRequest( $this->getWorkerPath( 'sleepWorker.php' ), new UrlEncodedFormData( ['test-key' => $i, 'sleep' => 2]) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$success ) + { + $success[] = (int)$response->getBody(); + } + ); + + $request->addFailureCallbacks( + static function ( Throwable $e ) use ( &$failures ) + { + $failures[] = $e; + } + ); $client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request ); } @@ -302,27 +296,25 @@ private function killPoolWorkers( array $PIDs, int $signal ) : void public function testFailureCallbackGetsCalledIfAllProcessesGetInterruptedOnUnixDomainSocket( int $signal ) : void { $client = new Client(); - $request = new PostRequest( $this->getWorkerPath( 'sleepWorker.php' ) ); $success = []; $failures = []; - $request->addResponseCallbacks( - static function ( ProvidesResponseData $response ) use ( &$success ) - { - $success[] = (int)$response->getBody(); - } - ); - - $request->addFailureCallbacks( - static function ( Throwable $e ) use ( &$failures ) - { - $failures[] = $e; - } - ); - for ( $i = 0; $i < 3; $i++ ) { - $request->setContent( new UrlEncodedFormData( ['test-key' => $i, 'sleep' => 1] ) ); + $request = new PostRequest( $this->getWorkerPath( 'sleepWorker.php' ), new UrlEncodedFormData( ['test-key' => $i, 'sleep' => 1] ) ); + $request->addResponseCallbacks( + static function ( ProvidesResponseData $response ) use ( &$success ) + { + $success[] = (int)$response->getBody(); + } + ); + + $request->addFailureCallbacks( + static function ( Throwable $e ) use ( &$failures ) + { + $failures[] = $e; + } + ); $client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request ); } diff --git a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php index 8a6de77..3c28fc5 100644 --- a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php +++ b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php @@ -208,7 +208,7 @@ public function testCanReadResponses() : void $socketIdOne = $this->client->sendAsyncRequest( $this->connection, $request ); - $request->setContent( new UrlEncodedFormData( ['test-key' => 'test'] ) ); + $request = new PostRequest( $this->getWorkerPath( 'worker.php' ), new UrlEncodedFormData( ['test-key' => 'test'] ) ); $socketIdTwo = $this->client->sendAsyncRequest( $this->connection, $request ); @@ -419,7 +419,7 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter ) */ public function testCanGetLengthOfSentContent( int $length ) : void { - $content = str_repeat( 'a', $length ); + $content = new UrlEncodedFormData(['test' => str_repeat( 'a', $length )]); $request = new PostRequest( $this->getWorkerPath( 'lengthWorker.php' ), $content ); $response = $this->client->sendRequest( $this->connection, $request ); diff --git a/tests/Unit/RequestContents/JsonDataTest.php b/tests/Unit/RequestContents/JsonDataTest.php index e9c0451..4994b7f 100644 --- a/tests/Unit/RequestContents/JsonDataTest.php +++ b/tests/Unit/RequestContents/JsonDataTest.php @@ -31,7 +31,7 @@ public function testGetContent() : void */ public function testGetContentType( $data, string $expectedContent ) : void { - self::assertSame( $expectedContent, (new JsonData( $data ))->getContent() ); + self::assertSame( $expectedContent, (new JsonData( $data ))->toString() ); } /** @@ -71,6 +71,6 @@ public function testGetContentThrowsExceptionIfDataCannotBeEncodedAsJson() : voi $data = ['unit' => ['test' => ['level' => ['three' => ['and' => ['more']]]]]]; - self::assertSame( '', (new JsonData( $data, 0, 3 ))->getContent() ); + self::assertSame( '', (new JsonData( $data, 0, 3 ))->toString() ); } } diff --git a/tests/Unit/RequestContents/MultipartFormDataTest.php b/tests/Unit/RequestContents/MultipartFormDataTest.php index 5d629c4..fd65698 100644 --- a/tests/Unit/RequestContents/MultipartFormDataTest.php +++ b/tests/Unit/RequestContents/MultipartFormDataTest.php @@ -36,7 +36,7 @@ public function testAddFile() : void . file_get_contents( __DIR__ . '/_files/php-logo.png' ) . "\r\n" . "--__X_FASTCGI_CLIENT_BOUNDARY__--\r\n\r\n"; - self::assertSame( $expectedContent, $multipartFormData->getContent() ); + self::assertSame( $expectedContent, $multipartFormData->toString() ); } /** @@ -67,7 +67,7 @@ public function testGetContent() : void . file_get_contents( __DIR__ . '/_files/php-logo.png' ) . "\r\n" . "--__X_FASTCGI_CLIENT_BOUNDARY__--\r\n\r\n"; - self::assertSame( $expectedContent, $multipartFormData->getContent() ); + self::assertSame( $expectedContent, $multipartFormData->toString() ); } public function testConstructorThrowsExceptionIfFileDoesNotExist() : void diff --git a/tests/Unit/RequestContents/UrlEncodedFormDataTest.php b/tests/Unit/RequestContents/UrlEncodedFormDataTest.php index 453b231..8610627 100644 --- a/tests/Unit/RequestContents/UrlEncodedFormDataTest.php +++ b/tests/Unit/RequestContents/UrlEncodedFormDataTest.php @@ -27,6 +27,6 @@ public function testGetContent() : void $formData = ['unit' => 'test', 'test' => 'unit']; $expectedContent = 'unit=test&test=unit'; - self::assertSame( $expectedContent, (new UrlEncodedFormData( $formData ))->getContent() ); + self::assertSame( $expectedContent, (new UrlEncodedFormData( $formData ))->toString() ); } } diff --git a/tests/Unit/Requests/AbstractRequestTest.php b/tests/Unit/Requests/AbstractRequestTest.php index 9d75c7c..960a00a 100644 --- a/tests/Unit/Requests/AbstractRequestTest.php +++ b/tests/Unit/Requests/AbstractRequestTest.php @@ -2,6 +2,7 @@ namespace hollodotme\FastCGI\Tests\Unit\Requests; +use hollodotme\FastCGI\Interfaces\ComposesRequestContent; use hollodotme\FastCGI\RequestContents\UrlEncodedFormData; use hollodotme\FastCGI\Requests\AbstractRequest; use PHPUnit\Framework\ExpectationFailedException; @@ -41,19 +42,17 @@ public function testCanGetDefaultValues( string $requestMethod ) : void /** * @param string $requestMethod * @param string $scriptFilename - * @param string $content * * @return AbstractRequest */ - private function getRequest( string $requestMethod, string $scriptFilename ) : AbstractRequest + private function getRequest( string $requestMethod, string $scriptFilename, ?ComposesRequestContent $content = null ) : AbstractRequest { - return new class($requestMethod, $scriptFilename) extends AbstractRequest { - /** @var string */ - private $requestMethod; + return new class($requestMethod, $scriptFilename, $content) extends AbstractRequest { + private string $requestMethod; - public function __construct( string $requestMethod, string $scriptFilename ) + public function __construct( string $requestMethod, string $scriptFilename, ?ComposesRequestContent $content = null ) { - parent::__construct( $scriptFilename ); + parent::__construct( $scriptFilename, $content ); $this->requestMethod = $requestMethod; } @@ -97,7 +96,7 @@ public function requestMethodProvider() : array */ public function testCanGetParametersArray( string $requestMethod ) : void { - $request = $this->getRequest( $requestMethod, '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( $requestMethod, '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setCustomVar( 'UNIT', 'Test' ); $request->setRequestUri( '/unit/test/' ); @@ -121,28 +120,13 @@ public function testCanGetParametersArray( string $requestMethod ) : void self::assertSame( $expectedParams, $request->getParams() ); } - /** - * @throws ExpectationFailedException - * @throws InvalidArgumentException - */ - public function testContentLengthChangesWithContent() : void - { - $request = $this->getRequest( 'GET', '/path/to/script.php', new UrlEncodedFormData( ['test' => 'some content'] ) ); - - self::assertSame( 12, $request->getContentLength() ); - - $request->setContent( new UrlEncodedFormData( ['test' => 'some new content'] ) ); - - self::assertSame( 16, $request->getContentLength() ); - } - /** * @throws ExpectationFailedException * @throws InvalidArgumentException */ public function testCanOverwriteVars() : void { - $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( 'POST', '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setRemoteAddress( '10.100.10.1' ); $request->setRemotePort( 8599 ); $request->setServerSoftware( 'unit/test' ); @@ -185,7 +169,7 @@ public function testCanOverwriteVars() : void */ public function testCanResetCustomVars() : void { - $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( 'POST', '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setCustomVar( 'UNIT', 'Test' ); self::assertSame( ['UNIT' => 'Test'], $request->getCustomVars() ); diff --git a/tests/Unit/Requests/DeleteRequestTest.php b/tests/Unit/Requests/DeleteRequestTest.php index cb99872..67f650e 100644 --- a/tests/Unit/Requests/DeleteRequestTest.php +++ b/tests/Unit/Requests/DeleteRequestTest.php @@ -16,7 +16,7 @@ final class DeleteRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new DeleteRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new DeleteRequest( '/path/to/script.php' ); self::assertSame( 'DELETE', $request->getRequestMethod() ); } @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = DeleteRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new DeleteRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/GetRequestTest.php b/tests/Unit/Requests/GetRequestTest.php index 76f708c..5ab597c 100644 --- a/tests/Unit/Requests/GetRequestTest.php +++ b/tests/Unit/Requests/GetRequestTest.php @@ -16,7 +16,7 @@ final class GetRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new GetRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new GetRequest( '/path/to/script.php' ); self::assertSame( 'GET', $request->getRequestMethod() ); } @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = GetRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new GetRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PatchRequestTest.php b/tests/Unit/Requests/PatchRequestTest.php index fdd52a3..8be05f0 100644 --- a/tests/Unit/Requests/PatchRequestTest.php +++ b/tests/Unit/Requests/PatchRequestTest.php @@ -16,7 +16,7 @@ final class PatchRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new PatchRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PatchRequest( '/path/to/script.php' ); self::assertSame( 'PATCH', $request->getRequestMethod() ); } @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PatchRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PatchRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PostRequestTest.php b/tests/Unit/Requests/PostRequestTest.php index df5fce9..61b764d 100644 --- a/tests/Unit/Requests/PostRequestTest.php +++ b/tests/Unit/Requests/PostRequestTest.php @@ -16,7 +16,7 @@ final class PostRequestTest extends TestCase */ public function testRequestMethodIsPost() : void { - $request = new PostRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PostRequest( '/path/to/script.php' ); self::assertSame( 'POST', $request->getRequestMethod() ); } @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PostRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PostRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PutRequestTest.php b/tests/Unit/Requests/PutRequestTest.php index d042e6d..17f2738 100644 --- a/tests/Unit/Requests/PutRequestTest.php +++ b/tests/Unit/Requests/PutRequestTest.php @@ -16,7 +16,7 @@ final class PutRequestTest extends TestCase */ public function testRequestMethodIsPut() : void { - $request = new PutRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PutRequest( '/path/to/script.php' ); self::assertSame( 'PUT', $request->getRequestMethod() ); } @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PutRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PutRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Sockets/SocketCollectionTest.php b/tests/Unit/Sockets/SocketCollectionTest.php index 9529586..09ca340 100644 --- a/tests/Unit/Sockets/SocketCollectionTest.php +++ b/tests/Unit/Sockets/SocketCollectionTest.php @@ -246,9 +246,7 @@ public function testSocketWithResponseIsIdle() : void ); $request = new PostRequest( - dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php', - '' - ); + dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php' ); $socket->sendRequest( $request ); /** @noinspection UnusedFunctionResultInspection */ @@ -281,7 +279,7 @@ public function testBusySocketIsNotIdle() : void ); $socket->sendRequest( - new PostRequest( '/some/script.php', '' ) + new PostRequest( '/some/script.php' ) ); self::assertNull( $this->collection->getIdleSocket( $connection ) ); @@ -487,7 +485,7 @@ public function testHasBusySockets() : void ); $socket->sendRequest( - new PostRequest( '/some/sctipt.php', '' ) + new PostRequest( '/some/sctipt.php' ) ); self::assertTrue( $this->collection->hasBusySockets() ); diff --git a/tests/Unit/Sockets/SocketTest.php b/tests/Unit/Sockets/SocketTest.php index 737acc3..f67ae27 100644 --- a/tests/Unit/Sockets/SocketTest.php +++ b/tests/Unit/Sockets/SocketTest.php @@ -10,6 +10,7 @@ use hollodotme\FastCGI\Exceptions\TimedoutException; use hollodotme\FastCGI\Exceptions\WriteFailedException; use hollodotme\FastCGI\Interfaces\ProvidesResponseData; +use hollodotme\FastCGI\RequestContents\UrlEncodedFormData; use hollodotme\FastCGI\Requests\PostRequest; use hollodotme\FastCGI\SocketConnections\Defaults; use hollodotme\FastCGI\SocketConnections\NetworkSocket; @@ -79,7 +80,7 @@ public function testCanSendRequestAndFetchResponse() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $socket->sendRequest( $request ); @@ -108,7 +109,7 @@ public function testCanCollectResource() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $socket->collectResource( $resources ); @@ -135,7 +136,7 @@ public function testCanNotifyResponseCallback() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $request->addResponseCallbacks( static function ( ProvidesResponseData $response ) @@ -163,7 +164,7 @@ public function testCanNotifyFailureCallback() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $request->addFailureCallbacks( static function ( Throwable $throwable ) @@ -284,7 +285,7 @@ public function testIsUsableWhenInInitialState() : void public function testIsNotUsableWhenTimedOut() : void { $socket = $this->getSocket(); - $content = http_build_query( ['sleep' => 1, 'test-key' => 'unit'] ); + $content = new UrlEncodedFormData( ['sleep' => 1, 'test-key' => 'unit'] ); $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php', $content ); $socket->sendRequest( $request );