Skip to content

Commit 916cdb5

Browse files
committed
test: init namespace tests
1 parent b080898 commit 916cdb5

File tree

10 files changed

+373
-54
lines changed

10 files changed

+373
-54
lines changed

composer.lock

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.4/phpunit.xsd"
55
colors="true"
66
cacheDirectory="test/phpunit/.phpunit.cache"
77
bootstrap="vendor/autoload.php"
@@ -18,8 +18,6 @@
1818
</testsuite>
1919
</testsuites>
2020

21-
<logging />
22-
2321
<source>
2422
<include>
2523
<directory>

src/Init/RequestInit.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
namespace GT\WebEngine\Init;
33

44
use Closure;
5-
use GT\Input\Input;
6-
use GT\Http\Uri;
7-
use GT\Http\ServerInfo;
5+
use Gt\Input\Input;
6+
use Gt\Http\Uri;
7+
use Gt\Http\ServerInfo;
88
use GT\WebEngine\Dispatch\PathNormaliser;
99

1010
class RequestInit {

src/Init/RouterInit.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ public function __construct(
5151
);
5252
$this->baseRouter->route($request);
5353
$viewClass = $this->baseRouter->getViewClass() ?? NullView::class;
54+
55+
// @codeCoverageIgnoreStart
5456
if(str_starts_with($viewClass, "Gt\\")) {
5557
$viewClass = str_replace("Gt\\", "GT\\", $viewClass);
5658
}
59+
// @codeCoverageIgnoreEnd
60+
5761
$this->view = new $viewClass($response->getBody());
5862

5963
$this->viewAssembly = $this->baseRouter->getViewAssembly();

src/Init/SessionInit.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public function __construct(
1717
bool $useTransSid,
1818
bool $useCookies,
1919
array $currentCookieArray,
20+
?SessionSetup $sessionSetup = null,
21+
string|Session $sessionClass = Session::class,
2022
) {
2123
$originalCookie = $_COOKIE;
2224
$_COOKIE = $currentCookieArray;
@@ -30,14 +32,21 @@ public function __construct(
3032
];
3133

3234
$sessionId = $_COOKIE[$sessionConfig["name"]] ?? null;
33-
$sessionSetup = new SessionSetup();
35+
$sessionSetup = $sessionSetup ?? new SessionSetup();
3436
$sessionHandler = $sessionSetup->attachHandler($sessionConfig["handler"]);
3537

36-
$this->session = new Session(
37-
$sessionHandler,
38-
$sessionConfig,
39-
$sessionId,
40-
);
38+
if($sessionClass instanceof Session) {
39+
$this->session = $sessionClass;
40+
}
41+
else {
42+
// @codeCoverageIgnoreStart
43+
$this->session = new $sessionClass(
44+
$sessionHandler,
45+
$sessionConfig,
46+
$sessionId,
47+
);
48+
// @codeCoverageIgnoreEnd
49+
}
4150

4251
$_COOKIE = $originalCookie;
4352
}

test/phpunit/ApplicationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public function testStart_callsRedirectExecute():void {
3535
$requestFactory->method("createServerRequestFromGlobalState")
3636
->willReturn($serverRequest);
3737
$dispatcher = self::createMock(Dispatcher::class);
38+
3839
$response = self::createMock(Response::class);
3940
$response->method('getStatusCode')->willReturn(200);
4041
$response->method('getHeaders')->willReturn(['Content-Type' => ['text/html']]);
4142
$response->method('getBody')->willReturn(new \Gt\Http\Stream());
4243
$dispatcher->method('generateResponse')->willReturn($response);
44+
4345
$dispatcherFactory = self::createMock(DispatcherFactory::class);
4446
$dispatcherFactory->method('create')->willReturn($dispatcher);
4547

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace GT\WebEngine\Test\Init;
3+
4+
use Closure;
5+
use Gt\Http\ServerInfo;
6+
use Gt\Http\Uri;
7+
use Gt\Input\Input;
8+
use GT\WebEngine\Init\RequestInit;
9+
use GT\WebEngine\Dispatch\PathNormaliser;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class RequestInitTest extends TestCase {
13+
public function testConstructor_normalisesPath_andBuildsInputAndServerInfo():void {
14+
$pathNormaliser = self::createMock(PathNormaliser::class);
15+
16+
$uri = self::createMock(Uri::class);
17+
$uri->method("getPath")
18+
->willReturn("https://example.test/section");
19+
20+
$forceTrailing = true;
21+
$redirectCalled = false;
22+
$redirectArg = null;
23+
$redirect = function($u) use (&$redirectCalled, &$redirectArg) {
24+
$redirectCalled = true;
25+
$redirectArg = $u;
26+
};
27+
28+
$pathNormaliser->expects(self::once())
29+
->method('normaliseTrailingSlash')
30+
->with($uri, $forceTrailing, self::isInstanceOf(Closure::class))
31+
->willReturnCallback(function(Uri $u, bool $force, Closure $redirectCallback) {
32+
// Call the provided redirect to simulate a normalisation.
33+
$redirectCallback($u);
34+
});
35+
36+
$get = ['a' => '1'];
37+
$post = ['b' => '2'];
38+
$tmp = tempnam(sys_get_temp_dir(), 'upload');
39+
file_put_contents($tmp, 'x');
40+
$files = [
41+
'f' => [
42+
'name' => 'x.txt',
43+
'type' => 'text/plain',
44+
'tmp_name' => $tmp,
45+
'error' => 0,
46+
'size' => 1,
47+
],
48+
];
49+
$server = ['REQUEST_URI' => '/section'];
50+
51+
$sut = new RequestInit(
52+
$pathNormaliser,
53+
$uri,
54+
$forceTrailing,
55+
$redirect,
56+
$get,
57+
$post,
58+
$files,
59+
$server,
60+
);
61+
62+
// Assert redirect path normalisation collaboration occurred
63+
self::assertTrue($redirectCalled);
64+
self::assertSame($uri, $redirectArg);
65+
66+
// Call getters to ensure full coverage and type expectations
67+
$input = $sut->getInput();
68+
$serverInfo = $sut->getServerInfo();
69+
self::assertInstanceOf(Input::class, $input);
70+
self::assertInstanceOf(ServerInfo::class, $serverInfo);
71+
self::assertSame('/section', $serverInfo->getRequestUri()->getPath());
72+
}
73+
}

0 commit comments

Comments
 (0)