MongoGridFSTest.php 14 KB

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