/
PhpInputStreamTest.php
85 lines (68 loc) · 2.25 KB
/
PhpInputStreamTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace LaminasTest\Diactoros;
use Laminas\Diactoros\PhpInputStream;
use PHPUnit\Framework\TestCase;
use function file_get_contents;
use function substr;
final class PhpInputStreamTest extends TestCase
{
/** @var string */
protected $file;
/** @var PhpInputStream */
protected $stream;
protected function setUp(): void
{
$this->file = __DIR__ . '/TestAsset/php-input-stream.txt';
$this->stream = new PhpInputStream($this->file);
}
public function getFileContents(): string
{
return file_get_contents($this->file);
}
public function assertStreamContents(string $test, string $message = ''): void
{
$content = $this->getFileContents();
$this->assertSame($content, $test, $message);
}
public function testStreamIsNeverWritable(): void
{
$this->assertFalse($this->stream->isWritable());
}
public function testCanReadStreamIteratively(): void
{
$body = '';
while (! $this->stream->eof()) {
$body .= $this->stream->read(128);
}
$this->assertStreamContents($body);
}
public function testGetContentsReturnsRemainingContentsOfStream(): void
{
$this->stream->read(128);
$remainder = $this->stream->getContents();
$contents = $this->getFileContents();
$this->assertSame(substr($contents, 128), $remainder);
}
public function testGetContentsReturnCacheWhenReachedEof(): void
{
$this->stream->getContents();
$this->assertStreamContents($this->stream->getContents());
$stream = new PhpInputStream('data://,0');
$stream->read(1);
$stream->read(1);
$this->assertSame('0', $stream->getContents(), 'Don\'t evaluate 0 as empty');
}
public function testCastingToStringReturnsFullContentsRegardlessOfPriorReads(): void
{
$this->stream->read(128);
$this->assertStreamContents($this->stream->__toString());
}
public function testMultipleCastsToStringReturnSameContentsEvenIfReadsOccur(): void
{
$first = (string) $this->stream;
$this->stream->read(128);
$second = (string) $this->stream;
$this->assertSame($first, $second);
}
}