Skip to content

Commit 68d2990

Browse files
committed
feature: non-html views
1 parent 26d8475 commit 68d2990

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

test/phpunit/View/NullViewTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace GT\WebEngine\Test\View;
3+
4+
use GT\WebEngine\View\NullView;
5+
use PHPUnit\Framework\TestCase;
6+
use Psr\Http\Message\StreamInterface;
7+
8+
class NullViewTest extends TestCase {
9+
public function testCreateViewModel_returnsEmptyStringPlaceholder():void {
10+
$sut = new NullView($this->newRecordingStream());
11+
12+
self::assertSame("", $sut->createViewModel());
13+
}
14+
15+
private function newRecordingStream():StreamInterface {
16+
return new class implements StreamInterface {
17+
public string $buffer = "";
18+
public function __toString():string { return $this->buffer; }
19+
public function close():void {}
20+
public function detach() {}
21+
public function getSize():?int { return strlen($this->buffer); }
22+
public function tell():int { return strlen($this->buffer); }
23+
public function eof():bool { return true; }
24+
public function isSeekable():bool { return false; }
25+
public function seek($offset, $whence = SEEK_SET):void {}
26+
public function rewind():void {}
27+
public function isWritable():bool { return true; }
28+
public function write($string):int { $this->buffer .= (string)$string; return strlen((string)$string); }
29+
public function isReadable():bool { return false; }
30+
public function read($length):string { return ""; }
31+
public function getContents():string { return $this->buffer; }
32+
public function getMetadata($key = null) { return null; }
33+
};
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)