|
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
|
|
|
private function createStream(iterable $sequence = self::DEFAULT_SEQUENCE, $return = null): GeneratorStream |
|
122
|
|
|
{ |
|
123
|
|
|
$function = static function (iterable $iterable, $return): Generator { |
|
124
|
|
|
yield from $iterable; |
|
125
|
|
|
return $return; |
|
126
|
|
|
}; |
|
127
|
|
|
return new GeneratorStream($function($sequence, $return)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|