diff --git a/tests/http/stateless/ApplicationRoutingTest.php b/tests/http/stateless/ApplicationRoutingTest.php new file mode 100644 index 00000000..870f9983 --- /dev/null +++ b/tests/http/stateless/ApplicationRoutingTest.php @@ -0,0 +1,153 @@ +statelessApplication(); + + $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, + $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::assertJsonStringEqualsJsonString( + <<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 + { + $app = $this->statelessApplication(); + + $response = $app->handle( + FactoryHelper::createRequest( + method: 'GET', + uri: '/site/get?foo=bar&a[b]=c', + ), + ); + + 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::assertJsonStringEqualsJsonString( + <<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 + { + $app = $this->statelessApplication(); + + $response = $app->handle( + FactoryHelper::createRequest( + method: 'GET', + uri: '/site/query/foo?q=1', + ), + ); + + 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::assertJsonStringEqualsJsonString( + <<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 + { + $request = FactoryHelper::createRequest(method: 'GET', uri: 'site/update/123'); + + $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. */