Commit 471baea1 authored by Jeremy Mikola's avatar Jeremy Mikola

Check that stream metadata contains a GridFS StreamWrapper (fixes #299)

parent b4338692
...@@ -529,8 +529,8 @@ class Bucket ...@@ -529,8 +529,8 @@ class Bucket
$metadata = stream_get_meta_data($stream); $metadata = stream_get_meta_data($stream);
if (!$metadata['wrapper_data'] instanceof StreamWrapper) { if ( ! isset ($metadata['wrapper_data']) || ! $metadata['wrapper_data'] instanceof StreamWrapper) {
throw InvalidArgumentException::invalidType('$stream wrapper data', $metadata['wrapper_data'], 'MongoDB\Driver\GridFS\StreamWrapper'); throw InvalidArgumentException::invalidType('$stream wrapper data', isset($metadata['wrapper_data']) ? $metadata['wrapper_data'] : null, 'MongoDB\Driver\GridFS\StreamWrapper');
} }
return $metadata['wrapper_data']->getFile(); return $metadata['wrapper_data']->getFile();
......
...@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase
public function provideInvalidStreamValues() public function provideInvalidStreamValues()
{ {
return $this->wrapValuesForDataProvider([null, 123, 'foo', [], hash_init('md5')]); return $this->wrapValuesForDataProvider($this->getInvalidStreamValues());
} }
/** /**
...@@ -407,13 +407,18 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -407,13 +407,18 @@ class BucketFunctionalTest extends FunctionalTestCase
/** /**
* @expectedException MongoDB\Exception\InvalidArgumentException * @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues * @dataProvider provideInvalidGridFSStreamValues
*/ */
public function testGetFileDocumentForStreamShouldRequireStreamResource($stream) public function testGetFileDocumentForStreamShouldRequireGridFSStreamResource($stream)
{ {
$this->bucket->getFileDocumentForStream($stream); $this->bucket->getFileDocumentForStream($stream);
} }
public function provideInvalidGridFSStreamValues()
{
return $this->wrapValuesForDataProvider(array_merge($this->getInvalidStreamValues(), [$this->createStream()]));
}
public function testGetFileIdForStreamUsesTypeMap() public function testGetFileIdForStreamUsesTypeMap()
{ {
$stream = $this->bucket->openUploadStream('filename', ['_id' => ['x' => 1]]); $stream = $this->bucket->openUploadStream('filename', ['_id' => ['x' => 1]]);
...@@ -441,9 +446,9 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -441,9 +446,9 @@ class BucketFunctionalTest extends FunctionalTestCase
/** /**
* @expectedException MongoDB\Exception\InvalidArgumentException * @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues * @dataProvider provideInvalidGridFSStreamValues
*/ */
public function testGetFileIdForStreamShouldRequireStreamResource($stream) public function testGetFileIdForStreamShouldRequireGridFSStreamResource($stream)
{ {
$this->bucket->getFileIdForStream($stream); $this->bucket->getFileIdForStream($stream);
} }
...@@ -714,4 +719,14 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -714,4 +719,14 @@ class BucketFunctionalTest extends FunctionalTestCase
call_user_func($callback, $foundIndex); call_user_func($callback, $foundIndex);
} }
} }
/**
* Return a list of invalid stream values.
*
* @return array
*/
private function getInvalidStreamValues()
{
return [null, 123, 'foo', [], hash_init('md5')];
}
} }
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