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 22
33namespace spec \Http \Message \StreamFactory ;
44
5+ use GuzzleHttp \Psr7 \Stream ;
6+ use Psr \Http \Message \StreamInterface ;
7+
58trait StreamFactoryBehavior
69{
710 function it_is_a_stream_factory ()
@@ -32,4 +35,33 @@ function it_creates_a_stream_from_non_seekable_resource()
3235 $ this ->createStream ($ resource )
3336 ->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
3437 }
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+ }
3567}
Original file line number Diff line number Diff line change @@ -18,26 +18,19 @@ final class DiactorosStreamFactory implements StreamFactory
1818 */
1919 public function createStream ($ body = null )
2020 {
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+ }
3224
33- $ body = $ stream ;
34- }
25+ if ( is_resource ( $ body)) {
26+ return new Stream ( $ body );
3527 }
3628
37- if ($ body ->isSeekable ()) {
38- $ body ->rewind ();
29+ $ stream = new Stream ('php://memory ' , 'rw ' );
30+ if (null !== $ body && '' !== $ body ) {
31+ $ stream ->write ((string ) $ body );
3932 }
4033
41- return $ body ;
34+ return $ stream ;
4235 }
4336}
Original file line number Diff line number Diff line change @@ -23,20 +23,13 @@ public function createStream($body = null)
2323 }
2424
2525 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 );
3627 }
3728
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 );
4033 }
4134
4235 return $ stream ;
You can’t perform that action at this time.
0 commit comments