Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions tests/http/stateless/ApplicationRoutingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

namespace yii2\extensions\psrbridge\tests\http\stateless;

use PHPUnit\Framework\Attributes\Group;
use yii\base\InvalidConfigException;
use yii2\extensions\psrbridge\tests\support\FactoryHelper;
use yii2\extensions\psrbridge\tests\TestCase;

#[Group('http')]
final class ApplicationRoutingTest extends TestCase
{
/**
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
public function testHandlePostParameters(): void
{
$app = $this->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(
<<<JSON
{"foo":"bar","a":{"b":"c"}}
JSON,
$response->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(
<<<JSON
{"foo":"bar","a":{"b":"c"}}
JSON,
$response->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(
<<<JSON
{"test":"foo","q":"1","queryParams":{"test":"foo","q":"1"}}
JSON,
$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
{
$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.",
);
}
}
145 changes: 0 additions & 145 deletions tests/http/stateless/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<JSON
{"foo":"bar","a":{"b":"c"}}
JSON,
$response->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(
<<<JSON
{"foo":"bar","a":{"b":"c"}}
JSON,
$response->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.
*/
Expand Down
Loading