MongoGridFSTest.php 16 KB

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