Skip to content

Check that stream metadata contains a GridFS StreamWrapper #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ private function getRawFileDocumentForStream($stream)

$metadata = stream_get_meta_data($stream);

if (!$metadata['wrapper_data'] instanceof StreamWrapper) {
throw InvalidArgumentException::invalidType('$stream wrapper data', $metadata['wrapper_data'], 'MongoDB\Driver\GridFS\StreamWrapper');
if ( ! isset ($metadata['wrapper_data']) || ! $metadata['wrapper_data'] instanceof StreamWrapper) {
throw InvalidArgumentException::invalidType('$stream wrapper data', isset($metadata['wrapper_data']) ? $metadata['wrapper_data'] : null, 'MongoDB\Driver\GridFS\StreamWrapper');
}

return $metadata['wrapper_data']->getFile();
Expand Down
25 changes: 20 additions & 5 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function testDownloadToStreamShouldRequireDestinationStream($destination)

public function provideInvalidStreamValues()
{
return $this->wrapValuesForDataProvider([null, 123, 'foo', [], hash_init('md5')]);
return $this->wrapValuesForDataProvider($this->getInvalidStreamValues());
}

/**
Expand Down Expand Up @@ -407,13 +407,18 @@ public function testGetFileDocumentForStreamWithWritableStream()

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
* @dataProvider provideInvalidGridFSStreamValues
*/
public function testGetFileDocumentForStreamShouldRequireStreamResource($stream)
public function testGetFileDocumentForStreamShouldRequireGridFSStreamResource($stream)
{
$this->bucket->getFileDocumentForStream($stream);
}

public function provideInvalidGridFSStreamValues()
{
return $this->wrapValuesForDataProvider(array_merge($this->getInvalidStreamValues(), [$this->createStream()]));
}

public function testGetFileIdForStreamUsesTypeMap()
{
$stream = $this->bucket->openUploadStream('filename', ['_id' => ['x' => 1]]);
Expand Down Expand Up @@ -441,9 +446,9 @@ public function testGetFileIdForStreamWithWritableStream()

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidStreamValues
* @dataProvider provideInvalidGridFSStreamValues
*/
public function testGetFileIdForStreamShouldRequireStreamResource($stream)
public function testGetFileIdForStreamShouldRequireGridFSStreamResource($stream)
{
$this->bucket->getFileIdForStream($stream);
}
Expand Down Expand Up @@ -714,4 +719,14 @@ private function assertIndexExists($collectionName, $indexName, $callback = null
call_user_func($callback, $foundIndex);
}
}

/**
* Return a list of invalid stream values.
*
* @return array
*/
private function getInvalidStreamValues()
{
return [null, 123, 'foo', [], hash_init('md5')];
}
}