forked from xp-framework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpooledInputStreamTest.class.php
More file actions
executable file
·183 lines (147 loc) · 5.71 KB
/
SpooledInputStreamTest.class.php
File metadata and controls
executable file
·183 lines (147 loc) · 5.71 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php namespace io\unittest;
use io\streams\{SpooledInputStream, MemoryInputStream, Streams};
use io\{TempFile, Folder, Path, IOException};
use lang\Environment;
use test\{Assert, Expect, Test, Values};
class SpooledInputStreamTest {
const BYTES= 'Test success';
/** @param ?string|io.Path|io.Folder|io.File $temp */
private function newFixture($temp= null): SpooledInputStream {
return new SpooledInputStream(new MemoryInputStream(self::BYTES), $temp);
}
#[Test]
public function can_create() {
$this->newFixture();
}
#[Test]
public function reading() {
$stream= $this->newFixture();
Assert::equals(0, $stream->tell());
Assert::equals(strlen(self::BYTES), $stream->available());
Assert::equals(self::BYTES, Streams::readAll($stream));
}
#[Test]
public function reading_after_seeking() {
$stream= $this->newFixture();
$stream->seek(5);
$stream->seek(0);
Assert::equals(0, $stream->tell());
Assert::equals(strlen(self::BYTES), $stream->available());
Assert::equals(self::BYTES, Streams::readAll($stream));
}
#[Test, Values([0, 1, 5])]
public function seeking_forward($offset) {
$stream= $this->newFixture();
$stream->seek($offset, SEEK_SET);
Assert::equals($offset, $stream->tell());
Assert::equals(strlen(self::BYTES) - $offset, $stream->available());
Assert::equals(substr(self::BYTES, $offset), Streams::readAll($stream));
}
#[Test, Values([0, -1, -5])]
public function seeking_to_end($offset) {
$stream= $this->newFixture();
$stream->seek($offset, SEEK_END);
Assert::equals(strlen(self::BYTES) + $offset, $stream->tell());
Assert::equals(-$offset, $stream->available());
Assert::equals(substr(self::BYTES, strlen(self::BYTES) + $offset), Streams::readAll($stream));
}
#[Test, Values([0, 1, -1])]
public function seeking_relative($offset) {
$stream= $this->newFixture();
$stream->seek(5, SEEK_SET);
$stream->seek($offset, SEEK_CUR);
Assert::equals(5 + $offset, $stream->tell());
Assert::equals(strlen(self::BYTES) - 5 - $offset, $stream->available());
Assert::equals(substr(self::BYTES, 5 + $offset), Streams::readAll($stream));
}
#[Test, Values([0, 1, 5])]
public function seeking_forward_set($offset) {
$stream= $this->newFixture();
$stream->seek($offset, SEEK_SET);
Assert::equals($offset, $stream->tell());
Assert::equals(strlen(self::BYTES) - $offset, $stream->available());
Assert::equals(substr(self::BYTES, $offset), Streams::readAll($stream));
}
#[Test, Values([0, 1, 5])]
public function seeking_forward_cur($offset) {
$stream= $this->newFixture();
$stream->seek(strlen(self::BYTES), SEEK_SET);
$stream->seek(-$offset, SEEK_CUR);
Assert::equals(strlen(self::BYTES) - $offset, $stream->tell());
Assert::equals($offset, $stream->available());
Assert::equals(substr(self::BYTES, strlen(self::BYTES) - $offset), Streams::readAll($stream));
}
#[Test, Values([0, 1, 5])]
public function seeking_forward_triggers_read($offset) {
$stream= $this->newFixture();
$stream->seek(5, SEEK_SET);
$stream->seek(strlen(self::BYTES) - $offset, SEEK_SET);
Assert::equals(strlen(self::BYTES) - $offset, $stream->tell());
Assert::equals($offset, $stream->available());
Assert::equals(substr(self::BYTES, strlen(self::BYTES) - $offset), Streams::readAll($stream));
}
#[Test, Values([0, 1, 5])]
public function seeking_back_to_offset_after_reading_until_end($offset) {
$stream= $this->newFixture();
while ($stream->available()) {
$stream->read();
}
$stream->seek($offset, SEEK_SET);
Assert::equals($offset, $stream->tell());
Assert::equals(strlen(self::BYTES) - $offset, $stream->available());
Assert::equals(substr(self::BYTES, $offset), Streams::readAll($stream));
}
#[Test, Values([0, -1, -5])]
public function seeking_to_end_after_reading_until_end($offset) {
$stream= $this->newFixture();
while ($stream->available()) {
$stream->read();
}
$stream->seek($offset, SEEK_END);
Assert::equals(strlen(self::BYTES) + $offset, $stream->tell());
Assert::equals(-$offset, $stream->available());
Assert::equals(substr(self::BYTES, strlen(self::BYTES) + $offset), Streams::readAll($stream));
}
#[Test, Values([0, -1, -5])]
public function seeking_to_end_after_seeking_relative($offset) {
$stream= $this->newFixture();
$stream->seek(5, SEEK_SET);
$stream->seek($offset, SEEK_END);
Assert::equals(strlen(self::BYTES) + $offset, $stream->tell());
Assert::equals(-$offset, $stream->available());
Assert::equals(substr(self::BYTES, strlen(self::BYTES) + $offset), Streams::readAll($stream));
}
#[Test]
public function close_can_be_called_twice() {
$stream= $this->newFixture();
$stream->close();
$stream->close();
}
#[Test, Expect(IOException::class), Values([SEEK_SET, SEEK_CUR])]
public function cannot_seek_before_beginning_of_file($whence) {
$this->newFixture()->seek(-1, $whence);
}
#[Test, Expect(IOException::class)]
public function cannot_seek_with_invalid_whence() {
$this->newFixture()->seek(0, 6100);
}
#[Test]
public function position_after_seek_error() {
$stream= $this->newFixture();
$stream->read(4);
Assert::throws(IOException::class, fn() => $stream->seek(-1));
Assert::equals(4, $stream->tell());
}
#[Test]
public function position_after_seek_set_past_end() {
$stream= $this->newFixture();
$stream->seek(strlen(self::BYTES) + 1, SEEK_SET);
Assert::equals(strlen(self::BYTES), $stream->tell());
}
#[Test]
public function position_after_seek_end_past_end() {
$stream= $this->newFixture();
$stream->seek(1, SEEK_END);
Assert::equals(strlen(self::BYTES), $stream->tell());
}
}