MongoGridFSTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. $collection = $this->getGridFS();
  194. $_FILES['foo'] = [
  195. 'name' => 'test.php',
  196. 'error' => UPLOAD_ERR_OK,
  197. 'tmp_name' => __FILE__,
  198. ];
  199. $id = $collection->storeUpload(
  200. 'foo',
  201. ['chunkSize' => 100, 'foo' => 'bar']
  202. );
  203. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  204. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  205. $this->assertSame(1, $newCollection->count());
  206. $md5 = md5_file(__FILE__);
  207. $size = filesize(__FILE__);
  208. $record = $newCollection->findOne();
  209. $this->assertNotNull($record);
  210. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $record);
  211. $this->assertSame((string) $id, (string) $record->_id);
  212. $this->assertObjectHasAttribute('foo', $record);
  213. $this->assertAttributeSame('bar', 'foo', $record);
  214. $this->assertObjectHasAttribute('length', $record);
  215. $this->assertAttributeSame($size, 'length', $record);
  216. $this->assertObjectHasAttribute('chunkSize', $record);
  217. $this->assertAttributeSame(100, 'chunkSize', $record);
  218. $this->assertObjectHasAttribute('md5', $record);
  219. $this->assertAttributeSame($md5, 'md5', $record);
  220. $this->assertObjectHasAttribute('filename', $record);
  221. $this->assertAttributeSame('test.php', 'filename', $record);
  222. $numberOfChunks = (int)ceil($size / 100);
  223. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  224. }
  225. public function testFindOneReturnsFile()
  226. {
  227. $collection = $this->getGridFS();
  228. $this->prepareFile();
  229. $result = $collection->findOne();
  230. $this->assertInstanceOf('MongoGridFSFile', $result);
  231. }
  232. public function testPut()
  233. {
  234. $collection = $this->getGridFS();
  235. $id = $collection->put(__FILE__, ['chunkSize' => 100, 'foo' => 'bar']);
  236. $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
  237. $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
  238. $this->assertSame(1, $newCollection->count());
  239. $size = filesize(__FILE__);
  240. $numberOfChunks = (int)ceil($size / 100);
  241. $this->assertSame($numberOfChunks, $newChunksCollection->count());
  242. }
  243. /**
  244. * @var \MongoID
  245. */
  246. protected function prepareFile($data = 'abcd', $extra = [])
  247. {
  248. $collection = $this->getGridFS();
  249. // to make sure we have multiple chunks
  250. $extra += ['chunkSize' => 2];
  251. return $collection->storeBytes($data, $extra);
  252. }
  253. }