-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStreamTransferTest.class.php
More file actions
executable file
·153 lines (123 loc) · 4.4 KB
/
StreamTransferTest.class.php
File metadata and controls
executable file
·153 lines (123 loc) · 4.4 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
<?php namespace io\unittest;
use io\IOException;
use io\streams\{InputStream, MemoryInputStream, MemoryOutputStream, OutputStream, StreamTransfer};
use test\{Assert, Test, Values};
class StreamTransferTest {
/** Returns an uncloseable input stream */
protected function uncloseableInputStream() {
return new class() implements InputStream {
public function read($length= 8192) { }
public function available() { }
public function close() { throw new \io\IOException("Close error"); }
};
}
/** Returns a closeable input stream */
protected function closeableInputStream() {
return new class() implements InputStream {
public $closed= false;
public function read($length= 8192) { }
public function available() { }
public function close() { $this->closed= true; }
};
}
/** Returns an uncloseable output stream */
protected function uncloseableOutputStream() {
return new class() implements OutputStream {
public function write($data) { }
public function flush() { }
public function close() { throw new IOException('Close error'); }
};
}
/** Returns a closeable output stream */
protected function closeableOutputStream() {
return new class() implements OutputStream {
public $closed= false;
public function write($data) { }
public function flush() { }
public function close() { $this->closed= true; }
};
}
#[Test]
public function transfer_all() {
$out= new MemoryOutputStream();
$s= new StreamTransfer(new MemoryInputStream('Hello'), $out);
$size= $s->transferAll();
Assert::equals('Hello', $out->bytes());
Assert::equals(5, $size);
}
#[Test]
public function transmit() {
$out= new MemoryOutputStream();
$s= new StreamTransfer(new MemoryInputStream('Hello'), $out);
foreach ($s->transmit() as $yield) { }
Assert::equals('Hello', $out->bytes());
}
#[Test, Values([[0, []], [1, [1]], [1024, [1024]], [1025, [1024, 1]], [2077, [1024, 1024, 29]]])]
public function transmit_chunks($length, $chunks) {
$out= new MemoryOutputStream();
$data= str_repeat('*', $length);
$s= new StreamTransfer(new MemoryInputStream($data), $out);
$transmitted= [];
foreach ($s->transmit(1024) as $yield) {
$transmitted[]= $yield;
}
Assert::equals($data, $out->bytes());
Assert::equals($chunks, $transmitted);
}
#[Test]
public function nothing_available_after_transfer() {
$in= new MemoryInputStream('Hello');
$s= new StreamTransfer($in, new MemoryOutputStream());
$s->transferAll();
Assert::equals(0, $in->available());
}
#[Test]
public function closing_twice() {
$s= new StreamTransfer(new MemoryInputStream('Hello'), new MemoryOutputStream());
$s->close();
$s->close();
}
#[Test]
public function close() {
$in= $this->closeableInputStream();
$out= $this->closeableOutputStream();
(new StreamTransfer($in, $out))->close();
Assert::true($in->closed, 'input closed');
Assert::true($out->closed, 'output closed');
}
#[Test]
public function closing_output_fails() {
$in= $this->closeableInputStream();
$out= $this->uncloseableOutputStream();
try {
(new StreamTransfer($in, $out))->close();
$this->fail('Expected exception not caught', null, 'io.IOException');
} catch (IOException $expected) {
Assert::equals('Could not close output stream: Close error', $expected->getMessage());
}
Assert::true($in->closed, 'input closed');
}
#[Test]
public function closing_input_fails() {
$in= $this->uncloseableInputStream();
$out= $this->closeableOutputStream();
try {
(new StreamTransfer($in, $out))->close();
$this->fail('Expected exception not caught', null, 'io.IOException');
} catch (IOException $expected) {
Assert::equals('Could not close input stream: Close error', $expected->getMessage());
}
Assert::true($out->closed, 'output closed');
}
#[Test]
public function closing_input_and_output_fails() {
$in= $this->uncloseableInputStream();
$out= $this->uncloseableOutputStream();
try {
(new StreamTransfer($in, $out))->close();
$this->fail('Expected exception not caught', null, 'io.IOException');
} catch (IOException $expected) {
Assert::equals('Could not close input stream: Close error, Could not close output stream: Close error', $expected->getMessage());
}
}
}