Skip to content

Commit ac9a229

Browse files
committed
test: dispatch namespace tests (not dispatcher)
1 parent 4e9d84f commit ac9a229

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

src/Dispatch/Dispatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use GT\DomTemplate\PlaceholderBinder;
1818
use GT\DomTemplate\TableBinder;
1919
use GT\Http\Header\ResponseHeaders;
20-
use GT\Http\Request;
21-
use GT\Http\Response;
20+
use Gt\Http\Request;
21+
use Gt\Http\Response;
2222
use Gt\Http\ResponseStatusException\ClientError\HttpNotFound;
2323
use Gt\Http\ResponseStatusException\ResponseStatusException;
2424
use Gt\Http\StatusCode;
@@ -129,7 +129,7 @@ public function __construct(
129129
$requestInit = $requestInit ?? new RequestInit(
130130
$pathNormaliser,
131131
$request->getUri(),
132-
$config->getBool("app.force_trailing_slash"),
132+
$config->getBool("app.force_trailing_slash") ?? true,
133133
$this->response->redirect(...),
134134
$this->globals["_GET"],
135135
$this->globals["_POST"],
@@ -407,7 +407,7 @@ public function processResponse(bool $processingError = false):void {
407407
}
408408
}
409409

410-
if($responseWithHeader = $this->headerManager->apply(
410+
if($responseWithHeader = $this->headerManager->applyWithHeader(
411411
$this->response->getResponseHeaders(),
412412
$this->response->withHeader(...)
413413
)) {

src/Dispatch/HeaderManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use GT\Http\Response;
77

88
class HeaderManager {
9-
public function apply(ResponseHeaders $responseHeaders, Closure $withHeaderCallback):?Response {
9+
public function applyWithHeader(ResponseHeaders $responseHeaders, Closure $withHeaderCallback):?Response {
1010
$response = null;
1111

1212
foreach($responseHeaders->asArray() as $name => $value) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace GT\WebEngine\Test\Dispatch;
3+
4+
use Gt\Config\Config;
5+
use Gt\Http\Request;
6+
use Gt\Http\ResponseStatusException\ClientError\HttpNotFound;
7+
use Gt\Http\Uri;
8+
use Gt\Session\FileHandler;
9+
use GT\WebEngine\Dispatch\DispatcherFactory;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class DispatcherFactoryTest extends TestCase {
13+
public function testCreate():void {
14+
$config = self::createMock(Config::class);
15+
$config->method("getString")
16+
->willReturnMap([
17+
["app.namespace", "Example\\NS"],
18+
["app.class_dir", "/tmp/phpgt-webengine-test--dispatcher-factory--class"],
19+
["router.router_file", "/tmp/phpgt-webengine-test--dispatcher-factory--router"],
20+
["router.router_class", "TestRouter"],
21+
["router.default_content_type", "unit/test"],
22+
["session.name", "GT_Test_Session"],
23+
["session.handler", FileHandler::class],
24+
["session.path", "/"],
25+
["view.component_directory", "/tmp/phpgt-webengine-test--dispatcher-factory--component-dir"],
26+
["view.partial_directory", "/tmp/phpgt-webengine-test--dispatcher-factory--partial-dir"],
27+
]);
28+
$config->method("getInt")
29+
->willReturnMap([
30+
["router.redirect_response_code", 321],
31+
]);
32+
33+
$requestUri = self::createMock(Uri::class);
34+
$requestUri->method("getPath")
35+
->willReturn("/test/");
36+
$request = self::createMock(Request::class);
37+
$request->method("getUri")
38+
->willReturn($requestUri);
39+
$request->method("getMethod")
40+
->willReturn("GET");
41+
$globals = [
42+
"_GET" => [],
43+
"_POST" => [],
44+
"_FILES" => [],
45+
"_SERVER" => [],
46+
"_COOKIE" => [],
47+
];
48+
$finishCallback = fn() => null;
49+
$errorStatus = 123;
50+
51+
$sut = new DispatcherFactory();
52+
$dispatcher = $sut->create($config, $request, $globals, $finishCallback, $errorStatus);
53+
self::expectException(HttpNotFound::class);
54+
$dispatcher->generateResponse();
55+
}
56+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace GT\WebEngine\Test\Dispatch;
3+
4+
use Gt\Http\Header\ResponseHeaders;
5+
use GT\WebEngine\Dispatch\HeaderManager;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class HeaderManagerTest extends TestCase {
9+
public function testApplyWithHeader():void {
10+
$testHeaderArray = [
11+
"X-PHPGT-TEST" => uniqid(),
12+
"X-TIMESTAMP" => time(),
13+
];
14+
15+
$callbackParameterCalls = [];
16+
$callback = function($name, $value) use(&$callbackParameterCalls) {
17+
array_push($callbackParameterCalls, [$name, $value]);
18+
};
19+
20+
$responseHeaders = self::createMock(ResponseHeaders::class);
21+
$responseHeaders->expects(self::once())
22+
->method("asArray")
23+
->willReturn($testHeaderArray);
24+
25+
$sut = new HeaderManager();
26+
$sut->applyWithHeader($responseHeaders, $callback);
27+
28+
self::assertCount(count($testHeaderArray), $callbackParameterCalls);
29+
$index = 0;
30+
foreach($testHeaderArray as $key => $value) {
31+
$actualCallbackParameters = $callbackParameterCalls[$index];
32+
self::assertSame($key, $actualCallbackParameters[0]);
33+
self::assertSame($value, $actualCallbackParameters[1]);
34+
$index++;
35+
}
36+
}
37+
}

test/phpunit/Dispatch/RouterFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ private function writeRouterFile(string $namespace, string $className, string $f
3737
class %CLASS% extends BaseRouter {
3838
public ?RouterConfig $receivedConfig = null;
3939
public ?Container $receivedContainer = null;
40-
public function __construct(RouterConfig $config) {
41-
parent::__construct($config);
40+
public function __construct(RouterConfig $config, ?int $errorStatus) {
41+
parent::__construct($config, errorStatus: $errorStatus);
4242
$this->receivedConfig = $config;
4343
}
4444
public function setContainer(Container $container):void {

0 commit comments

Comments
 (0)