Commit 6590854b authored by Jeremy Mikola's avatar Jeremy Mikola

Clean up BucketFunctionalTest

parent 9b219418
...@@ -2,9 +2,14 @@ ...@@ -2,9 +2,14 @@
namespace MongoDB\Tests\GridFS; namespace MongoDB\Tests\GridFS;
use MongoDB\BSON\Binary;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\GridFS\Bucket; use MongoDB\GridFS\Bucket;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\ListCollections;
use MongoDB\Operation\ListIndexes;
/** /**
* Functional tests for the Bucket class. * Functional tests for the Bucket class.
...@@ -53,271 +58,445 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -53,271 +58,445 @@ class BucketFunctionalTest extends FunctionalTestCase
return $options; return $options;
} }
/**
* @dataProvider provideInputDataAndExpectedChunks
*/
public function testDelete($input, $expectedChunks)
{
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, $expectedChunks);
$this->bucket->delete($id);
$this->assertCollectionCount($this->filesCollection, 0);
$this->assertCollectionCount($this->chunksCollection, 0);
}
public function provideInputDataAndExpectedChunks()
{
return [
['', 0],
['foobar', 1],
[str_repeat('a', 261120), 1],
[str_repeat('a', 261121), 2],
[str_repeat('a', 522240), 2],
[str_repeat('a', 522241), 3],
[str_repeat('foobar', 43520), 1],
[str_repeat('foobar', 43521), 2],
[str_repeat('foobar', 87040), 2],
[str_repeat('foobar', 87041), 3],
];
}
/**
* @expectedException MongoDB\GridFS\Exception\FileNotFoundException
*/
public function testDeleteShouldRequireFileToExist()
{
$this->bucket->delete('nonexistent-id');
}
/**
* @dataProvider provideInputDataAndExpectedChunks
*/
public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expectedChunks)
{
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, $expectedChunks);
$this->filesCollection->deleteOne(['_id' => $id]);
try {
$this->bucket->delete($id);
$this->fail('FileNotFoundException was not thrown');
} catch (FileNotFoundException $e) {}
$this->assertCollectionCount($this->chunksCollection, 0);
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
*/
public function testDownloadingFileWithMissingChunk()
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$this->chunksCollection->deleteOne(['files_id' => $id, 'n' => 0]);
stream_get_contents($this->bucket->openDownloadStream($id));
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
*/
public function testDownloadingFileWithUnexpectedChunkIndex()
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$this->chunksCollection->updateOne(
['files_id' => $id, 'n' => 0],
['$set' => ['n' => 1]]
);
stream_get_contents($this->bucket->openDownloadStream($id));
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
*/
public function testDownloadingFileWithUnexpectedChunkSize()
{
$id = $this->bucket->uploadFromStream("filename", $this->createStream("foobar"));
$this->chunksCollection->updateOne(
['files_id' => $id, 'n' => 0],
['$set' => ['data' => new Binary('fooba', Binary::TYPE_GENERIC)]]
);
stream_get_contents($this->bucket->openDownloadStream($id));
}
/**
* @dataProvider provideInputDataAndExpectedChunks
*/
public function testDownloadToStream($input)
{
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$destination = $this->createStream();
$this->bucket->downloadToStream($id, $destination);
$this->assertStreamContents($input, $destination);
}
/**
* @expectedException MongoDB\GridFS\Exception\FileNotFoundException
*/
public function testDownloadToStreamShouldRequireFileToExist()
{
$this->bucket->downloadToStream('nonexistent-id', $this->createStream());
}
public function testDrop()
{
$this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 1);
$this->bucket->drop();
$this->assertCollectionDoesNotExist($this->filesCollection->getCollectionName());
$this->assertCollectionDoesNotExist($this->chunksCollection->getCollectionName());
}
public function testFind()
{
$this->bucket->uploadFromStream('a', $this->createStream('foo'));
$this->bucket->uploadFromStream('b', $this->createStream('foobar'));
$this->bucket->uploadFromStream('c', $this->createStream('foobarbaz'));
$cursor = $this->bucket->find(
['length' => ['$lte' => 6]],
[
'projection' => [
'filename' => 1,
'length' => 1,
'_id' => 0,
],
'sort' => ['length' => -1],
]
);
$expected = [
['filename' => 'b', 'length' => 6],
['filename' => 'a', 'length' => 3],
];
$this->assertSameDocuments($expected, $cursor);
}
public function testGetDatabaseName() public function testGetDatabaseName()
{ {
$this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName()); $this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName());
} }
public function testBasicOperations() public function testGetIdFromStream()
{ {
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello world")); $id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
$contents = stream_get_contents($this->bucket->openDownloadStream($id)); $stream = $this->bucket->openDownloadStream($id);
$this->assertEquals("hello world", $contents);
$this->assertEquals(1, $this->bucket->getCollectionWrapper()->getFilesCollection()->count());
$this->assertEquals(1, $this->bucket->getCollectionWrapper()->getChunksCollection()->count());
$this->bucket->delete($id); $this->assertEquals($id, $this->bucket->getIdFromStream($stream));
$error=null;
try{
$this->bucket->openDownloadStream($id);
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals(0, $this->bucket->getCollectionWrapper()->getFilesCollection()->count());
$this->assertEquals(0, $this->bucket->getCollectionWrapper()->getChunksCollection()->count());
} }
public function testMultiChunkDelete()
/**
* @dataProvider provideInputDataAndExpectedChunks
*/
public function testOpenDownloadStream($input)
{ {
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello"), ['chunkSizeBytes'=>1]); $id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$this->assertEquals(1, $this->bucket->getCollectionWrapper()->getFilesCollection()->count());
$this->assertEquals(5, $this->bucket->getCollectionWrapper()->getChunksCollection()->count()); $this->assertStreamContents($input, $this->bucket->openDownloadStream($id));
$this->bucket->delete($id);
$this->assertEquals(0, $this->bucket->getCollectionWrapper()->getFilesCollection()->count());
$this->assertEquals(0, $this->bucket->getCollectionWrapper()->getChunksCollection()->count());
} }
public function testEmptyFile() /**
* @dataProvider provideInputDataAndExpectedChunks
*/
public function testOpenDownloadStreamAndMultipleReadOperations($input)
{ {
$id = $this->bucket->uploadFromStream("test_filename",$this->generateStream("")); $id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$contents = stream_get_contents($this->bucket->openDownloadStream($id)); $stream = $this->bucket->openDownloadStream($id);
$this->assertEquals("", $contents); $buffer = '';
$this->assertEquals(1, $this->bucket->getCollectionWrapper()->getFilesCollection()->count());
$this->assertEquals(0, $this->bucket->getCollectionWrapper()->getChunksCollection()->count());
$raw = $this->bucket->getCollectionWrapper()->getFilesCollection()->findOne(); while (strlen($buffer) < strlen($input)) {
$this->assertEquals(0, $raw->length); $expectedReadLength = min(4096, strlen($input) - strlen($buffer));
$this->assertEquals($id, $raw->_id); $buffer .= $read = fread($stream, 4096);
$this->assertTrue($raw->uploadDate instanceof \MongoDB\BSON\UTCDateTime);
$this->assertEquals(255 * 1024, $raw->chunkSize); $this->assertInternalType('string', $read);
$this->assertTrue(is_string($raw->md5)); $this->assertEquals($expectedReadLength, strlen($read));
}
$this->assertTrue(fclose($stream));
$this->assertEquals($input, $buffer);
} }
public function testCorruptChunk()
/**
* @expectedException MongoDB\GridFS\Exception\FileNotFoundException
*/
public function testOpenDownloadStreamShouldRequireFileToExist()
{ {
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("foobar")); $this->bucket->openDownloadStream('nonexistent-id');
}
$this->collectionWrapper->getChunksCollection()->updateOne(['files_id' => $id], /**
['$set' => ['data' => new \MongoDB\BSON\Binary('foo', \MongoDB\BSON\Binary::TYPE_GENERIC)]]); * @expectedException MongoDB\GridFS\Exception\FileNotFoundException
$error = null; */
try{ public function testOpenDownloadStreamByNameShouldRequireFilenameToExist()
$download = $this->bucket->openDownloadStream($id); {
stream_get_contents($download); $this->bucket->openDownloadStream('nonexistent-filename');
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError);
} }
public function testErrorsOnMissingChunk()
public function testOpenDownloadStreamByName()
{ {
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello world,abcdefghijklmnopqrstuv123456789"), ["chunkSizeBytes" => 1]); $this->bucket->uploadFromStream('filename', $this->createStream('foo'));
$this->bucket->uploadFromStream('filename', $this->createStream('bar'));
$this->bucket->uploadFromStream('filename', $this->createStream('baz'));
$this->collectionWrapper->getChunksCollection()->deleteOne(['files_id' => $id, 'n' => 7]); $this->assertStreamContents('baz', $this->bucket->openDownloadStreamByName('filename'));
$error = null; $this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('filename', ['revision' => -3]));
try{ $this->assertStreamContents('bar', $this->bucket->openDownloadStreamByName('filename', ['revision' => -2]));
$download = $this->bucket->openDownloadStream($id); $this->assertStreamContents('baz', $this->bucket->openDownloadStreamByName('filename', ['revision' => -1]));
stream_get_contents($download); $this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('filename', ['revision' => 0]));
} catch(\MongoDB\Exception\Exception $e) { $this->assertStreamContents('bar', $this->bucket->openDownloadStreamByName('filename', ['revision' => 1]));
$error = $e; $this->assertStreamContents('baz', $this->bucket->openDownloadStreamByName('filename', ['revision' => 2]));
}
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError);
} }
public function testUploadEnsureIndexes()
/**
* @expectedException MongoDB\GridFS\Exception\FileNotFoundException
* @dataProvider provideNonexistentFilenameAndRevision
*/
public function testOpenDownloadStreamByNameShouldRequireFilenameAndRevisionToExist($filename, $revision)
{ {
$chunks = $this->bucket->getCollectionWrapper()->getChunksCollection(); $this->bucket->uploadFromStream('filename', $this->createStream('foo'));
$files = $this->bucket->getCollectionWrapper()->getFilesCollection(); $this->bucket->uploadFromStream('filename', $this->createStream('bar'));
$this->bucket->uploadFromStream("filename", $this->generateStream("junk"));
$chunksIndexed = false; $this->bucket->openDownloadStream($filename, ['revision' => $revision]);
foreach($chunks->listIndexes() as $index) { }
$chunksIndexed = $chunksIndexed || ($index->isUnique() && $index->getKey() === ['files_id' => 1, 'n' => 1]);
}
$this->assertTrue($chunksIndexed);
$filesIndexed = false; public function provideNonexistentFilenameAndRevision()
foreach($files->listIndexes() as $index) { {
$filesIndexed = $filesIndexed || ($index->getKey() === ['filename' => 1, 'uploadDate' => 1]); return [
} ['filename', 2],
$this->assertTrue($filesIndexed); ['filename', -3],
} ['nonexistent-filename', 0],
public function testGetLastVersion() ['nonexistent-filename', -1],
{ ];
$idOne = $this->bucket->uploadFromStream("test",$this->generateStream("foo"));
$streamTwo = $this->bucket->openUploadStream("test");
fwrite($streamTwo, "bar");
//echo "Calling FSTAT\n";
//$stat = fstat($streamTwo);
$idTwo = $this->bucket->getIdFromStream($streamTwo);
//var_dump
//var_dump($idTwo);
fclose($streamTwo);
$idThree = $this->bucket->uploadFromStream("test",$this->generateStream("baz"));
$this->assertEquals("baz", stream_get_contents($this->bucket->openDownloadStreamByName("test")));
$this->bucket->delete($idThree);
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test")));
$this->bucket->delete($idTwo);
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test")));
$this->bucket->delete($idOne);
$error = null;
try{
$this->bucket->openDownloadStreamByName("test");
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
} }
public function testGetVersion()
public function testOpenUploadStream()
{ {
$this->bucket->uploadFromStream("test",$this->generateStream("foo")); $stream = $this->bucket->openUploadStream('filename');
$this->bucket->uploadFromStream("test",$this->generateStream("bar"));
$this->bucket->uploadFromStream("test",$this->generateStream("baz"));
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => 0]))); fwrite($stream, 'foobar');
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => 1]))); fclose($stream);
$this->assertEquals("baz", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => 2])));
$this->assertEquals("baz", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -1]))); $this->assertStreamContents('foobar', $this->bucket->openDownloadStreamByName('filename'));
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -2]))); }
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -3])));
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException'; /**
$error = null; * @dataProvider provideInputDataAndExpectedChunks
try{ */
$this->bucket->openDownloadStreamByName("test", ['revision' => 3]); public function testOpenUploadStreamAndMultipleWriteOperations($input)
} catch(\MongoDB\Exception\Exception $e) { {
$error = $e; $stream = $this->bucket->openUploadStream('filename');
} $offset = 0;
$this->assertTrue($error instanceof $fileNotFound);
$error = null; while ($offset < strlen($input)) {
try{ $expectedWriteLength = min(4096, strlen($input) - $offset);
$this->bucket->openDownloadStreamByName("test", ['revision' => -4]); $writeLength = fwrite($stream, substr($input, $offset, 4096));
} catch(\MongoDB\Exception\Exception $e) { $offset += $writeLength;
$error = $e;
} $this->assertEquals($expectedWriteLength, $writeLength);
$this->assertTrue($error instanceof $fileNotFound);
}
public function testGridfsFind()
{
$this->bucket->uploadFromStream("two",$this->generateStream("test2"));
usleep(5000);
$this->bucket->uploadFromStream("two",$this->generateStream("test2+"));
usleep(5000);
$this->bucket->uploadFromStream("one",$this->generateStream("test1"));
usleep(5000);
$this->bucket->uploadFromStream("two",$this->generateStream("test2++"));
$cursor = $this->bucket->find(["filename" => "two"]);
$count = count($cursor->toArray());
$this->assertEquals(3, $count);
$cursor = $this->bucket->find([]);
$count = count($cursor->toArray());
$this->assertEquals(4, $count);
$cursor = $this->bucket->find([], ["noCursorTimeout"=>false, "sort"=>["uploadDate"=> -1], "skip"=>1, "limit"=>2]);
$outputs = ["test1", "test2+"];
$i=0;
foreach($cursor as $file){
$contents = stream_get_contents($this->bucket->openDownloadStream($file->_id));
$this->assertEquals($outputs[$i], $contents);
$i++;
} }
$this->assertTrue(fclose($stream));
$this->assertStreamContents($input, $this->bucket->openDownloadStreamByName('filename'));
} }
public function testGridInNonIntChunksize()
public function testRename()
{ {
$id = $this->bucket->uploadFromStream("f",$this->generateStream("data")); $id = $this->bucket->uploadFromStream('a', $this->createStream('foo'));
$this->bucket->getCollectionWrapper()->getFilesCollection()->updateOne(["filename"=>"f"], $this->bucket->rename($id, 'b');
['$set'=> ['chunkSize' => 100.00]]);
$this->assertEquals("data", stream_get_contents($this->bucket->openDownloadStream($id))); $fileDocument = $this->filesCollection->findOne(
['_id' => $id],
['projection' => ['filename' => 1, '_id' => 0]]
);
$this->assertSameDocument(['filename' => 'b'], $fileDocument);
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('b'));
} }
public function testBigInsert()
public function testRenameShouldNotRequireFileToBeModified()
{ {
for ($tmpStream = tmpfile(), $i = 0; $i < 20; $i++) { $id = $this->bucket->uploadFromStream('a', $this->createStream('foo'));
fwrite($tmpStream, str_repeat('a', 1048576)); $this->bucket->rename($id, 'a');
}
fseek($tmpStream, 0); $fileDocument = $this->filesCollection->findOne(
$this->bucket->uploadFromStream("BigInsertTest", $tmpStream); ['_id' => $id],
fclose($tmpStream); ['projection' => ['filename' => 1, '_id' => 0]]
);
$this->assertSameDocument(['filename' => 'a'], $fileDocument);
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('a'));
} }
public function testGetIdFromStream()
/**
* @expectedException MongoDB\GridFS\Exception\FileNotFoundException
*/
public function testRenameShouldRequireFileToExist()
{ {
$upload = $this->bucket->openUploadStream("test"); $this->bucket->rename('nonexistent-id', 'b');
$id = $this->bucket->getIdFromStream($upload); }
fclose($upload);
$this->assertTrue($id instanceof \MongoDB\BSON\ObjectId);
$download = $this->bucket->openDownloadStream($id); public function testUploadFromStream()
$id=null; {
$id = $this->bucket->getIdFromStream($download); $options = [
fclose($download); '_id' => 'custom-id',
$this->assertTrue($id instanceof \MongoDB\BSON\ObjectId); 'chunkSizeBytes' => 2,
'metadata' => ['foo' => 'bar'],
];
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'), $options);
$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 3);
$this->assertSame('custom-id', $id);
$fileDocument = $this->filesCollection->findOne(['_id' => $id]);
$this->assertSameDocument(['foo' => 'bar'], $fileDocument['metadata']);
} }
public function testRename()
public function testUploadingAnEmptyFile()
{ {
$id = $this->bucket->uploadFromStream("first_name", $this->generateStream("testing")); $id = $this->bucket->uploadFromStream('filename', $this->createStream(''));
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStream($id))); $destination = $this->createStream();
$this->bucket->downloadToStream($id, $destination);
$this->bucket->rename($id, "second_name"); $this->assertStreamContents('', $destination);
$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 0);
$error = null; $fileDocument = $this->filesCollection->findOne(
try{ ['_id' => $id],
$this->bucket->openDownloadStreamByName("first_name"); [
} catch(\MongoDB\Exception\Exception $e) { 'projection' => [
$error = $e; 'length' => 1,
} 'md5' => 1,
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException'; '_id' => 0,
$this->assertTrue($error instanceof $fileNotFound); ],
]
);
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name"))); $expected = [
'length' => 0,
'md5' => 'd41d8cd98f00b204e9800998ecf8427e',
];
$this->assertSameDocument($expected, $fileDocument);
} }
public function testDrop()
public function testUploadingFirstFileCreatesIndexes()
{ {
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello world")); $this->bucket->uploadFromStream('filename', $this->createStream('foo'));
$this->bucket->drop();
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello world")); $this->assertIndexExists($this->filesCollection->getCollectionName(), 'filename_1_uploadDate_1');
$this->assertEquals(1, $this->collectionWrapper->getFilesCollection()->count()); $this->assertIndexExists($this->chunksCollection->getCollectionName(), 'files_id_1_n_1', function(IndexInfo $info) {
$this->assertTrue($info->isUnique());
});
} }
/** /**
*@dataProvider provideInsertChunks * Asserts that a collection with the given name does not exist on the
* server.
*
* @param string $collectionName
*/ */
public function testProvidedMultipleReads($data) private function assertCollectionDoesNotExist($collectionName)
{ {
$upload = $this->bucket->openUploadStream("test", ["chunkSizeBytes"=>rand(1, 5)]); $operation = new ListCollections($this->getDatabaseName());
fwrite($upload,$data); $collections = $operation->execute($this->getPrimaryServer());
$id = $this->bucket->getIdFromStream($upload);
fclose($upload); $foundCollection = null;
$download = $this->bucket->openDownloadStream($id);
$readPos = 0; foreach ($collections as $collection) {
while($readPos < strlen($data)){ if ($collection->getName() === $collectionName) {
$numToRead = rand(1, strlen($data) - $readPos); $foundCollection = $collection;
$expected = substr($data, $readPos, $numToRead); break;
$actual = fread($download, $numToRead); }
$this->assertEquals($expected,$actual);
$readPos+= $numToRead;
} }
$actual = fread($download, 5);
$expected = ""; $this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
$this->assertEquals($expected,$actual);
fclose($download);
} }
private function generateStream($input)
/**
* Asserts that an index with the given name exists for the collection.
*
* An optional $callback may be provided, which should take an IndexInfo
* argument as its first and only parameter. If an IndexInfo matching the
* given name is found, it will be passed to the callback, which may perform
* additional assertions.
*
* @param string $collectionName
* @param string $indexName
* @param callable $callback
*/
private function assertIndexExists($collectionName, $indexName, $callback = null)
{ {
$stream = fopen('php://temp', 'w+'); if ($callback !== null && ! is_callable($callback)) {
fwrite($stream, $input); throw new InvalidArgumentException('$callback is not a callable');
rewind($stream); }
return $stream;
$operation = new ListIndexes($this->getDatabaseName(), $collectionName);
$indexes = $operation->execute($this->getPrimaryServer());
$foundIndex = null;
foreach ($indexes as $index) {
if ($index->getName() === $indexName) {
$foundIndex = $index;
break;
}
}
$this->assertNotNull($foundIndex, sprintf('Index %s does not exist', $indexName));
if ($callback !== null) {
call_user_func($callback, $foundIndex);
}
} }
} }
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
namespace MongoDB\Tests\GridFS; namespace MongoDB\Tests\GridFS;
use MongoDB\GridFS;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\GridFS\Bucket;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/** /**
...@@ -12,28 +12,44 @@ use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; ...@@ -12,28 +12,44 @@ use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
abstract class FunctionalTestCase extends BaseFunctionalTestCase abstract class FunctionalTestCase extends BaseFunctionalTestCase
{ {
protected $bucket; protected $bucket;
protected $collectionWrapper; protected $chunksCollection;
protected $filesCollection;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
foreach(['fs.files', 'fs.chunks'] as $collection){
$col = new Collection($this->manager, $this->getDatabaseName(), $collection); $this->bucket = new Bucket($this->manager, $this->getDatabaseName());
$col->drop(); $this->bucket->drop();
}
$this->bucket = new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName()); $this->chunksCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.chunks');
$this->collectionWrapper = $this->bucket->getCollectionWrapper(); $this->filesCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.files');
} }
public function tearDown() /**
* Rewinds a stream and asserts its contents.
*
* @param string $expectedContents
* @param resource $stream
*/
protected function assertStreamContents($expectedContents, $stream)
{ {
foreach(['fs.files', 'fs.chunks'] as $collection){ $this->assertEquals($expectedContents, stream_get_contents($stream, -1,.0));
$col = new Collection($this->manager, $this->getDatabaseName(), $collection); }
$col->drop();
} /**
if ($this->hasFailed()) { * Creates an in-memory stream with the given data.
return; *
} * @param string $data
* @return resource
*/
protected function createStream($data = '')
{
$stream = fopen('php://temp', 'w+b');
fwrite($stream, $data);
rewind($stream);
return $stream;
} }
public function provideInsertChunks() public function provideInsertChunks()
......
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