Commit a916f8f6 authored by Jeremy Mikola's avatar Jeremy Mikola

Rewrite functional tests for internal stream classes

parent fc12c77c
...@@ -55,26 +55,4 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase ...@@ -55,26 +55,4 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
return $stream; return $stream;
} }
public function provideInsertChunks()
{
$dataVals = [];
$testArgs[][] = "hello world";
$testArgs[][] = "1234567890";
$testArgs[][] = "~!@#$%^&*()_+";
for($j=0; $j<30; $j++){
$randomTest = "";
for($i=0; $i<100; $i++){
$randomTest .= chr(rand(0, 256));
}
$testArgs[][] = $randomTest;
}
$utf8="";
for($i=0; $i<256; $i++){
$utf8 .= chr($i);
}
$testArgs[][]=$utf8;
return $testArgs;
}
} }
<?php
namespace MongoDB\Tests\GridFS;
use MongoDB\GridFS;
/**
* Functional tests for the Bucket class.
*/
class GridFSStreamTest extends FunctionalTestCase
{
public function testBasic()
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$upload->insertChunks("hello world");
$id = $upload->getId();
$upload->close();
$this->assertEquals(1, $this->collectionWrapper->getFilesCollection()->count());
$this->assertEquals(1, $this->collectionWrapper->getChunksCollection()->count());
$file = $this->collectionWrapper->findFileById($id);
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$stream = fopen('php://temp', 'w+');
$download->downloadToStream($stream);
rewind($stream);
$contents = stream_get_contents($stream);
$this->assertEquals("hello world", $contents);
fclose($stream);
#make sure it's still there!
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$stream = fopen('php://temp', 'w+');
$download->downloadToStream($stream);
rewind($stream);
$contents = stream_get_contents($stream);
$this->assertEquals("hello world", $contents);
fclose($stream);
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$id = $upload->getId();
$upload->close();
$this->assertEquals(2, $this->collectionWrapper->getFilesCollection()->count());
$this->assertEquals(1, $this->collectionWrapper->getChunksCollection()->count());
$file = $this->collectionWrapper->findFileById($id);
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$stream = fopen('php://temp', 'w+');
$download->downloadToStream($stream);
rewind($stream);
$contents = stream_get_contents($stream);
$this->assertEquals("", $contents);
}
public function testMd5()
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$upload->insertChunks("hello world\n");
$id = $upload->getId();
$upload->close();
$file = $this->collectionWrapper->findFileById($id);
$this->assertEquals("6f5902ac237024bdd0c176cb93063dc4", $file->md5);
}
public function testUploadDefaultOpts()
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$this->assertTrue($upload->getId() instanceof \MongoDB\BSON\ObjectId);
$this->assertTrue($upload->getFile()["uploadDate"] instanceof \MongoDB\BSON\UTCDateTime);
$this->assertEquals($upload->getFile()["filename"], "test");
$this->assertEquals($upload->getLength(),0);
$this->assertTrue(!isset($upload->getFile()["contentType"]));
$this->assertTrue(!isset($upload->getFile()["aliases"]));
$this->assertTrue(!isset($upload->getFile()["metadata"]));
$this->assertEquals(255 * 1024, $upload->getChunkSize());
}
public function testUploadCustomOpts()
{
$options = ["chunkSizeBytes" => 1,
"contentType" => "text/html",
"aliases" => ["foo", "bar"],
"metadata" => ["foo" => 1, "bar" => 2]
];
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test", $options);
$this->assertEquals($upload->getChunkSize(), 1);
$this->assertEquals($upload->getFile()["contentType"], "text/html");
$this->assertEquals($upload->getFile()["aliases"], ["foo", "bar"]);
$this->assertEquals($upload->getFile()["metadata"], ["foo" => 1, "bar" => 2]);
}
public function testDownloadDefaultOpts()
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$upload->close();
$file = $this->collectionWrapper->findFileById($upload->getId());
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$download->close();
$this->assertEquals($upload->getId(), $download->getId());
$this->assertEquals(0, $download->getFile()->length);
$this->assertTrue(!isset($download->getFile()->contentType));
$this->assertTrue(!isset($download->getFile()->aliases));
$this->assertTrue(!isset($download->getFile()->metadata));
$this->assertTrue($download->getFile()->uploadDate instanceof \MongoDB\BSON\UTCDateTime);
$this->assertEquals(255 * 1024, $download->getFile()->chunkSize);
$this->assertEquals("d41d8cd98f00b204e9800998ecf8427e", $download->getFile()->md5);
}
public function testDownloadCustomOpts()
{
$options = ["chunkSizeBytes" => 1000,
"contentType" => "text/html",
"aliases" => ["foo", "bar"],
"metadata" => ["foo" => 1, "bar" => 2]
];
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test", $options);
$upload->insertChunks("hello world");
$upload->close();
$file = $this->collectionWrapper->findFileById($upload->getId());
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$this->assertEquals("test", $download->getFile()->filename);
$this->assertEquals($upload->getId(), $download->getId());
$this->assertEquals(11, $download->getFile()->length);
$this->assertEquals("text/html", $download->getFile()->contentType);
$this->assertEquals(1000, $download->getFile()->chunkSize);
$this->assertEquals(["foo", "bar"], $download->getFile()->aliases);
$this->assertEquals(["foo"=> 1, "bar"=> 2], (array) $download->getFile()->metadata);
$this->assertEquals("5eb63bbbe01eeed093cb22bb8f5acdc3", $download->getFile()->md5);
}
/**
*@dataProvider provideInsertChunks
*/
public function testInsertChunks($data)
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$upload->insertChunks($data);
$upload->close();
$stream = $this->bucket->openDownloadStream($upload->getId());
$this->assertEquals($data, stream_get_contents($stream));
}
public function testMultiChunkFile()
{
$toUpload="";
for($i=0; $i<255*1024+1000; $i++){
$toUpload .= "a";
}
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test");
$upload->insertChunks($toUpload);
$upload->close();
$this->assertEquals(1, $this->collectionWrapper->getFilesCollection()->count());
$this->assertEquals(2, $this->collectionWrapper->getChunksCollection()->count());
$download = $this->bucket->openDownloadStream($upload->getId());
$this->assertEquals($toUpload, stream_get_contents($download));
}
/**
*@dataProvider provideInsertChunks
*/
public function testSmallChunks($data)
{
$options = ["chunkSizeBytes"=>1];
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test", $options);
$upload->insertChunks($data);
$upload->close();
$this->assertEquals(strlen($data), $this->collectionWrapper->getChunksCollection()->count());
$this->assertEquals(1, $this->collectionWrapper->getFilesCollection()->count());
$stream = $this->bucket->openDownloadStream($upload->getId());
$this->assertEquals($data, stream_get_contents($stream));
}
public function testMultipleReads()
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test", ["chunkSizeBytes"=>3]);
$upload->insertChunks("hello world");
$upload->close();
$file = $this->collectionWrapper->findFileById($upload->getId());
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$this->assertEquals("he", $download->downloadNumBytes(2));
$this->assertEquals("ll", $download->downloadNumBytes(2));
$this->assertEquals("o ", $download->downloadNumBytes(2));
$this->assertEquals("wo", $download->downloadNumBytes(2));
$this->assertEquals("rl", $download->downloadNumBytes(2));
$this->assertEquals("d", $download->downloadNumBytes(2));
$this->assertEquals("", $download->downloadNumBytes(2));
$this->assertEquals("", $download->downloadNumBytes(2));
$download->close();
}
/**
*@dataProvider provideInsertChunks
*/
public function testProvidedMultipleReads($data)
{
$upload = new \MongoDB\GridFS\WritableStream($this->collectionWrapper, "test", ["chunkSizeBytes"=>rand(1, 5)]);
$upload->insertChunks($data);
$upload->close();
$file = $this->collectionWrapper->findFileById($upload->getId());
$download = new \MongoDB\GridFS\ReadableStream($this->collectionWrapper, $file);
$readPos = 0;
while($readPos < strlen($data)){
$numToRead = rand(1, strlen($data) - $readPos);
$expected = substr($data, $readPos, $numToRead);
$actual = $download->downloadNumBytes($numToRead);
$this->assertEquals($expected,$actual);
$readPos+= $numToRead;
}
$actual = $download->downloadNumBytes(5);
$expected = "";
$this->assertEquals($expected,$actual);
$download->close();
}
/**
* @expectedException \MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidUploadConstructorOptions
*/
public function testUploadConstructorOptionTypeChecks(array $options)
{
new \MongoDB\GridFS\WritableStream($this->collectionWrapper,"test", $options);
}
public function provideInvalidUploadConstructorOptions()
{
$options = [];
$invalidContentType = [123, 3.14, true, [], new \stdClass];
$invalidAliases = ['foo', 3.14, true, [12, 34], new \stdClass];
$invalidMetadata = ['foo', 3.14, true];
foreach ($invalidContentType as $value) {
$options[][] = ['contentType' => $value];
}
foreach ($invalidAliases as $value) {
$options[][] = ['aliases' => $value];
}
foreach ($invalidMetadata as $value) {
$options[][] = ['metadata' => $value];
}
return $options;
}
}
<?php
namespace MongoDB\Tests\GridFS;
use MongoDB\BSON\Binary;
use MongoDB\GridFS\CollectionWrapper;
use MongoDB\GridFS\ReadableStream;
/**
* Functional tests for the internal ReadableStream class.
*/
class ReadableStreamFunctionalTest extends FunctionalTestCase
{
private $collectionWrapper;
public function setUp()
{
parent::setUp();
$this->collectionWrapper = new CollectionWrapper($this->manager, $this->getDatabaseName(), 'fs');
$this->filesCollection->insertMany([
['_id' => 'length-0', 'length' => 0, 'chunkSize' => 4],
['_id' => 'length-0-with-empty-chunk', 'length' => 0, 'chunkSize' => 4],
['_id' => 'length-2', 'length' => 2, 'chunkSize' => 4],
['_id' => 'length-8', 'length' => 8, 'chunkSize' => 4],
['_id' => 'length-10', 'length' => 10, 'chunkSize' => 4],
]);
$this->chunksCollection->insertMany([
['_id' => 1, 'files_id' => 'length-0-with-empty-chunk', 'n' => 0, 'data' => new Binary('', Binary::TYPE_GENERIC)],
['_id' => 2, 'files_id' => 'length-2', 'n' => 0, 'data' => new Binary('ab', Binary::TYPE_GENERIC)],
['_id' => 3, 'files_id' => 'length-8', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
['_id' => 4, 'files_id' => 'length-8', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
['_id' => 5, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
['_id' => 6, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
['_id' => 7, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)],
]);
}
public function testValidConstructorFileDocument()
{
new ReadableStream($this->collectionWrapper, (object) ['_id' => null, 'chunkSize' => 1, 'length' => 0]);
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @dataProvider provideInvalidConstructorFileDocuments
*/
public function testConstructorFileDocumentChecks($file)
{
new ReadableStream($this->collectionWrapper, $file);
}
public function provideInvalidConstructorFileDocuments()
{
$options = [];
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = (object) ['_id' => 1, 'chunkSize' => $value, 'length' => 0];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = (object) ['_id' => 1, 'chunkSize' => 1, 'length' => $value];
}
$options[][] = (object) ['_id' => 1, 'chunkSize' => 0, 'length' => 0];
$options[][] = (object) ['_id' => 1, 'chunkSize' => 1, 'length' => -1];
$options[][] = (object) ['chunkSize' => 1, 'length' => 0];
return $options;
}
/**
* @dataProvider provideFileIdAndExpectedBytes
*/
public function testDownloadNumBytes($fileId, $numBytes, $expectedBytes)
{
$fileDocument = $this->collectionWrapper->findFileById($fileId);
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$this->assertSame($expectedBytes, $stream->downloadNumBytes($numBytes));
}
public function provideFileIdAndExpectedBytes()
{
return [
['length-0', 0, ''],
['length-0', 2, ''],
['length-0-with-empty-chunk', 0, ''],
['length-0-with-empty-chunk', 2, ''],
['length-2', 0, ''],
['length-2', 2, 'ab'],
['length-2', 4, 'ab'],
['length-8', 0, ''],
['length-8', 2, 'ab'],
['length-8', 4, 'abcd'],
['length-8', 6, 'abcdef'],
['length-8', 8, 'abcdefgh'],
['length-8', 10, 'abcdefgh'],
['length-10', 0, ''],
['length-10', 2, 'ab'],
['length-10', 4, 'abcd'],
['length-10', 6, 'abcdef'],
['length-10', 8, 'abcdefgh'],
['length-10', 10, 'abcdefghij'],
['length-10', 12, 'abcdefghij'],
];
}
/**
* @dataProvider provideFileIdAndExpectedBytes
*/
public function testDownloadNumBytesCalledMultipleTimes($fileId, $numBytes, $expectedBytes)
{
$fileDocument = $this->collectionWrapper->findFileById($fileId);
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
for ($i = 0; $i < $numBytes; $i++) {
$expectedByte = isset($expectedBytes[$i]) ? $expectedBytes[$i] : '';
$this->assertSame($expectedByte, $stream->downloadNumBytes(1));
}
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedExceptionMessage Chunk not found for index "2"
*/
public function testDownloadNumBytesWithMissingChunk()
{
$this->chunksCollection->deleteOne(['files_id' => 'length-10', 'n' => 2]);
$fileDocument = $this->collectionWrapper->findFileById('length-10');
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$stream->downloadNumBytes(10);
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedExceptionMessage Expected chunk to have index "1" but found "2"
*/
public function testDownloadNumBytesWithUnexpectedChunkIndex()
{
$this->chunksCollection->deleteOne(['files_id' => 'length-10', 'n' => 1]);
$fileDocument = $this->collectionWrapper->findFileById('length-10');
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$stream->downloadNumBytes(10);
}
/**
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
* @expectedExceptionMessage Expected chunk to have size "2" but found "1"
*/
public function testDownloadNumBytesWithUnexpectedChunkSize()
{
$this->chunksCollection->updateOne(
['files_id' => 'length-10', 'n' => 2],
['$set' => ['data' => new Binary('i', Binary::TYPE_GENERIC)]]
);
$fileDocument = $this->collectionWrapper->findFileById('length-10');
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$stream->downloadNumBytes(10);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public function testDownloadNumBytesWithNegativeReadSize()
{
$fileDocument = $this->collectionWrapper->findFileById('length-0');
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$stream->downloadNumBytes(-1);
}
}
<?php
namespace MongoDB\Tests\GridFS;
use MongoDB\GridFS\CollectionWrapper;
use MongoDB\GridFS\WritableStream;
/**
* Functional tests for the internal WritableStream class.
*/
class WritableStreamFunctionalTest extends FunctionalTestCase
{
private $collectionWrapper;
public function setUp()
{
parent::setUp();
$this->collectionWrapper = new CollectionWrapper($this->manager, $this->getDatabaseName(), 'fs');
}
public function testValidConstructorOptions()
{
new WritableStream($this->collectionWrapper, 'filename', [
'_id' => 'custom-id',
'chunkSizeBytes' => 2,
'metadata' => ['foo' => 'bar'],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new WritableStream($this->collectionWrapper, 'filename', $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['chunkSizeBytes' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['metadata' => $value];
}
return $options;
}
/**
* @dataProvider provideInputDataAndExpectedMD5
*/
public function testInsertChunksCalculatesMD5($input, $expectedMD5)
{
$stream = new WritableStream($this->collectionWrapper, 'filename');
$stream->insertChunks($input);
$stream->close();
$fileDocument = $this->filesCollection->findOne(
['_id' => $stream->getId()],
['projection' => ['md5' => 1, '_id' => 0]]
);
$this->assertSameDocument(['md5' => $expectedMD5], $fileDocument);
}
public function provideInputDataAndExpectedMD5()
{
return [
['', 'd41d8cd98f00b204e9800998ecf8427e'],
['foobar', '3858f62230ac3c915f300c664312c63f'],
[str_repeat('foobar', 43520), '88ff0e5fcb0acb27947d736b5d69cb73'],
[str_repeat('foobar', 43521), '8ff86511c95a06a611842ceb555d8454'],
[str_repeat('foobar', 87040), '45bfa1a9ec36728ee7338d15c5a30c13'],
[str_repeat('foobar', 87041), '95e78f624f8e745bcfd2d11691fa601e'],
];
}
/**
* @dataProvider provideInputDataAndExpectedMD5
*/
public function testUploadFromStreamCalculatesMD5($input, $expectedMD5)
{
$stream = new WritableStream($this->collectionWrapper, 'filename');
$stream->uploadFromStream($this->createStream($input));
//$stream->close();
$fileDocument = $this->filesCollection->findOne(
['_id' => $stream->getId()],
['projection' => ['md5' => 1, '_id' => 0]]
);
$this->assertSameDocument(['md5' => $expectedMD5], $fileDocument);
}
}
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