Skip to content

Commit dde0c9f

Browse files
authored
Merge pull request #5 from MaplePHP/develop
Guard rewind in toString and prevent exceptions
2 parents f9b97d9 + 6330212 commit dde0c9f

File tree

3 files changed

+139
-50
lines changed

3 files changed

+139
-50
lines changed

src/Stream.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@ public function getStream(): string
7878
*/
7979
public function __toString(): string
8080
{
81-
$this->rewind();
82-
return $this->getContents();
81+
try {
82+
if ($this->isSeekable()) {
83+
$this->rewind();
84+
}
85+
86+
return $this->getContents();
87+
} catch (\Throwable $e) {
88+
return '';
89+
}
8390
}
8491

8592
/**

tests/unitary-stream.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use MaplePHP\Http\Stream;
6+
use MaplePHP\Unitary\{Expect, TestCase};
7+
8+
9+
group('MaplePHP\Http\Stream', function (TestCase $case) {
10+
11+
// -------------------------------------------------
12+
// Simple value expectations
13+
// -------------------------------------------------
14+
15+
$case->expect((new Stream())->getStream())
16+
->isString()
17+
->isEqualTo('php://temp')
18+
->validate();
19+
20+
$case->expect(is_resource((new Stream())->getResource()))
21+
->isTrue()
22+
->validate();
23+
24+
25+
// -------------------------------------------------
26+
// write / read behaviour
27+
// -------------------------------------------------
28+
$case->expect(function (Expect $expect) {
29+
30+
$s = new Stream(Stream::TEMP, 'w+');
31+
$bytes = $s->write('Hello');
32+
33+
$expect->expect($bytes)
34+
->isInt()
35+
->isGreaterThan( 1);
36+
});
37+
38+
39+
$case->expect(function (Expect $expect) {
40+
41+
$s = new Stream(Stream::TEMP, 'w+');
42+
$s->write('HelloWorld');
43+
$s->rewind();
44+
45+
$first = $s->read(5);
46+
$rest = $s->getContents();
47+
48+
$expect->expect($first)->isEqualTo('Hello');
49+
$expect->expect($rest)->isEqualTo('World');
50+
});
51+
52+
53+
// -------------------------------------------------
54+
// __toString rewind behaviour
55+
// -------------------------------------------------
56+
57+
$case->expect(function (Expect $expect) {
58+
59+
$s = new Stream(Stream::TEMP, 'w+');
60+
$s->write('ABC');
61+
$s->read(1);
62+
63+
$expect->expect((string)$s)
64+
->isString()
65+
->isEqualTo('ABC');
66+
});
67+
68+
69+
// -------------------------------------------------
70+
// seek / tell
71+
// -------------------------------------------------
72+
73+
$case->expect(function (Expect $expect) {
74+
75+
$s = new Stream(Stream::TEMP, 'w+');
76+
$s->write('ABCDE');
77+
$s->rewind();
78+
$s->read(2);
79+
80+
$expect->expect($s->tell())
81+
->isInt()
82+
->isEqualTo(2);
83+
});
84+
85+
86+
// -------------------------------------------------
87+
// getLines
88+
// -------------------------------------------------
89+
90+
$case->expect(function (Expect $expect) {
91+
92+
$s = new Stream(Stream::TEMP, 'w+');
93+
$s->write("A\nB\nC\nD\n");
94+
95+
$expect->expect($s->getLines(2, 3))
96+
->isString()
97+
->isEqualTo("B\nC\n");
98+
});
99+
100+
101+
// -------------------------------------------------
102+
// clean
103+
// -------------------------------------------------
104+
105+
$case->expect(function (Expect $expect) {
106+
107+
$s = new Stream(Stream::TEMP, 'w+');
108+
$s->write('12345');
109+
$s->clean();
110+
111+
$expect->expect($s->getSize())
112+
->isInt()
113+
->isEqualTo(0);
114+
});
115+
116+
117+
// -------------------------------------------------
118+
// close / detach
119+
// -------------------------------------------------
120+
121+
$case->expect(function (Expect $expect) {
122+
123+
$s = new Stream(Stream::TEMP, 'w+');
124+
$s->close();
125+
126+
$expect->expect($s->getResource())
127+
->isNull();
128+
});
129+
130+
});

tests/unitaryRequest.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)