@@ -93,6 +93,37 @@ public function testEmptyFile()
93
93
$ this ->assertEquals (255 * 1024 , $ raw ->chunkSize );
94
94
$ this ->assertTrue (is_string ($ raw ->md5 ));
95
95
}
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
+ }
96
127
public function testUploadEnsureIndexes ()
97
128
{
98
129
$ chunks = $ this ->bucket ->getCollectionsWrapper ()->getChunksCollection ();
0 commit comments