Commit dd7058c9 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #219

parents 7a6bb13d a36b0799
......@@ -2,6 +2,8 @@
namespace MongoDB\GridFS;
use Exception;
/**
* Stream wrapper for reading and writing a GridFS file.
*
......@@ -57,6 +59,10 @@ class StreamWrapper
*/
public function stream_eof()
{
if ( ! $this->stream instanceof ReadableStream) {
return false;
}
return $this->stream->isEOF();
}
......@@ -97,8 +103,16 @@ class StreamWrapper
*/
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);
} catch (Exception $e) {
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
return false;
}
}
/**
......@@ -122,14 +136,20 @@ class StreamWrapper
*
* @see http://php.net/manual/en/streamwrapper.stream-write.php
* @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)
{
// TODO: Ensure that $this->stream is a WritableStream
$this->stream->insertChunks($data);
if ( ! $this->stream instanceof WritableStream) {
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
return $readBytes;
}
public function isEOF()
{
return $this->isClosed;
}
private function abort()
{
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);
......
......@@ -125,7 +125,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithMissingChunk()
{
......@@ -137,7 +137,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithUnexpectedChunkIndex()
{
......@@ -152,7 +152,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithUnexpectedChunkSize()
{
......
......@@ -6,9 +6,9 @@ use MongoDB\Collection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Exception\RuntimeException;
use MongoDB\Operation\BulkWrite;
use DateTime;
use Exception;
use IteratorIterator;
use LogicException;
use MultipleIterator;
......@@ -50,7 +50,7 @@ class SpecFunctionalTest extends FunctionalTestCase
try {
$result = $this->executeAct($test['act']);
} catch (RuntimeException $e) {
} catch (Exception $e) {
$result = $e;
}
......@@ -333,7 +333,10 @@ class SpecFunctionalTest extends FunctionalTestCase
case 'ChunkIsMissing':
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:
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