MongoGridFSTest.php 15 KB

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