FilesystemSpec.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace spec\Gaufrette;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. class FilesystemSpec extends ObjectBehavior
  6. {
  7. /**
  8. * @param \Gaufrette\Adapter $adapter
  9. */
  10. function let($adapter)
  11. {
  12. $this->beConstructedWith($adapter);
  13. }
  14. function it_is_initializable()
  15. {
  16. $this->shouldBeAnInstanceOf('Gaufrette\Filesystem');
  17. }
  18. /**
  19. * @param \Gaufrette\Adapter $adapter
  20. */
  21. function it_gives_access_to_adapter($adapter)
  22. {
  23. $this->getAdapter()->shouldBe($adapter);
  24. }
  25. /**
  26. * @param \Gaufrette\Adapter $adapter
  27. */
  28. function it_check_if_file_exists_using_adapter($adapter)
  29. {
  30. $adapter->exists('filename')->willReturn(true);
  31. $adapter->exists('otherFilename')->willReturn(false);
  32. $this->has('filename')->shouldReturn(true);
  33. $this->has('otherFilename')->shouldReturn(false);
  34. }
  35. /**
  36. * @param \Gaufrette\Adapter $adapter
  37. */
  38. function it_renames_file($adapter)
  39. {
  40. $adapter->exists('filename')->shouldBeCalled()->willReturn(true);
  41. $adapter->exists('otherFilename')->shouldBeCalled()->willReturn(false);
  42. $adapter->rename('filename', 'otherFilename')->shouldBeCalled()->willReturn(true);
  43. $this->rename('filename', 'otherFilename')->shouldReturn(true);
  44. }
  45. /**
  46. * @param \Gaufrette\Adapter $adapter
  47. */
  48. function it_fails_when_renamed_source_file_does_not_exist($adapter)
  49. {
  50. $adapter->exists('filename')->willReturn(false);
  51. $this
  52. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  53. ->duringRename('filename', 'otherFilename')
  54. ;
  55. }
  56. /**
  57. * @param \Gaufrette\Adapter $adapter
  58. */
  59. function it_fails_when_renamed_target_file_exists($adapter)
  60. {
  61. $adapter->exists('filename')->willReturn(true);
  62. $adapter->exists('otherFilename')->willReturn(true);
  63. $this
  64. ->shouldThrow(new \Gaufrette\Exception\UnexpectedFile('otherFilename'))
  65. ->duringRename('filename', 'otherFilename')
  66. ;
  67. }
  68. /**
  69. * @param \Gaufrette\Adapter $adapter
  70. */
  71. function it_fails_when_rename_is_not_successful($adapter)
  72. {
  73. $adapter->exists('filename')->willReturn(true);
  74. $adapter->exists('otherFilename')->willReturn(false);
  75. $adapter->rename('filename', 'otherFilename')->willReturn(false);
  76. $this
  77. ->shouldThrow(new \RuntimeException('Could not rename the "filename" key to "otherFilename".'))
  78. ->duringRename('filename', 'otherFilename')
  79. ;
  80. }
  81. /**
  82. * @param \Gaufrette\Adapter $adapter
  83. */
  84. function it_creates_file_object_for_key($adapter)
  85. {
  86. $adapter->exists('filename')->willReturn(true);
  87. $this->get('filename')->shouldBeAnInstanceOf('Gaufrette\File');
  88. }
  89. /**
  90. * @param \Gaufrette\Adapter $adapter
  91. */
  92. function it_does_not_get_file_object_when_file_with_key_does_not_exist($adapter)
  93. {
  94. $adapter->exists('filename')->willReturn(false);
  95. $this
  96. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  97. ->duringGet('filename')
  98. ;
  99. }
  100. /**
  101. * @param \Gaufrette\Adapter $adapter
  102. */
  103. function it_gets_file_object_when_file_does_not_exist_but_can_be_created($adapter)
  104. {
  105. $adapter->exists('filename')->willReturn(false);
  106. $this->get('filename', true)->shouldBeAnInstanceOf('Gaufrette\File');
  107. }
  108. /**
  109. * @param \spec\Gaufrette\Adapter $extendedAdapter
  110. * @param \Gaufrette\File $file
  111. */
  112. function it_delegates_file_creation_to_adapter_when_adapter_is_file_factory($extendedAdapter, $file)
  113. {
  114. $this->beConstructedWith($extendedAdapter);
  115. $extendedAdapter->exists('filename')->willReturn(true);
  116. $extendedAdapter->createFile('filename', $this)->willReturn($file);
  117. $this->get('filename')->shouldBe($file);
  118. }
  119. /**
  120. * @param \Gaufrette\Adapter $adapter
  121. */
  122. function it_writes_content_to_new_file($adapter)
  123. {
  124. $adapter->exists('filename')->shouldBeCalled()->willReturn(false);
  125. $adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
  126. $this->write('filename', 'some content to write')->shouldReturn(21);
  127. }
  128. /**
  129. * @param \Gaufrette\Adapter $adapter
  130. */
  131. function it_updates_content_of_file($adapter)
  132. {
  133. $adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
  134. $this->write('filename', 'some content to write', true)->shouldReturn(21);
  135. }
  136. /**
  137. * @param \Gaufrette\Adapter $adapter
  138. */
  139. function it_does_not_update_content_of_file_when_file_cannot_be_overwriten($adapter)
  140. {
  141. $adapter->exists('filename')->willReturn(true);
  142. $adapter->write('filename', 'some content to write')->shouldNotBeCalled();
  143. $this
  144. ->shouldThrow(new \Gaufrette\Exception\FileAlreadyExists('filename'))
  145. ->duringWrite('filename', 'some content to write')
  146. ;
  147. }
  148. /**
  149. * @param \Gaufrette\Adapter $adapter
  150. */
  151. function it_fails_when_write_is_not_successful($adapter)
  152. {
  153. $adapter->exists('filename')->willReturn(false);
  154. $adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(false);
  155. $this
  156. ->shouldThrow(new \RuntimeException('Could not write the "filename" key content.'))
  157. ->duringWrite('filename', 'some content to write')
  158. ;
  159. }
  160. /**
  161. * @param \Gaufrette\Adapter $adapter
  162. */
  163. function it_read_file($adapter)
  164. {
  165. $adapter->exists('filename')->shouldBeCalled()->willReturn(true);
  166. $adapter->read('filename')->shouldBeCalled()->willReturn('Some content');
  167. $this->read('filename')->shouldReturn('Some content');
  168. }
  169. /**
  170. * @param \Gaufrette\Adapter $adapter
  171. */
  172. function it_does_not_read_file_which_does_not_exist($adapter)
  173. {
  174. $adapter->exists('filename')->willReturn(false);
  175. $this
  176. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  177. ->duringRead('filename');
  178. }
  179. /**
  180. * @param \Gaufrette\Adapter $adapter
  181. */
  182. function it_fails_when_read_is_not_successful($adapter)
  183. {
  184. $adapter->exists('filename')->willReturn(true);
  185. $adapter->read('filename')->willReturn(false);
  186. $this
  187. ->shouldThrow(new \RuntimeException('Could not read the "filename" key content.'))
  188. ->duringRead('filename')
  189. ;
  190. }
  191. /**
  192. * @param \Gaufrette\Adapter $adapter
  193. */
  194. function it_deletes_file($adapter)
  195. {
  196. $adapter->exists('filename')->shouldBeCalled()->willReturn(true);
  197. $adapter->delete('filename')->shouldBeCalled()->willReturn(true);
  198. $this->delete('filename')->shouldReturn(true);
  199. }
  200. /**
  201. * @param \Gaufrette\Adapter $adapter
  202. */
  203. function it_does_not_delete_file_which_does_not_exist($adapter)
  204. {
  205. $adapter->exists('filename')->willReturn(false);
  206. $this
  207. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  208. ->duringDelete('filename')
  209. ;
  210. }
  211. /**
  212. * @param \Gaufrette\Adapter $adapter
  213. */
  214. function it_fails_when_delete_is_not_successful($adapter)
  215. {
  216. $adapter->exists('filename')->willReturn(true);
  217. $adapter->delete('filename')->willReturn(false);
  218. $this
  219. ->shouldThrow(new \RuntimeException('Could not remove the "filename" key.'))
  220. ->duringDelete('filename')
  221. ;
  222. }
  223. /**
  224. * @param \Gaufrette\Adapter $adapter
  225. */
  226. function it_should_get_all_keys($adapter)
  227. {
  228. $keys = array('filename', 'filename1', 'filename2');
  229. $adapter->keys()->willReturn($keys);
  230. $this->keys()->shouldReturn($keys);
  231. }
  232. /**
  233. * @param \Gaufrette\Adapter $adapter
  234. */
  235. function it_match_listed_keys_using_specified_pattern($adapter)
  236. {
  237. $keys = array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey');
  238. $adapter->keys()->willReturn($keys);
  239. $adapter->isDirectory(Argument::any())->willReturn(false);
  240. $this->listKeys()->shouldReturn(
  241. array(
  242. 'keys' => array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'),
  243. 'dirs' => array()
  244. )
  245. );
  246. $this->listKeys('filename')->shouldReturn(
  247. array(
  248. 'keys' => array('filename', 'filename1', 'filename2'),
  249. 'dirs' => array()
  250. )
  251. );
  252. $this->listKeys('Key')->shouldReturn(
  253. array(
  254. 'keys' => array('KeyTest'),
  255. 'dirs' => array()
  256. )
  257. );
  258. }
  259. /**
  260. * @param \Gaufrette\Adapter $adapter
  261. */
  262. function it_listing_directories_using_adapter_is_directory_method($adapter)
  263. {
  264. $keys = array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey');
  265. $adapter->keys()->willReturn($keys);
  266. $adapter->isDirectory('filename')->willReturn(false);
  267. $adapter->isDirectory('filename2')->willReturn(false);
  268. $adapter->isDirectory('KeyTest')->willReturn(false);
  269. $adapter->isDirectory('testkey')->willReturn(false);
  270. $adapter->isDirectory('filename1')->willReturn(true);
  271. $adapter->isDirectory('testKey')->willReturn(true);
  272. $this->listKeys()->shouldReturn(
  273. array(
  274. 'keys' => array('filename', 'filename2', 'KeyTest', 'testkey'),
  275. 'dirs' => array('filename1', 'testKey')
  276. )
  277. );
  278. $this->listKeys('filename')->shouldReturn(
  279. array(
  280. 'keys' => array('filename', 'filename2'),
  281. 'dirs' => array('filename1')
  282. )
  283. );
  284. $this->listKeys('Key')->shouldReturn(
  285. array(
  286. 'keys' => array('KeyTest'),
  287. 'dirs' => array()
  288. )
  289. );
  290. }
  291. /**
  292. * @param \Gaufrette\Adapter $adapter
  293. */
  294. function it_gets_mtime_of_file_using_adapter($adapter)
  295. {
  296. $adapter->exists('filename')->willReturn(true);
  297. $adapter->mtime('filename')->willReturn(1234567);
  298. $this->mtime('filename')->shouldReturn(1234567);
  299. }
  300. /**
  301. * @param \Gaufrette\Adapter $adapter
  302. */
  303. function it_does_not_get_mtime_of_file_which_does_not_exist($adapter)
  304. {
  305. $adapter->exists('filename')->willReturn(false);
  306. $this
  307. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  308. ->duringMtime('filename')
  309. ;
  310. }
  311. /**
  312. * @param \Gaufrette\Adapter $adapter
  313. */
  314. function it_calculates_file_checksum($adapter)
  315. {
  316. $adapter->exists('filename')->shouldBeCalled()->willReturn(true);
  317. $adapter->read('filename')->willReturn('some content');
  318. $this->checksum('filename')->shouldReturn(md5('some content'));
  319. }
  320. /**
  321. * @param \Gaufrette\Adapter $adapter
  322. */
  323. function it_does_not_calculate_checksum_of_file_which_does_not_exist($adapter)
  324. {
  325. $adapter->exists('filename')->shouldBeCalled()->willReturn(false);
  326. $this
  327. ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
  328. ->duringChecksum('filename');
  329. }
  330. /**
  331. * @param \spec\Gaufrette\Adapter $extendedAdapter
  332. */
  333. function it_delegates_checksum_calculation_to_adapter_when_adapter_is_checksum_calculator($extendedAdapter)
  334. {
  335. $this->beConstructedWith($extendedAdapter);
  336. $extendedAdapter->exists('filename')->shouldBeCalled()->willReturn(true);
  337. $extendedAdapter->read('filename')->shouldNotBeCalled();
  338. $extendedAdapter->checksum('filename')->shouldBeCalled()->willReturn(12);
  339. $this->checksum('filename')->shouldReturn(12);
  340. }
  341. /**
  342. * @param \spec\Gaufrette\Adapter $extendedAdapter
  343. */
  344. function it_delegates_mime_type_resolution_to_adapter_when_adapter_is_mime_type_provider($extendedAdapter)
  345. {
  346. $this->beConstructedWith($extendedAdapter);
  347. $extendedAdapter->exists('filename')->willReturn(true);
  348. $extendedAdapter->mimeType('filename')->willReturn('text/plain');
  349. $this->mimeType('filename')->shouldReturn('text/plain');
  350. }
  351. function it_cannot_resolve_mime_type_if_the_adapter_cannot_provide_it($adapter)
  352. {
  353. $adapter->exists('filename')->willReturn(true);
  354. $this
  355. ->shouldThrow(new \LogicException(sprintf('Adapter "%s" cannot provide MIME type', get_class($adapter->getWrappedObject()))))
  356. ->duringMimeType('filename');
  357. }
  358. }
  359. interface Adapter extends \Gaufrette\Adapter,
  360. \Gaufrette\Adapter\FileFactory,
  361. \Gaufrette\Adapter\StreamFactory,
  362. \Gaufrette\Adapter\ChecksumCalculator,
  363. \Gaufrette\Adapter\MetadataSupporter,
  364. \Gaufrette\Adapter\MimeTypeProvider
  365. {}