Skip to content

Commit 9d9965b

Browse files
Will Banfieldjmikola
authored andcommitted
Added corrupt chunk tests
1 parent ebf903d commit 9d9965b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/GridFS/GridFsUpload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function uploadFromStream($source)
107107
throw new UnexpectedTypeException('stream', $source);
108108
} else{
109109
$streamMetadata = stream_get_meta_data($source);
110-
110+
}
111111
while ($data = $this->readChunk($source)) {
112112
$this->insertChunk($data);
113113
}

tests/GridFS/BucketFunctionalTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ public function testEmptyFile()
9393
$this->assertEquals(255 * 1024, $raw->chunkSize);
9494
$this->assertTrue(is_string($raw->md5));
9595
}
96+
public function testCorruptChunk()
97+
{
98+
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("foobar"));
99+
100+
$this->collectionsWrapper->getChunksCollection()->updateOne(['files_id' => $id],
101+
['$set' => ['data' => new \MongoDB\BSON\Binary('foo', \MongoDB\BSON\Binary::TYPE_GENERIC)]]);
102+
$error = null;
103+
try{
104+
$download = $this->bucket->openDownloadStream($id);
105+
stream_get_contents($download);
106+
} catch(\MongoDB\Exception\Exception $e) {
107+
$error = $e;
108+
}
109+
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
110+
$this->assertTrue($error instanceof $corruptFileError);
111+
}
112+
public function testErrorsOnMissingChunk()
113+
{
114+
$id = $this->bucket->uploadFromStream("test_filename", $this->generateStream("hello world,abcdefghijklmnopqrstuv123456789"), ["chunkSizeBytes" => 1]);
115+
116+
$this->collectionsWrapper->getChunksCollection()->deleteOne(['files_id' => $id, 'n' => 7]);
117+
$error = null;
118+
try{
119+
$download = $this->bucket->openDownloadStream($id);
120+
stream_get_contents($download);
121+
} catch(\MongoDB\Exception\Exception $e) {
122+
$error = $e;
123+
}
124+
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
125+
$this->assertTrue($error instanceof $corruptFileError);
126+
}
96127
public function testUploadEnsureIndexes()
97128
{
98129
$chunks = $this->bucket->getCollectionsWrapper()->getChunksCollection();

0 commit comments

Comments
 (0)