File tree Expand file tree Collapse file tree 3 files changed +46
-28
lines changed Expand file tree Collapse file tree 3 files changed +46
-28
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace spec \Http \Message \StreamFactory ;
4
4
5
+ use GuzzleHttp \Psr7 \Stream ;
6
+ use Psr \Http \Message \StreamInterface ;
7
+
5
8
trait StreamFactoryBehavior
6
9
{
7
10
function it_is_a_stream_factory ()
@@ -32,4 +35,33 @@ function it_creates_a_stream_from_non_seekable_resource()
32
35
$ this ->createStream ($ resource )
33
36
->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
34
37
}
38
+
39
+ function it_does_not_rewind_existing_stream ()
40
+ {
41
+ $ stream = new Stream (fopen ('php://memory ' , 'rw ' ));
42
+ $ stream ->write ('abcdef ' );
43
+ $ stream ->seek (3 );
44
+
45
+ $ this ->createStream ($ stream )
46
+ ->shouldHaveContent ('def ' );
47
+ }
48
+
49
+ function it_does_not_rewind_existing_resource ()
50
+ {
51
+ $ resource = fopen ('php://memory ' , 'rw ' );
52
+ fwrite ($ resource , 'abcdef ' );
53
+ fseek ($ resource , 3 );
54
+
55
+ $ this ->createStream ($ resource )
56
+ ->shouldHaveContent ('def ' );
57
+ }
58
+
59
+ public function getMatchers ()
60
+ {
61
+ return [
62
+ 'haveContent ' => function (StreamInterface $ subject , $ key ) {
63
+ return $ subject ->getContents () === $ key ;
64
+ },
65
+ ];
66
+ }
35
67
}
Original file line number Diff line number Diff line change @@ -18,26 +18,19 @@ final class DiactorosStreamFactory implements StreamFactory
18
18
*/
19
19
public function createStream ($ body = null )
20
20
{
21
- if (!$ body instanceof StreamInterface) {
22
- if (is_resource ($ body )) {
23
- $ body = new Stream ($ body );
24
- } else {
25
- $ stream = new Stream ('php://memory ' , 'rw ' );
26
-
27
- if (null === $ body || '' === $ body ) {
28
- return $ stream ;
29
- }
30
-
31
- $ stream ->write ((string ) $ body );
21
+ if ($ body instanceof StreamInterface) {
22
+ return $ body ;
23
+ }
32
24
33
- $ body = $ stream ;
34
- }
25
+ if ( is_resource ( $ body)) {
26
+ return new Stream ( $ body );
35
27
}
36
28
37
- if ($ body ->isSeekable ()) {
38
- $ body ->rewind ();
29
+ $ stream = new Stream ('php://memory ' , 'rw ' );
30
+ if (null !== $ body && '' !== $ body ) {
31
+ $ stream ->write ((string ) $ body );
39
32
}
40
33
41
- return $ body ;
34
+ return $ stream ;
42
35
}
43
36
}
Original file line number Diff line number Diff line change @@ -23,20 +23,13 @@ public function createStream($body = null)
23
23
}
24
24
25
25
if (is_resource ($ body )) {
26
- $ stream = new Stream ($ body );
27
- } else {
28
- $ resource = fopen ('php://memory ' , 'r+ ' );
29
- $ stream = new Stream ($ resource );
30
-
31
- if (null === $ body || '' === $ body ) {
32
- return $ stream ;
33
- }
34
-
35
- $ stream ->write ((string ) $ body );
26
+ return new Stream ($ body );
36
27
}
37
28
38
- if ($ stream ->isSeekable ()) {
39
- $ stream ->rewind ();
29
+ $ resource = fopen ('php://memory ' , 'r+ ' );
30
+ $ stream = new Stream ($ resource );
31
+ if (null !== $ body && '' !== $ body ) {
32
+ $ stream ->write ((string ) $ body );
40
33
}
41
34
42
35
return $ stream ;
You can’t perform that action at this time.
0 commit comments