|
| 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