GridFSSpec.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. class GridFSSpec extends ObjectBehavior
  6. {
  7. /**
  8. * @param \MongoGridFS $gridFs
  9. */
  10. function let($gridFs)
  11. {
  12. $this->beConstructedWith($gridFs);
  13. }
  14. function it_is_adapter()
  15. {
  16. $this->shouldHaveType('Gaufrette\Adapter');
  17. }
  18. function it_is_checksum_calculator()
  19. {
  20. $this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
  21. }
  22. function it_supports_metadata()
  23. {
  24. $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
  25. }
  26. function it_supports_native_list_keys()
  27. {
  28. $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
  29. }
  30. /**
  31. * @param \MongoGridFS $gridFs
  32. * @param \MongoGridFSFile $file
  33. */
  34. function it_reads_file($gridFs, $file)
  35. {
  36. $file
  37. ->getBytes()
  38. ->willReturn('some content')
  39. ;
  40. $gridFs
  41. ->findOne('filename', array())
  42. ->shouldBeCalled()
  43. ->willReturn($file)
  44. ;
  45. $this->read('filename')->shouldReturn('some content');
  46. }
  47. /**
  48. * @param \MongoGridFS $gridFs
  49. */
  50. function it_does_not_fail_when_cannot_read($gridFs)
  51. {
  52. $gridFs
  53. ->findOne('filename', array())
  54. ->shouldBeCalled()
  55. ->willReturn(null)
  56. ;
  57. $this->read('filename')->shouldReturn(false);
  58. }
  59. /**
  60. * @param \MongoGridFS $gridFs
  61. * @param \MongoGridFSFile $file
  62. */
  63. function it_checks_if_file_exists($gridFs, $file)
  64. {
  65. $gridFs
  66. ->findOne('filename', array())
  67. ->willReturn($file)
  68. ;
  69. $gridFs
  70. ->findOne('filename2', array())
  71. ->willReturn(null)
  72. ;
  73. $this->exists('filename')->shouldReturn(true);
  74. $this->exists('filename2')->shouldReturn(false);
  75. }
  76. /**
  77. * @param \MongoGridFS $gridFs
  78. */
  79. function it_deletes_file($gridFs)
  80. {
  81. $file = new \stdClass;
  82. $file->file = array('_id' => 123);
  83. $gridFs
  84. ->findOne('filename', array('_id'))
  85. ->willReturn($file)
  86. ;
  87. $gridFs
  88. ->delete(123)
  89. ->willReturn(true)
  90. ;
  91. $this->delete('filename')->shouldReturn(true);
  92. }
  93. /**
  94. * @param \MongoGridFS $gridFs
  95. */
  96. function it_does_not_delete_file($gridFs)
  97. {
  98. $file = new \stdClass;
  99. $file->file = array('_id' => 123);
  100. $gridFs
  101. ->findOne('filename', array('_id'))
  102. ->willReturn($file)
  103. ;
  104. $gridFs
  105. ->delete(123)
  106. ->willReturn(false)
  107. ;
  108. $this->delete('filename')->shouldReturn(false);
  109. $gridFs
  110. ->findOne('filename', array('_id'))
  111. ->willReturn(null)
  112. ;
  113. $this->delete('filename')->shouldReturn(false);
  114. }
  115. /**
  116. * @param \MongoGridFS $gridFs
  117. * @param \MongoGridFSFile $file
  118. */
  119. function it_writes_file($gridFs, $file)
  120. {
  121. $file
  122. ->getSize()
  123. ->willReturn(12)
  124. ;
  125. $gridFs
  126. ->findOne('filename', array())
  127. ->willReturn(null)
  128. ;
  129. $gridFs
  130. ->storeBytes('some content', array('date' => 1234, 'someother' => 'metadata', 'filename' => 'filename'))
  131. ->willReturn('someId')
  132. ;
  133. $gridFs
  134. ->findOne(array('_id' => 'someId'))
  135. ->willReturn($file)
  136. ;
  137. $this->setMetadata('filename', array('date' => 1234, 'someother' => 'metadata'));
  138. $this
  139. ->write('filename', 'some content')
  140. ->shouldReturn(12)
  141. ;
  142. }
  143. /**
  144. * @param \MongoGridFS $gridFs
  145. * @param \MongoGridFSFile $file
  146. */
  147. function it_updates_file($gridFs, $file)
  148. {
  149. $someFile = new \stdClass;
  150. $someFile->file = array('_id' => 123);
  151. $gridFs
  152. ->findOne('filename', array('_id'))
  153. ->shouldBeCalled()
  154. ->willReturn($someFile)
  155. ;
  156. $gridFs
  157. ->delete(123)
  158. ->shouldBeCalled()
  159. ->willReturn(true)
  160. ;
  161. $file
  162. ->getSize()
  163. ->willReturn(12)
  164. ;
  165. $gridFs
  166. ->findOne('filename', array())
  167. ->willReturn($file)
  168. ;
  169. $gridFs
  170. ->storeBytes(Argument::any(), Argument::any())
  171. ->willReturn('someId')
  172. ;
  173. $gridFs
  174. ->findOne(array('_id' => 'someId'))
  175. ->willReturn($file)
  176. ;
  177. $this
  178. ->write('filename', 'some content')
  179. ->shouldReturn(12)
  180. ;
  181. }
  182. /**
  183. * @param \MongoGridFS $gridFs
  184. * @param \MongoGridFSFile $file
  185. */
  186. function it_renames_file($gridFs, $file)
  187. {
  188. $file
  189. ->getBytes()
  190. ->willReturn('some content')
  191. ;
  192. $file
  193. ->getSize()
  194. ->willReturn(12)
  195. ;
  196. $gridFs
  197. ->findOne('otherFilename', array())
  198. ->willReturn(null)
  199. ;
  200. $gridFs
  201. ->findOne('filename', array())
  202. ->shouldBeCalled()
  203. ->willReturn($file)
  204. ;
  205. $gridFs
  206. ->storeBytes('some content', array('date' => 1234, 'filename' => 'otherFilename'))
  207. ->shouldBeCalled()
  208. ->willReturn('someId')
  209. ;
  210. $fileToDelete = new \stdClass;
  211. $fileToDelete->file = array('_id' => 123);
  212. $gridFs
  213. ->findOne('filename', array('_id'))
  214. ->willReturn($fileToDelete)
  215. ;
  216. $gridFs
  217. ->findOne(array('_id' => 'someId'))
  218. ->willReturn($file)
  219. ;
  220. $gridFs
  221. ->delete(123)
  222. ->shouldBeCalled()
  223. ->willReturn(true)
  224. ;
  225. $this->setMetadata('otherFilename', array('date' => 1234));
  226. $this->rename('filename', 'otherFilename')->shouldReturn(true);
  227. }
  228. /**
  229. * @param \MongoGridFS $gridFs
  230. */
  231. function it_fetches_keys($gridFs, $file, $otherFile)
  232. {
  233. $gridFs
  234. ->find(array(), array('filename'))
  235. ->willReturn(array(new TestFile('filename'), new TestFile('otherFilename')))
  236. ;
  237. $this->keys()->shouldReturn(array('filename', 'otherFilename'));
  238. }
  239. /**
  240. * @param \MongoGridFS $gridFs
  241. */
  242. function it_fetches_mtime($gridFs)
  243. {
  244. $time = new \stdClass;
  245. $time->sec = 12345;
  246. $someFile = new \stdClass;
  247. $someFile->file = array('date' => $time);
  248. $gridFs
  249. ->findOne('filename', array('date'))
  250. ->willReturn($someFile)
  251. ;
  252. $this->mtime('filename')->shouldReturn(12345);
  253. }
  254. /**
  255. * @param \MongoGridFS $gridFs
  256. */
  257. function it_calculates_checksum($gridFs)
  258. {
  259. $someFile = new \stdClass;
  260. $someFile->file = array('md5' => 'md5123');
  261. $gridFs
  262. ->findOne('filename', array('md5'))
  263. ->willReturn($someFile)
  264. ;
  265. $this->checksum('filename')->shouldReturn('md5123');
  266. }
  267. }
  268. class TestFile
  269. {
  270. private $filename;
  271. public function __construct($filename)
  272. {
  273. $this->filename = $filename;
  274. }
  275. public function getFilename()
  276. {
  277. return $this->filename;
  278. }
  279. }