1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Tests\Http\Stream; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Spiral\Http\Stream\GeneratorStream; |
11
|
|
|
|
12
|
|
|
final class GeneratorStreamTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
protected const DEFAULT_SEQUENCE = [0, 'foo', 1, 'bar', 42, 'baz', '', "\n", 'end']; |
15
|
|
|
protected const DEFAULT_CONTENT_RESULT = "0foo1bar42baz\nend"; |
16
|
|
|
|
17
|
|
|
public function testGetSize(): void |
18
|
|
|
{ |
19
|
|
|
$stream = $this->createStream(); |
20
|
|
|
|
21
|
|
|
$this->assertNull($stream->getSize()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testIsSeekable(): void |
25
|
|
|
{ |
26
|
|
|
$stream = $this->createStream(); |
27
|
|
|
|
28
|
|
|
$this->assertFalse($stream->isSeekable()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testSeek(): void |
32
|
|
|
{ |
33
|
|
|
$stream = $this->createStream(); |
34
|
|
|
|
35
|
|
|
$this->expectException(RuntimeException::class); |
36
|
|
|
|
37
|
|
|
$stream->seek(5); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testRewindOnInit(): void |
41
|
|
|
{ |
42
|
|
|
$stream = $this->createStream(); |
43
|
|
|
|
44
|
|
|
$stream->rewind(); |
45
|
|
|
|
46
|
|
|
$this->assertSame(0, $stream->tell()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testRewindAfterRead(): void |
50
|
|
|
{ |
51
|
|
|
if (PHP_VERSION_ID < 80000) { |
52
|
|
|
$this->markTestSkipped('See issue https://bugs.php.net/bug.php?id=79927.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$stream = $this->createStream(); |
56
|
|
|
$stream->read(1); |
57
|
|
|
$stream->read(1); |
58
|
|
|
|
59
|
|
|
$this->expectException(\Exception::class); |
60
|
|
|
|
61
|
|
|
$stream->rewind(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testIsWritable(): void |
65
|
|
|
{ |
66
|
|
|
$stream = $this->createStream(); |
67
|
|
|
|
68
|
|
|
$this->assertFalse($stream->isWritable()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testWrite(): void |
72
|
|
|
{ |
73
|
|
|
$stream = $this->createStream(); |
74
|
|
|
|
75
|
|
|
$this->expectException(RuntimeException::class); |
76
|
|
|
|
77
|
|
|
$stream->write('test'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testRead(): void |
81
|
|
|
{ |
82
|
|
|
$stream = $this->createStream(); |
83
|
|
|
|
84
|
|
|
$result1 = $stream->read(4); |
85
|
|
|
$result2 = $stream->read(4); |
86
|
|
|
|
87
|
|
|
$this->assertSame('0', $result1); |
88
|
|
|
$this->assertSame('foo', $result2); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testReadWithReturnOnly(): void |
92
|
|
|
{ |
93
|
|
|
$rValue = 'return-value'; |
94
|
|
|
$stream = $this->createStream([], $rValue); |
95
|
|
|
|
96
|
|
|
$result = $stream->read(12); |
97
|
|
|
|
98
|
|
|
$this->assertSame($rValue, $result); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testToStringWithReturn(): void |
102
|
|
|
{ |
103
|
|
|
$rValue = 'return-value'; |
104
|
|
|
$stream = $this->createStream(self::DEFAULT_SEQUENCE, $rValue); |
105
|
|
|
|
106
|
|
|
$result = (string) $stream; |
107
|
|
|
|
108
|
|
|
$this->assertSame(self::DEFAULT_CONTENT_RESULT . $rValue, $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testToStringWithReturnOnly(): void |
112
|
|
|
{ |
113
|
|
|
$rValue = 'return-value'; |
114
|
|
|
$stream = $this->createStream([], $rValue); |
115
|
|
|
|
116
|
|
|
$result = (string) $stream; |
117
|
|
|
|
118
|
|
|
$this->assertSame($rValue, $result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testUnableReadStream(): void |
122
|
|
|
{ |
123
|
|
|
$stream = $this->createStream(); |
124
|
|
|
$stream->close(); |
125
|
|
|
|
126
|
|
|
$this->assertSame('', (string) $stream); |
127
|
|
|
|
128
|
|
|
$this->expectException(RuntimeException::class); |
129
|
|
|
$this->expectErrorMessage('Unable to read stream contents.'); |
130
|
|
|
|
131
|
|
|
$stream->getContents(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testClose(): void |
135
|
|
|
{ |
136
|
|
|
$stream = $this->createStream(); |
137
|
|
|
$stream->close(); |
138
|
|
|
|
139
|
|
|
$this->expectException(RuntimeException::class); |
140
|
|
|
$this->expectErrorMessage('Cannot read from non-readable stream.'); |
141
|
|
|
|
142
|
|
|
$stream->read(1); |
143
|
|
|
|
144
|
|
|
$this->assertFalse($stream->isReadable()); |
145
|
|
|
$this->assertNull($stream->getSize()); |
146
|
|
|
$this->assertSame(0, $stream->tell()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testEof(): void |
150
|
|
|
{ |
151
|
|
|
$stream = $this->createStream(); |
152
|
|
|
|
153
|
|
|
$this->assertFalse($stream->eof()); |
154
|
|
|
|
155
|
|
|
$stream->close(); |
156
|
|
|
|
157
|
|
|
$this->assertTrue($stream->eof()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testIsReadable(): void |
161
|
|
|
{ |
162
|
|
|
$stream = $this->createStream(); |
163
|
|
|
|
164
|
|
|
$this->assertTrue($stream->isReadable()); |
165
|
|
|
|
166
|
|
|
$stream->close(); |
167
|
|
|
|
168
|
|
|
$this->assertFalse($stream->isReadable()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetMetadata(): void |
172
|
|
|
{ |
173
|
|
|
$stream = $this->createStream(); |
174
|
|
|
|
175
|
|
|
$this->assertSame(['seekable' => false, 'eof' => false], $stream->getMetadata()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function createStream(iterable $sequence = self::DEFAULT_SEQUENCE, $return = null): GeneratorStream |
179
|
|
|
{ |
180
|
|
|
$function = static function (iterable $iterable, $return): Generator { |
181
|
|
|
yield from $iterable; |
182
|
|
|
return $return; |
183
|
|
|
}; |
184
|
|
|
return new GeneratorStream($function($sequence, $return)); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|