|
| 1 | +<?php |
| 2 | +namespace GT\WebEngine\Test\View; |
| 3 | + |
| 4 | +use Gt\Dom\HTMLDocument; |
| 5 | +use GT\WebEngine\View\HTMLView; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Psr\Http\Message\StreamInterface; |
| 8 | + |
| 9 | +class HTMLViewTest extends TestCase { |
| 10 | + private string $tmpDir; |
| 11 | + |
| 12 | + protected function setUp():void { |
| 13 | + parent::setUp(); |
| 14 | + $this->tmpDir = sys_get_temp_dir() . "/phpgt-webengine-test--View-HTMLView-" . uniqid(); |
| 15 | + if(!is_dir($this->tmpDir)) { |
| 16 | + mkdir($this->tmpDir, recursive: true); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + protected function tearDown():void { |
| 21 | + foreach(scandir($this->tmpDir) ?: [] as $f) { |
| 22 | + if($f === '.' || $f === '..') { continue; } |
| 23 | + @unlink($this->tmpDir . DIRECTORY_SEPARATOR . $f); |
| 24 | + } |
| 25 | + @rmdir($this->tmpDir); |
| 26 | + parent::tearDown(); |
| 27 | + } |
| 28 | + |
| 29 | + private function newRecordingStream(): StreamInterface { |
| 30 | + return new class implements StreamInterface { |
| 31 | + public string $buffer = ""; |
| 32 | + public function __toString():string { return $this->buffer; } |
| 33 | + public function close():void {} |
| 34 | + public function detach() {} |
| 35 | + public function getSize():?int { return strlen($this->buffer); } |
| 36 | + public function tell():int { return strlen($this->buffer); } |
| 37 | + public function eof():bool { return true; } |
| 38 | + public function isSeekable():bool { return false; } |
| 39 | + public function seek($offset, $whence = SEEK_SET):void {} |
| 40 | + public function rewind():void {} |
| 41 | + public function isWritable():bool { return true; } |
| 42 | + public function write($string):int { $this->buffer .= (string)$string; return strlen((string)$string); } |
| 43 | + public function isReadable():bool { return false; } |
| 44 | + public function read($length):string { return ""; } |
| 45 | + public function getContents():string { return $this->buffer; } |
| 46 | + public function getMetadata($key = null) { return null; } |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + public function testCreateViewModel_concatenatesFilesIntoHtmlDocument():void { |
| 51 | + $file1 = $this->tmpDir . "/one.html"; |
| 52 | + $file2 = $this->tmpDir . "/two.html"; |
| 53 | + file_put_contents($file1, "<div id=one>One</div>"); |
| 54 | + file_put_contents($file2, "<p id=two>Two</p>"); |
| 55 | + |
| 56 | + $sut = new HTMLView($this->newRecordingStream()); |
| 57 | + $sut->addViewFile($file1); |
| 58 | + $sut->addViewFile($file2); |
| 59 | + |
| 60 | + $doc = $sut->createViewModel(); |
| 61 | + self::assertInstanceOf(HTMLDocument::class, $doc); |
| 62 | + $docStr = (string)$doc; |
| 63 | + self::assertTrue(strpos($docStr, 'One') !== false); |
| 64 | + self::assertTrue(strpos($docStr, 'Two') !== false); |
| 65 | + } |
| 66 | + |
| 67 | + public function testStream_serialisesHtmlDocumentToOutput():void { |
| 68 | + $stream = $this->newRecordingStream(); |
| 69 | + $sut = new HTMLView($stream); |
| 70 | + $doc = new HTMLDocument("<main>Content</main>"); |
| 71 | + $sut->stream($doc); |
| 72 | + self::assertSame((string)$doc, (string)$stream); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCreateViewModel_withNoFiles_returnsEmptyDocument():void { |
| 76 | + $sut = new HTMLView($this->newRecordingStream()); |
| 77 | + $doc = $sut->createViewModel(); |
| 78 | + self::assertInstanceOf(HTMLDocument::class, $doc); |
| 79 | + self::assertIsString((string)$doc); |
| 80 | + } |
| 81 | +} |
0 commit comments