Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit e81e871

Browse files
committed
Merge branch 'hotfix/247'
Close #247
2 parents fbcf611 + c9b2604 commit e81e871

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cache:
88
- $HOME/.local
99
- zf-mkdoc-theme
1010

11+
dist: trusty
12+
1113
env:
1214
global:
1315
- COMPOSER_ARGS="--no-interaction"

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#247](https://github.com/zendframework/zend-diactoros/pull/247) fixes the
22+
`Stream` and `RelativeStream` `__toString()` method implementations to check
23+
if the stream `isSeekable()` before attempting to `rewind()` it, ensuring that
24+
the method does not raise exceptions (PHP does not allow exceptions in that
25+
method). In particular, this fixes an issue when using AWS S3 streams.
2226

2327
## 1.4.0 - 2017-04-06
2428

src/RelativeStream.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function __construct(StreamInterface $decoratedStream, $offset)
4848
*/
4949
public function __toString()
5050
{
51-
$this->seek(0);
51+
if ($this->isSeekable()) {
52+
$this->seek(0);
53+
}
5254
return $this->getContents();
5355
}
5456

src/Stream.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public function __toString()
4848
}
4949

5050
try {
51-
$this->rewind();
51+
if ($this->isSeekable()) {
52+
$this->rewind();
53+
}
54+
5255
return $this->getContents();
5356
} catch (RuntimeException $e) {
5457
return '';

test/RelativeStreamTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace ZendTest\Diactoros;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use Prophecy\Argument;
1314
use Zend\Diactoros\RelativeStream;
1415
use Zend\Diactoros\Stream;
1516

@@ -21,6 +22,7 @@ class RelativeStreamTest extends TestCase
2122
public function testToString()
2223
{
2324
$decorated = $this->prophesize('Zend\Diactoros\Stream');
25+
$decorated->isSeekable()->willReturn(true);
2426
$decorated->tell()->willReturn(100);
2527
$decorated->seek(100, SEEK_SET)->shouldBeCalled();
2628
$decorated->getContents()->shouldBeCalled()->willReturn('foobarbaz');
@@ -178,4 +180,16 @@ public function testGetContentsRaisesExceptionWhenPointerIsBehindOffset()
178180
$stream = new RelativeStream($decorated->reveal(), 100);
179181
$stream->getContents();
180182
}
183+
184+
public function testCanReadContentFromNotSeekableResource()
185+
{
186+
$decorated = $this->prophesize('Zend\Diactoros\Stream');
187+
$decorated->isSeekable()->willReturn(false);
188+
$decorated->seek(Argument::any())->shouldNotBeCalled();
189+
$decorated->tell()->willReturn(3);
190+
$decorated->getContents()->willReturn('CONTENTS');
191+
192+
$stream = new RelativeStream($decorated->reveal(), 3);
193+
$this->assertEquals('CONTENTS', $stream->__toString());
194+
}
181195
}

test/StreamTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,4 +590,21 @@ public function getResourceFor67()
590590

591591
return false;
592592
}
593+
594+
public function testCanReadContentFromNotSeekableResource()
595+
{
596+
$this->tmpnam = tempnam(sys_get_temp_dir(), 'diac');
597+
file_put_contents($this->tmpnam, 'FOO BAR');
598+
$resource = fopen($this->tmpnam, 'r');
599+
$stream = $this
600+
->getMockBuilder('Zend\Diactoros\Stream')
601+
->setConstructorArgs([$resource])
602+
->setMethods(['isSeekable'])
603+
->getMock();
604+
605+
$stream->expects($this->any())->method('isSeekable')
606+
->will($this->returnValue(false));
607+
608+
$this->assertEquals('FOO BAR', $stream->__toString());
609+
}
593610
}

0 commit comments

Comments
 (0)