|
| 1 | +<?php |
| 2 | +namespace GT\WebEngine\Test\View; |
| 3 | + |
| 4 | +use Gt\Dom\HTMLDocument; |
| 5 | +use GT\WebEngine\View\HTMLView; |
| 6 | +use GT\WebEngine\View\ViewStreamer; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Psr\Http\Message\StreamInterface; |
| 9 | + |
| 10 | +class ViewStreamerTest extends TestCase { |
| 11 | + public function testStream_delegatesToViewStreamMethod():void { |
| 12 | + $document = new HTMLDocument("<main>Streamed</main>"); |
| 13 | + $view = $this->getMockBuilder(HTMLView::class) |
| 14 | + ->setConstructorArgs([$this->newRecordingStream()]) |
| 15 | + ->onlyMethods(["stream"]) |
| 16 | + ->getMock(); |
| 17 | + $view->expects(self::once()) |
| 18 | + ->method("stream") |
| 19 | + ->with($document); |
| 20 | + |
| 21 | + $sut = new ViewStreamer(); |
| 22 | + $sut->stream($view, $document); |
| 23 | + } |
| 24 | + |
| 25 | + private function newRecordingStream():StreamInterface { |
| 26 | + return new class implements StreamInterface { |
| 27 | + public string $buffer = ""; |
| 28 | + public function __toString():string { return $this->buffer; } |
| 29 | + public function close():void {} |
| 30 | + public function detach() {} |
| 31 | + public function getSize():?int { return strlen($this->buffer); } |
| 32 | + public function tell():int { return strlen($this->buffer); } |
| 33 | + public function eof():bool { return true; } |
| 34 | + public function isSeekable():bool { return false; } |
| 35 | + public function seek($offset, $whence = SEEK_SET):void {} |
| 36 | + public function rewind():void {} |
| 37 | + public function isWritable():bool { return true; } |
| 38 | + public function write($string):int { $this->buffer .= (string)$string; return strlen((string)$string); } |
| 39 | + public function isReadable():bool { return false; } |
| 40 | + public function read($length):string { return ""; } |
| 41 | + public function getContents():string { return $this->buffer; } |
| 42 | + public function getMetadata($key = null) { return null; } |
| 43 | + }; |
| 44 | + } |
| 45 | +} |
0 commit comments