|
| 1 | +<?php |
| 2 | +namespace GT\WebEngine\Test; |
| 3 | + |
| 4 | +use GT\Dom\HTMLDocument; |
| 5 | +use GT\DomTemplate\ComponentExpander; |
| 6 | +use GT\DomTemplate\PartialContent; |
| 7 | +use GT\DomTemplate\PartialContentDirectoryNotFoundException; |
| 8 | +use GT\DomTemplate\PartialExpander; |
| 9 | +use Gt\Http\Request; |
| 10 | +use Gt\Http\Stream; |
| 11 | +use Gt\Http\Uri; |
| 12 | +use GT\WebEngine\Logic\HTMLDocumentProcessor; |
| 13 | +use GT\Routing\RouterConfig; |
| 14 | +use GT\WebEngine\DefaultRouter; |
| 15 | +use Gt\ServiceContainer\Container; |
| 16 | +use GT\WebEngine\View\HeaderFooterPartialConflictException; |
| 17 | +use GT\WebEngine\View\HTMLView; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +require_once dirname(__DIR__, 2) . "/router.default.php"; |
| 21 | + |
| 22 | +class DefaultRouterTest extends TestCase { |
| 23 | + private string $tmpDir; |
| 24 | + private string $cwd; |
| 25 | + |
| 26 | + protected function setUp():void { |
| 27 | + parent::setUp(); |
| 28 | + $this->cwd = getcwd(); |
| 29 | + $this->tmpDir = sys_get_temp_dir() . "/phpgt-webengine-test--DefaultRouter-" . uniqid(); |
| 30 | + mkdir($this->tmpDir . "/page/admin", recursive: true); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown():void { |
| 34 | + chdir($this->cwd); |
| 35 | + $this->removeDirectory($this->tmpDir); |
| 36 | + parent::tearDown(); |
| 37 | + } |
| 38 | + |
| 39 | + public function testRoute_pageRequest_includesHeadersAndFootersInNestedOrder():void { |
| 40 | + file_put_contents($this->tmpDir . "/page/_header.html", "<html><body><header>site</header>"); |
| 41 | + file_put_contents($this->tmpDir . "/page/admin/_header.html", "<nav>admin</nav>"); |
| 42 | + file_put_contents($this->tmpDir . "/page/admin/users.html", "<main>users</main>"); |
| 43 | + file_put_contents($this->tmpDir . "/page/admin/_footer.html", "<footer>admin</footer>"); |
| 44 | + file_put_contents($this->tmpDir . "/page/_footer.html", "<footer>site</footer></body></html>"); |
| 45 | + |
| 46 | + chdir($this->tmpDir); |
| 47 | + |
| 48 | + $request = self::createMock(Request::class); |
| 49 | + $request->method("getMethod")->willReturn("GET"); |
| 50 | + $request->method("getHeaderLine") |
| 51 | + ->with("accept") |
| 52 | + ->willReturn("text/html"); |
| 53 | + $request->method("getUri")->willReturn(new Uri("https://example.test/admin/users")); |
| 54 | + |
| 55 | + $sut = new DefaultRouter(new RouterConfig(307, "text/html")); |
| 56 | + $container = new Container(); |
| 57 | + $container->set($request); |
| 58 | + $sut->setContainer($container); |
| 59 | + $sut->route($request); |
| 60 | + |
| 61 | + self::assertSame( |
| 62 | + [ |
| 63 | + "page/_header.html", |
| 64 | + "page/admin/_header.html", |
| 65 | + "page/admin/users.html", |
| 66 | + "page/admin/_footer.html", |
| 67 | + "page/_footer.html", |
| 68 | + ], |
| 69 | + iterator_to_array($sut->getViewAssembly()), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testRoute_pageRequest_withHeadersFootersAndPartials_throwsLogicException():void { |
| 74 | + class_exists(HTMLDocument::class); |
| 75 | + class_exists(ComponentExpander::class); |
| 76 | + class_exists(PartialContent::class); |
| 77 | + class_exists(PartialContentDirectoryNotFoundException::class); |
| 78 | + class_exists(PartialExpander::class); |
| 79 | + |
| 80 | + file_put_contents($this->tmpDir . "/page/_header.html", "<html><body><header>site</header>"); |
| 81 | + file_put_contents($this->tmpDir . "/page/admin/_header.html", "<nav>admin</nav>"); |
| 82 | + file_put_contents($this->tmpDir . "/page/admin/users.html", "<!-- extends=layout --><main>users</main>"); |
| 83 | + file_put_contents($this->tmpDir . "/page/admin/_footer.html", "<footer>admin</footer>"); |
| 84 | + file_put_contents($this->tmpDir . "/page/_footer.html", "<footer>site</footer></body></html>"); |
| 85 | + mkdir($this->tmpDir . "/page/_partial", recursive: true); |
| 86 | + file_put_contents( |
| 87 | + $this->tmpDir . "/page/_partial/layout.html", |
| 88 | + "<!doctype html><html><body><section data-partial></section></body></html>", |
| 89 | + ); |
| 90 | + |
| 91 | + chdir($this->tmpDir); |
| 92 | + |
| 93 | + $request = self::createMock(Request::class); |
| 94 | + $request->method("getMethod")->willReturn("GET"); |
| 95 | + $request->method("getHeaderLine") |
| 96 | + ->with("accept") |
| 97 | + ->willReturn("text/html"); |
| 98 | + $request->method("getUri")->willReturn(new Uri("https://example.test/admin/users")); |
| 99 | + |
| 100 | + $sut = new DefaultRouter(new RouterConfig(307, "text/html")); |
| 101 | + $container = new Container(); |
| 102 | + $container->set($request); |
| 103 | + $sut->setContainer($container); |
| 104 | + $sut->route($request); |
| 105 | + |
| 106 | + $view = new HTMLView(new Stream()); |
| 107 | + foreach($sut->getViewAssembly() as $viewFile) { |
| 108 | + $view->addViewFile($viewFile); |
| 109 | + } |
| 110 | + $viewModel = $view->createViewModel(); |
| 111 | + |
| 112 | + $processor = new HTMLDocumentProcessor("components", "page/_partial"); |
| 113 | + $this->expectException(HeaderFooterPartialConflictException::class); |
| 114 | + $this->expectExceptionMessage( |
| 115 | + "Header/footer view files cannot be combined with partial views." |
| 116 | + ); |
| 117 | + $processor->processPartialContent($viewModel, $sut->getViewAssembly()); |
| 118 | + } |
| 119 | + |
| 120 | + private function removeDirectory(string $dir):void { |
| 121 | + if(!is_dir($dir)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + foreach(scandir($dir) ?: [] as $file) { |
| 126 | + if($file === "." || $file === "..") { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + $path = $dir . DIRECTORY_SEPARATOR . $file; |
| 131 | + if(is_dir($path)) { |
| 132 | + $this->removeDirectory($path); |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + unlink($path); |
| 137 | + } |
| 138 | + |
| 139 | + rmdir($dir); |
| 140 | + } |
| 141 | +} |
0 commit comments