Skip to content

Commit dd7058c

Browse files
committed
Merge pull request #219
2 parents 7a6bb13 + a36b079 commit dd7058c

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\GridFS;
44

5+
use Exception;
6+
57
/**
68
* Stream wrapper for reading and writing a GridFS file.
79
*
@@ -57,6 +59,10 @@ public function stream_close()
5759
*/
5860
public function stream_eof()
5961
{
62+
if ( ! $this->stream instanceof ReadableStream) {
63+
return false;
64+
}
65+
6066
return $this->stream->isEOF();
6167
}
6268

@@ -93,12 +99,20 @@ public function stream_open($path, $mode, $options, &$openedPath)
9399
*
94100
* @see http://php.net/manual/en/streamwrapper.stream-read.php
95101
* @param integer $count Number of bytes to read
96-
* @return string
102+
* @return string
97103
*/
98104
public function stream_read($count)
99105
{
100-
// TODO: Ensure that $this->stream is a ReadableStream
101-
return $this->stream->downloadNumBytes($count);
106+
if ( ! $this->stream instanceof ReadableStream) {
107+
return '';
108+
}
109+
110+
try {
111+
return $this->stream->downloadNumBytes($count);
112+
} catch (Exception $e) {
113+
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
114+
return false;
115+
}
102116
}
103117

104118
/**
@@ -122,14 +136,20 @@ public function stream_stat()
122136
*
123137
* @see http://php.net/manual/en/streamwrapper.stream-write.php
124138
* @param string $data Data to write
125-
* @return integer The number of bytes successfully stored
139+
* @return integer The number of bytes written
126140
*/
127141
public function stream_write($data)
128142
{
129-
// TODO: Ensure that $this->stream is a WritableStream
130-
$this->stream->insertChunks($data);
143+
if ( ! $this->stream instanceof WritableStream) {
144+
return 0;
145+
}
131146

132-
return strlen($data);
147+
try {
148+
return $this->stream->insertChunks($data);
149+
} catch (Exception $e) {
150+
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
151+
return false;
152+
}
133153
}
134154

135155
/**

src/GridFS/WritableStream.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ public function insertChunks($toWrite)
184184
return $readBytes;
185185
}
186186

187-
public function isEOF()
188-
{
189-
return $this->isClosed;
190-
}
191-
192187
private function abort()
193188
{
194189
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
125125
}
126126

127127
/**
128-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
128+
* @expectedException PHPUnit_Framework_Error_Warning
129129
*/
130130
public function testDownloadingFileWithMissingChunk()
131131
{
@@ -137,7 +137,7 @@ public function testDownloadingFileWithMissingChunk()
137137
}
138138

139139
/**
140-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
140+
* @expectedException PHPUnit_Framework_Error_Warning
141141
*/
142142
public function testDownloadingFileWithUnexpectedChunkIndex()
143143
{
@@ -152,7 +152,7 @@ public function testDownloadingFileWithUnexpectedChunkIndex()
152152
}
153153

154154
/**
155-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
155+
* @expectedException PHPUnit_Framework_Error_Warning
156156
*/
157157
public function testDownloadingFileWithUnexpectedChunkSize()
158158
{

tests/GridFS/SpecFunctionalTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use MongoDB\BSON\Binary;
77
use MongoDB\BSON\ObjectId;
88
use MongoDB\BSON\UTCDateTime;
9-
use MongoDB\Exception\RuntimeException;
109
use MongoDB\Operation\BulkWrite;
1110
use DateTime;
11+
use Exception;
1212
use IteratorIterator;
1313
use LogicException;
1414
use MultipleIterator;
@@ -50,7 +50,7 @@ public function testSpecification(array $initialData, array $test)
5050

5151
try {
5252
$result = $this->executeAct($test['act']);
53-
} catch (RuntimeException $e) {
53+
} catch (Exception $e) {
5454
$result = $e;
5555
}
5656

@@ -333,7 +333,10 @@ private function getExceptionClassForError($error)
333333

334334
case 'ChunkIsMissing':
335335
case 'ChunkIsWrongSize':
336-
return 'MongoDB\GridFS\Exception\CorruptFileException';
336+
/* Although ReadableStream throws a CorruptFileException, the
337+
* stream wrapper will convert it to a PHP error of type
338+
* E_USER_WARNING. */
339+
return 'PHPUnit_Framework_Error_Warning';
337340

338341
default:
339342
throw new LogicException('Unsupported error: ' . $error);

0 commit comments

Comments
 (0)