MongoGridFSTest.php 13 KB

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