MongoGridFSTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. class MongoGridFSTest extends TestCase
  5. {
  6. public function testSerialize()
  7. {
  8. $this->assertIsString(serialize($this->getGridFS()));
  9. }
  10. public function testChunkProperty()
  11. {
  12. $collection = $this->getGridFS();
  13. $this->assertInstanceOf('MongoCollection', $collection->chunks);
  14. $this->assertSame('mongo-php-adapter.fs.chunks', (string) $collection->chunks);
  15. }
  16. public function testCustomCollectionName()
  17. {
  18. $collection = $this->getGridFS('foofs');
  19. $this->assertSame('mongo-php-adapter.foofs.files', (string) $collection);
  20. $this->assertInstanceOf('MongoCollection', $collection->chunks);
  21. $this->assertSame('mongo-php-adapter.foofs.chunks', (string) $collection->chunks);
  22. }
  23. public function testDrop()
  24. {
  25. $collection = $this->getGridFS();
  26. $document = ['foo' => 'bar'];
  27. $collection->insert($document);
  28. unset($document['_id']);
  29. $collection->chunks->insert($document);
  30. $collection->drop();
  31. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  32. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  33. $this->assertSame(0, $newCollection->count());
  34. $this->assertSame(0, $newChunksCollection->count());
  35. }
  36. public function testFindReturnsGridFSCursor()
  37. {
  38. $this->prepareData();
  39. $collection = $this->getGridFS();
  40. $this->assertInstanceOf('MongoGridFSCursor', $collection->find());
  41. }
  42. public function testStoringData()
  43. {
  44. $collection = $this->getGridFS();
  45. $id = $collection->storeBytes(
  46. 'abcd',
  47. [
  48. 'foo' => 'bar',
  49. 'chunkSize' => 2,
  50. ]
  51. );
  52. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  53. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  54. $this->assertSame(1, $newCollection->count());
  55. $this->assertSame(2, $newChunksCollection->count());
  56. $record = $newCollection->findOne();
  57. $this->assertNotNull($record);
  58. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $record->_id);
  59. $this->assertSame((string) $id, (string) $record->_id);
  60. $this->assertSame('bar', $record->foo);
  61. $this->assertSame(4, $record->length);
  62. $this->assertSame(2, $record->chunkSize);
  63. $this->assertSame('e2fc714c4727ee9395f324cd2e7f331f', $record->md5);
  64. $chunksCursor = $newChunksCollection->find([], ['sort' => ['n' => 1]]);
  65. $chunks = iterator_to_array($chunksCursor);
  66. $firstChunk = $chunks[0];
  67. $this->assertNotNull($firstChunk);
  68. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $firstChunk->files_id);
  69. $this->assertSame((string) $id, (string) $firstChunk->files_id);
  70. $this->assertSame(0, $firstChunk->n);
  71. $this->assertInstanceOf('MongoDB\BSON\Binary', $firstChunk->data);
  72. $this->assertSame('ab', (string) $firstChunk->data->getData());
  73. $secondChunck = $chunks[1];
  74. $this->assertNotNull($secondChunck);
  75. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $secondChunck->files_id);
  76. $this->assertSame((string) $id, (string) $secondChunck->files_id);
  77. $this->assertSame(1, $secondChunck->n);
  78. $this->assertInstanceOf('MongoDB\BSON\Binary', $secondChunck->data);
  79. $this->assertSame('cd', (string) $secondChunck->data->getData());
  80. }
  81. public function testIndexesCreation()
  82. {
  83. $collection = $this->getGridFS();
  84. $id = $collection->storeBytes(
  85. 'abcd',
  86. [
  87. 'foo' => 'bar',
  88. 'chunkSize' => 2,
  89. ]
  90. );
  91. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  92. $indexes = iterator_to_array($newChunksCollection->listIndexes());
  93. $this->assertCount(2, $indexes);
  94. $index = $indexes[1];
  95. $this->assertSame(['files_id' => 1, 'n' => 1], $index->getKey());
  96. $this->assertTrue($index->isUnique());
  97. }
  98. public function testDelete()
  99. {
  100. $collection = $this->getGridFS();
  101. $id = $this->prepareFile();
  102. $collection->delete($id);
  103. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  104. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  105. $this->assertSame(0, $newCollection->count());
  106. $this->assertSame(0, $newChunksCollection->count());
  107. }
  108. public function testRemove()
  109. {
  110. $collection = $this->getGridFS();
  111. $this->prepareFile('data', ['foo' => 'bar']);
  112. $this->prepareFile('data', ['foo' => 'bar']);
  113. $collection->remove(['foo' => 'bar']);
  114. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  115. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  116. $this->assertSame(0, $newCollection->count());
  117. $this->assertSame(0, $newChunksCollection->count());
  118. }
  119. public function testStoreFile()
  120. {
  121. $collection = $this->getGridFS();
  122. $id = $collection->storeFile(__FILE__, ['chunkSize' => 100, 'foo' => 'bar']);
  123. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  124. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  125. $this->assertSame(1, $newCollection->count());
  126. $filename = __FILE__;
  127. $md5 = md5_file($filename);
  128. $size = filesize($filename);
  129. $record = $newCollection->findOne();
  130. $this->assertNotNull($record);
  131. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $record->_id);
  132. $this->assertSame((string) $id, (string) $record->_id);
  133. $this->assertSame('bar', $record->foo);
  134. $this->assertSame($size, $record->length);
  135. $this->assertSame(100, $record->chunkSize);
  136. $this->assertSame($md5, $record->md5);
  137. $this->assertSame($filename, $record->filename);
  138. $numberOfChunks = (int) ceil($size / 100);
  139. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  140. $expectedContent = substr(file_get_contents(__FILE__), 0, 100);
  141. $firstChunk = $newChunksCollection->findOne([], ['sort' => ['n' => 1]]);
  142. $this->assertNotNull($firstChunk);
  143. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $firstChunk->files_id);
  144. $this->assertSame((string) $id, (string) $firstChunk->files_id);
  145. $this->assertSame(0, $firstChunk->n);
  146. $this->assertInstanceOf('MongoDB\BSON\Binary', $firstChunk->data);
  147. $this->assertSame($expectedContent, (string) $firstChunk->data->getData());
  148. }
  149. public function testStoreFileResource()
  150. {
  151. $collection = $this->getGridFS();
  152. $id = $collection->storeFile(
  153. fopen(__FILE__, 'r'),
  154. ['chunkSize' => 100, 'foo' => 'bar', 'filename' => 'test.php']
  155. );
  156. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  157. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  158. $this->assertSame(1, $newCollection->count());
  159. $md5 = md5_file(__FILE__);
  160. $size = filesize(__FILE__);
  161. $filename = basename(__FILE__);
  162. $record = $newCollection->findOne();
  163. $this->assertNotNull($record);
  164. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $record->_id);
  165. $this->assertSame((string) $id, (string) $record->_id);
  166. $this->assertSame('bar', $record->foo);
  167. $this->assertSame($size, $record->length);
  168. $this->assertSame(100, $record->chunkSize);
  169. $this->assertSame($md5, $record->md5);
  170. $this->assertSame('test.php', $record->filename);
  171. $numberOfChunks = (int) ceil($size / 100);
  172. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  173. $expectedContent = substr(file_get_contents(__FILE__), 0, 100);
  174. $firstChunk = $newChunksCollection->findOne([], ['sort' => ['n' => 1]]);
  175. $this->assertNotNull($firstChunk);
  176. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $firstChunk->files_id);
  177. $this->assertSame((string) $id, (string) $firstChunk->files_id);
  178. $this->assertSame(0, $firstChunk->n);
  179. $this->assertInstanceOf('MongoDB\BSON\Binary', $firstChunk->data);
  180. $this->assertSame($expectedContent, (string) $firstChunk->data->getData());
  181. }
  182. public function testStoreUpload()
  183. {
  184. $this->skipTestIf(extension_loaded('mongo'));
  185. $collection = $this->getGridFS();
  186. $_FILES['foo'] = [
  187. 'name' => 'test.php',
  188. 'error' => UPLOAD_ERR_OK,
  189. 'tmp_name' => __FILE__,
  190. ];
  191. $id = $collection->storeUpload(
  192. 'foo',
  193. ['chunkSize' => 100, 'foo' => 'bar']
  194. );
  195. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  196. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  197. $this->assertSame(1, $newCollection->count());
  198. $md5 = md5_file(__FILE__);
  199. $size = filesize(__FILE__);
  200. $record = $newCollection->findOne();
  201. $this->assertNotNull($record);
  202. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $record->_id);
  203. $this->assertSame((string) $id, (string) $record->_id);
  204. $this->assertSame('bar', $record->foo);
  205. $this->assertSame($size, $record->length);
  206. $this->assertSame(100, $record->chunkSize);
  207. $this->assertSame($md5, $record->md5);
  208. $this->assertSame('test.php', $record->filename);
  209. $numberOfChunks = (int) ceil($size / 100);
  210. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  211. }
  212. public function testFindOneReturnsFile()
  213. {
  214. $collection = $this->getGridFS();
  215. $this->prepareFile();
  216. $result = $collection->findOne();
  217. $this->assertInstanceOf('MongoGridFSFile', $result);
  218. }
  219. public function testFindOneWithLegacyProjectionReturnsFile()
  220. {
  221. $collection = $this->getGridFS();
  222. $this->prepareFile('abcd', ['date' => new \MongoDate()]);
  223. $result = $collection->findOne([], ['date']);
  224. $this->assertInstanceOf('MongoGridFSFile', $result);
  225. $this->assertCount(2, $result->file);
  226. $this->assertArrayHasKey('date', $result->file);
  227. }
  228. public function testFindOneWithFilenameReturnsFile()
  229. {
  230. $collection = $this->getGridFS();
  231. $this->prepareFile('abcd', ['filename' => 'abcd']);
  232. $this->prepareFile('test', ['filename' => 'test']);
  233. $this->prepareFile('zyxv', ['filename' => 'zyxv']);
  234. $result = $collection->findOne('test');
  235. $this->assertInstanceOf('MongoGridFSFile', $result);
  236. $this->assertSame('test', $result->getBytes());
  237. }
  238. public function testFindOneNotFoundReturnsNull()
  239. {
  240. $collection = $this->getGridFS();
  241. $result = $collection->findOne();
  242. $this->assertNull($result);
  243. }
  244. public function testPut()
  245. {
  246. $collection = $this->getGridFS();
  247. $id = $collection->put(__FILE__, ['chunkSize' => 100, 'foo' => 'bar']);
  248. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  249. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  250. $this->assertSame(1, $newCollection->count());
  251. $size = filesize(__FILE__);
  252. $numberOfChunks = (int) ceil($size / 100);
  253. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  254. }
  255. public function testStoreByteExceptionWhileInsertingRecord()
  256. {
  257. $id = new \MongoID();
  258. $collection = $this->getGridFS();
  259. $document = ['_id' => $id];
  260. $collection->insert($document);
  261. $this->expectException(\MongoGridFSException::class);
  262. $this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
  263. $this->expectExceptionCode(11000);
  264. $collection->storeBytes('foo', ['_id' => $id]);
  265. }
  266. public function testStoreByteExceptionWhileInsertingChunks()
  267. {
  268. $collection = $this->getGridFS();
  269. $collection->chunks->createIndex(['n' => 1], ['unique' => true]);
  270. $document = ['n' => 0];
  271. $collection->chunks->insert($document);
  272. $this->expectException(\MongoGridFSException::class);
  273. $this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
  274. $this->expectExceptionCode(11000);
  275. $collection->storeBytes('foo');
  276. }
  277. public function testStoreFileExceptionWhileInsertingRecord()
  278. {
  279. $id = new \MongoID();
  280. $collection = $this->getGridFS();
  281. $document = ['_id' => $id];
  282. $collection->insert($document);
  283. $this->expectException(\MongoGridFSException::class);
  284. $this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
  285. $this->expectExceptionCode(11000);
  286. $collection->storeFile(__FILE__, ['_id' => $id]);
  287. }
  288. public function testStoreFileExceptionWhileInsertingChunks()
  289. {
  290. $collection = $this->getGridFS();
  291. $collection->chunks->createIndex(['n' => 1], ['unique' => true]);
  292. $document = ['n' => 0];
  293. $collection->chunks->insert($document);
  294. $this->expectException(\MongoGridFSException::class);
  295. $this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
  296. $this->expectExceptionCode(11000);
  297. $collection->storeFile(__FILE__);
  298. }
  299. public function testStoreFileExceptionWhileUpdatingFileRecord()
  300. {
  301. $collection = $this->getGridFS();
  302. $collection->createIndex(['length' => 1], ['unique' => true]);
  303. $document = ['length' => filesize(__FILE__)];
  304. $collection->insert($document);
  305. $this->expectException(\MongoGridFSException::class);
  306. $this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
  307. $this->expectExceptionCode(11000);
  308. $collection->storeFile(fopen(__FILE__, 'r'));
  309. }
  310. /**
  311. * @return \MongoID
  312. */
  313. protected function prepareFile($data = 'abcd', $extra = [])
  314. {
  315. $collection = $this->getGridFS();
  316. // to make sure we have multiple chunks
  317. $extra += ['chunkSize' => 2];
  318. return $collection->storeBytes($data, $extra);
  319. }
  320. }