Commit dd7058c9 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #219

parents 7a6bb13d a36b0799
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace MongoDB\GridFS; namespace MongoDB\GridFS;
use Exception;
/** /**
* Stream wrapper for reading and writing a GridFS file. * Stream wrapper for reading and writing a GridFS file.
* *
...@@ -57,6 +59,10 @@ class StreamWrapper ...@@ -57,6 +59,10 @@ class StreamWrapper
*/ */
public function stream_eof() public function stream_eof()
{ {
if ( ! $this->stream instanceof ReadableStream) {
return false;
}
return $this->stream->isEOF(); return $this->stream->isEOF();
} }
...@@ -97,8 +103,16 @@ class StreamWrapper ...@@ -97,8 +103,16 @@ class StreamWrapper
*/ */
public function stream_read($count) public function stream_read($count)
{ {
// TODO: Ensure that $this->stream is a ReadableStream if ( ! $this->stream instanceof ReadableStream) {
return '';
}
try {
return $this->stream->downloadNumBytes($count); return $this->stream->downloadNumBytes($count);
} catch (Exception $e) {
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
return false;
}
} }
/** /**
...@@ -122,14 +136,20 @@ class StreamWrapper ...@@ -122,14 +136,20 @@ class StreamWrapper
* *
* @see http://php.net/manual/en/streamwrapper.stream-write.php * @see http://php.net/manual/en/streamwrapper.stream-write.php
* @param string $data Data to write * @param string $data Data to write
* @return integer The number of bytes successfully stored * @return integer The number of bytes written
*/ */
public function stream_write($data) public function stream_write($data)
{ {
// TODO: Ensure that $this->stream is a WritableStream if ( ! $this->stream instanceof WritableStream) {
$this->stream->insertChunks($data); return 0;
}
return strlen($data); try {
return $this->stream->insertChunks($data);
} catch (Exception $e) {
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
return false;
}
} }
/** /**
......
...@@ -184,11 +184,6 @@ class WritableStream ...@@ -184,11 +184,6 @@ class WritableStream
return $readBytes; return $readBytes;
} }
public function isEOF()
{
return $this->isClosed;
}
private function abort() private function abort()
{ {
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']); $this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);
......
...@@ -125,7 +125,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -125,7 +125,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} }
/** /**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException * @expectedException PHPUnit_Framework_Error_Warning
*/ */
public function testDownloadingFileWithMissingChunk() public function testDownloadingFileWithMissingChunk()
{ {
...@@ -137,7 +137,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -137,7 +137,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} }
/** /**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException * @expectedException PHPUnit_Framework_Error_Warning
*/ */
public function testDownloadingFileWithUnexpectedChunkIndex() public function testDownloadingFileWithUnexpectedChunkIndex()
{ {
...@@ -152,7 +152,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -152,7 +152,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} }
/** /**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException * @expectedException PHPUnit_Framework_Error_Warning
*/ */
public function testDownloadingFileWithUnexpectedChunkSize() public function testDownloadingFileWithUnexpectedChunkSize()
{ {
......
...@@ -6,9 +6,9 @@ use MongoDB\Collection; ...@@ -6,9 +6,9 @@ use MongoDB\Collection;
use MongoDB\BSON\Binary; use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId; use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime; use MongoDB\BSON\UTCDateTime;
use MongoDB\Exception\RuntimeException;
use MongoDB\Operation\BulkWrite; use MongoDB\Operation\BulkWrite;
use DateTime; use DateTime;
use Exception;
use IteratorIterator; use IteratorIterator;
use LogicException; use LogicException;
use MultipleIterator; use MultipleIterator;
...@@ -50,7 +50,7 @@ class SpecFunctionalTest extends FunctionalTestCase ...@@ -50,7 +50,7 @@ class SpecFunctionalTest extends FunctionalTestCase
try { try {
$result = $this->executeAct($test['act']); $result = $this->executeAct($test['act']);
} catch (RuntimeException $e) { } catch (Exception $e) {
$result = $e; $result = $e;
} }
...@@ -333,7 +333,10 @@ class SpecFunctionalTest extends FunctionalTestCase ...@@ -333,7 +333,10 @@ class SpecFunctionalTest extends FunctionalTestCase
case 'ChunkIsMissing': case 'ChunkIsMissing':
case 'ChunkIsWrongSize': case 'ChunkIsWrongSize':
return 'MongoDB\GridFS\Exception\CorruptFileException'; /* Although ReadableStream throws a CorruptFileException, the
* stream wrapper will convert it to a PHP error of type
* E_USER_WARNING. */
return 'PHPUnit_Framework_Error_Warning';
default: default:
throw new LogicException('Unsupported error: ' . $error); throw new LogicException('Unsupported error: ' . $error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment