From f90cf1731c192d690d9b66050300fca199276688 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 24 Aug 2025 07:16:29 -0400 Subject: [PATCH 1/3] test(http): Extract routing from `ApplicationTest` to `ApplicationRoutingTest` class. --- .../http/stateless/ApplicationRoutingTest.php | 164 ++++++++++++++++++ tests/http/stateless/ApplicationTest.php | 145 ---------------- 2 files changed, 164 insertions(+), 145 deletions(-) create mode 100644 tests/http/stateless/ApplicationRoutingTest.php diff --git a/tests/http/stateless/ApplicationRoutingTest.php b/tests/http/stateless/ApplicationRoutingTest.php new file mode 100644 index 00000000..0873bd4e --- /dev/null +++ b/tests/http/stateless/ApplicationRoutingTest.php @@ -0,0 +1,164 @@ + 'bar', + 'a' => [ + 'b' => 'c', + ], + ]; + $_SERVER = [ + 'REQUEST_METHOD' => 'POST', + 'REQUEST_URI' => 'site/post', + ]; + + $app = $this->statelessApplication(); + + $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + + self::assertSame( + 200, + $response->getStatusCode(), + "Expected HTTP '200' for route 'site/post'.", + ); + self::assertSame( + 'application/json; charset=UTF-8', + $response->getHeaderLine('Content-Type'), + "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/post'.", + ); + self::assertSame( + <<getBody()->getContents(), + "Response body should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . + "'site/post' route.", + ); + } + + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testHandleQueryParameters(): void + { + $_GET = [ + 'foo' => 'bar', + 'a' => [ + 'b' => 'c', + ], + ]; + $_SERVER = [ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => 'site/get', + ]; + + $app = $this->statelessApplication(); + + $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + + self::assertSame( + 200, + $response->getStatusCode(), + "Expected HTTP '200' for route 'site/get'.", + ); + self::assertSame( + 'application/json; charset=UTF-8', + $response->getHeaderLine('Content-Type'), + "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/get'.", + ); + self::assertSame( + <<getBody()->getContents(), + "Response body should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . + "'site/get' route.", + ); + } + + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testHandleRouteAndQueryParameters(): void + { + $_GET = ['q' => '1']; + $_SERVER = [ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => 'site/query/foo?q=1', + ]; + + $app = $this->statelessApplication(); + + $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + + self::assertSame( + 200, + $response->getStatusCode(), + "Expected HTTP '200' for route 'site/query/foo?q=1'.", + ); + self::assertSame( + 'application/json; charset=UTF-8', + $response->getHeaderLine('Content-Type'), + "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/query/foo?q=1'.", + ); + self::assertSame( + '{"test":"foo","q":"1","queryParams":{"test":"foo","q":"1"}}', + $response->getBody()->getContents(), + "Response body should contain valid JSON with route and query parameters for 'site/query/foo?q=1' route.", + ); + } + + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testHandleRouteParameters(): void + { + $_SERVER = [ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => 'site/update/123', + ]; + + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + + $app = $this->statelessApplication(); + + $response = $app->handle($request); + + self::assertSame( + 200, + $response->getStatusCode(), + "Expected HTTP '200' for route 'site/update/123'.", + ); + self::assertSame( + 'application/json; charset=UTF-8', + $response->getHeaderLine('Content-Type'), + "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/update/123'.", + ); + self::assertSame( + '{"site/update":"123"}', + $response->getBody()->getContents(), + "Response body should contain valid JSON with the route parameter for 'site/update/123' route.", + ); + self::assertSame( + 'site/update/123', + $request->getUri()->getPath(), + "Request path should be 'site/update/123' for 'site/update/123' route.", + ); + } +} diff --git a/tests/http/stateless/ApplicationTest.php b/tests/http/stateless/ApplicationTest.php index 642baa5e..63a02df0 100644 --- a/tests/http/stateless/ApplicationTest.php +++ b/tests/http/stateless/ApplicationTest.php @@ -84,151 +84,6 @@ public function testRenderExceptionSetsDisplayErrorsInDebugMode(): void } } - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testReturnJsonResponseWithPostParametersForSitePostRoute(): void - { - $_POST = [ - 'foo' => 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'POST', - 'REQUEST_URI' => 'site/post', - ]; - - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - - self::assertSame( - 200, - $response->getStatusCode(), - "Response 'status code' should be '200' for 'site/post' route in 'StatelessApplication'.", - ); - self::assertSame( - <<getBody()->getContents(), - "Response 'body' should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . - "'site/post' route in 'StatelessApplication'.", - ); - } - - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testReturnJsonResponseWithQueryParametersForSiteGetRoute(): void - { - $_GET = [ - 'foo' => 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/get', - ]; - - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - - self::assertSame( - 200, - $response->getStatusCode(), - "Response 'status code' should be '200' for 'site/get' route in 'StatelessApplication'.", - ); - self::assertSame( - <<getBody()->getContents(), - "Response 'body' should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . - "'site/get' route in 'StatelessApplication'.", - ); - } - - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testReturnJsonResponseWithQueryParamsForSiteQueryRoute(): void - { - $_GET = ['q' => '1']; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/query/foo?q=1', - ]; - - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - - self::assertSame( - 200, - $response->getStatusCode(), - "Response 'status code' should be '200' for 'site/query/foo?q=1' route in 'StatelessApplication'.", - ); - self::assertSame( - 'application/json; charset=UTF-8', - $response->getHeaderLine('Content-Type'), - "Response 'Content-Type' should be 'application/json; charset=UTF-8' for 'site/query/foo?q=1' route in " . - "'StatelessApplication'.", - ); - self::assertSame( - '{"test":"foo","q":"1","queryParams":{"test":"foo","q":"1"}}', - $response->getBody()->getContents(), - "Response 'body' should contain valid JSON with route and query parameters for 'site/query/foo?q=1' in " . - "'StatelessApplication'.", - ); - } - - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testReturnJsonResponseWithRouteParameterForSiteUpdateRoute(): void - { - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/update/123', - ]; - - $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); - - $app = $this->statelessApplication(); - - $response = $app->handle($request); - - self::assertSame( - 200, - $response->getStatusCode(), - "Response 'status code' should be '200' for 'site/update/123' route in 'StatelessApplication', " . - 'indicating a successful update.', - ); - self::assertSame( - 'application/json; charset=UTF-8', - $response->getHeaderLine('Content-Type'), - "Response 'Content-Type' should be 'application/json; charset=UTF-8' for 'site/update/123' route in " . - "'StatelessApplication'.", - ); - self::assertSame( - '{"site/update":"123"}', - $response->getBody()->getContents(), - "Response 'body' should contain valid JSON with the route parameter for 'site/update/123' in " . - "'StatelessApplication'.", - ); - self::assertSame( - 'site/update/123', - $request->getUri()->getPath(), - "Request 'path' should be 'site/update/123' for 'site/update/123' route in 'StatelessApplication'.", - ); - } - /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ From 9196056f4201e3c70c4302231c42b1b03dfb256f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 24 Aug 2025 08:52:46 -0400 Subject: [PATCH 2/3] Apply fixed review coderabbitai nitpick comments. --- .../http/stateless/ApplicationRoutingTest.php | 71 ++++++++----------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/tests/http/stateless/ApplicationRoutingTest.php b/tests/http/stateless/ApplicationRoutingTest.php index 0873bd4e..73255172 100644 --- a/tests/http/stateless/ApplicationRoutingTest.php +++ b/tests/http/stateless/ApplicationRoutingTest.php @@ -17,20 +17,19 @@ final class ApplicationRoutingTest extends TestCase */ public function testHandlePostParameters(): void { - $_POST = [ - 'foo' => 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'POST', - 'REQUEST_URI' => 'site/post', - ]; - $app = $this->statelessApplication(); - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $response = $app->handle( + FactoryHelper::createRequest( + method: 'POST', + uri: '/site/post', + headers: ['Content-Type' => 'application/x-www-form-urlencoded'], + parsedBody: [ + 'foo' => 'bar', + 'a' => ['b' => 'c'], + ], + ), + ); self::assertSame( 200, @@ -42,7 +41,7 @@ public function testHandlePostParameters(): void $response->getHeaderLine('Content-Type'), "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/post'.", ); - self::assertSame( + self::assertJsonStringEqualsJsonString( << 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/get', - ]; - $app = $this->statelessApplication(); - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $response = $app->handle( + FactoryHelper::createRequest( + method: 'GET', + uri: '/site/get?foo=bar&a[b]=c', + ), + ); self::assertSame( 200, @@ -82,7 +75,7 @@ public function testHandleQueryParameters(): void $response->getHeaderLine('Content-Type'), "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/get'.", ); - self::assertSame( + self::assertJsonStringEqualsJsonString( << '1']; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/query/foo?q=1', - ]; - $app = $this->statelessApplication(); - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $response = $app->handle( + FactoryHelper::createRequest( + method: 'GET', + uri: '/site/query/foo?q=1', + ), + ); self::assertSame( 200, @@ -117,8 +109,10 @@ public function testHandleRouteAndQueryParameters(): void $response->getHeaderLine('Content-Type'), "Expected Content-Type 'application/json; charset=UTF-8' for route 'site/query/foo?q=1'.", ); - self::assertSame( - '{"test":"foo","q":"1","queryParams":{"test":"foo","q":"1"}}', + self::assertJsonStringEqualsJsonString( + <<getBody()->getContents(), "Response body should contain valid JSON with route and query parameters for 'site/query/foo?q=1' route.", ); @@ -129,12 +123,7 @@ public function testHandleRouteAndQueryParameters(): void */ public function testHandleRouteParameters(): void { - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/update/123', - ]; - - $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + $request = FactoryHelper::createRequest(method: 'GET', uri: 'site/update/123'); $app = $this->statelessApplication(); From e247b2b1eb57fd6911a59dbc2386bb394859ffda Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 24 Aug 2025 12:53:16 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- tests/http/stateless/ApplicationRoutingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/http/stateless/ApplicationRoutingTest.php b/tests/http/stateless/ApplicationRoutingTest.php index 73255172..870f9983 100644 --- a/tests/http/stateless/ApplicationRoutingTest.php +++ b/tests/http/stateless/ApplicationRoutingTest.php @@ -94,7 +94,7 @@ public function testHandleRouteAndQueryParameters(): void $response = $app->handle( FactoryHelper::createRequest( - method: 'GET', + method: 'GET', uri: '/site/query/foo?q=1', ), );