|
| 1 | +<?php |
| 2 | +namespace GT\WebEngine\Test; |
| 3 | + |
| 4 | +use Gt\Http\Request; |
| 5 | +use Gt\Http\Uri; |
| 6 | +use GT\Routing\RouterConfig; |
| 7 | +use GT\WebEngine\DefaultRouter; |
| 8 | +use Gt\ServiceContainer\Container; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +require_once dirname(__DIR__, 2) . "/router.default.php"; |
| 12 | + |
| 13 | +class DefaultRouterTest extends TestCase { |
| 14 | + private string $tmpDir; |
| 15 | + private string $cwd; |
| 16 | + |
| 17 | + protected function setUp():void { |
| 18 | + parent::setUp(); |
| 19 | + $this->cwd = getcwd(); |
| 20 | + $this->tmpDir = sys_get_temp_dir() . "/phpgt-webengine-test--DefaultRouter-" . uniqid(); |
| 21 | + mkdir($this->tmpDir . "/page/admin", recursive: true); |
| 22 | + } |
| 23 | + |
| 24 | + protected function tearDown():void { |
| 25 | + chdir($this->cwd); |
| 26 | + $this->removeDirectory($this->tmpDir); |
| 27 | + parent::tearDown(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testRoute_pageRequest_includesHeadersAndFootersInNestedOrder():void { |
| 31 | + file_put_contents($this->tmpDir . "/page/_header.html", "<html><body><header>site</header>"); |
| 32 | + file_put_contents($this->tmpDir . "/page/admin/_header.html", "<nav>admin</nav>"); |
| 33 | + file_put_contents($this->tmpDir . "/page/admin/users.html", "<main>users</main>"); |
| 34 | + file_put_contents($this->tmpDir . "/page/admin/_footer.html", "<footer>admin</footer>"); |
| 35 | + file_put_contents($this->tmpDir . "/page/_footer.html", "<footer>site</footer></body></html>"); |
| 36 | + |
| 37 | + chdir($this->tmpDir); |
| 38 | + |
| 39 | + $request = self::createMock(Request::class); |
| 40 | + $request->method("getMethod")->willReturn("GET"); |
| 41 | + $request->method("getHeaderLine") |
| 42 | + ->with("accept") |
| 43 | + ->willReturn("text/html"); |
| 44 | + $request->method("getUri")->willReturn(new Uri("https://example.test/admin/users")); |
| 45 | + |
| 46 | + $sut = new DefaultRouter(new RouterConfig(307, "text/html")); |
| 47 | + $container = new Container(); |
| 48 | + $container->set($request); |
| 49 | + $sut->setContainer($container); |
| 50 | + $sut->route($request); |
| 51 | + |
| 52 | + self::assertSame( |
| 53 | + [ |
| 54 | + "page/_header.html", |
| 55 | + "page/admin/_header.html", |
| 56 | + "page/admin/users.html", |
| 57 | + "page/admin/_footer.html", |
| 58 | + "page/_footer.html", |
| 59 | + ], |
| 60 | + iterator_to_array($sut->getViewAssembly()), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + private function removeDirectory(string $dir):void { |
| 65 | + if(!is_dir($dir)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + foreach(scandir($dir) ?: [] as $file) { |
| 70 | + if($file === "." || $file === "..") { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $path = $dir . DIRECTORY_SEPARATOR . $file; |
| 75 | + if(is_dir($path)) { |
| 76 | + $this->removeDirectory($path); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + unlink($path); |
| 81 | + } |
| 82 | + |
| 83 | + rmdir($dir); |
| 84 | + } |
| 85 | +} |
0 commit comments