StreamWrapperSpec.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace spec\Gaufrette;
  3. use Gaufrette\FilesystemMap;
  4. use Gaufrette\Filesystem;
  5. use Gaufrette\Stream;
  6. use PhpSpec\ObjectBehavior;
  7. use Prophecy\Argument;
  8. class StreamWrapperSpec extends ObjectBehavior
  9. {
  10. /**
  11. * @param \Gaufrette\FilesystemMap $map
  12. * @param \Gaufrette\Filesystem $filesystem
  13. * @param \Gaufrette\Stream $stream
  14. */
  15. function let(FilesystemMap $map, Filesystem $filesystem, Stream $stream)
  16. {
  17. $filesystem->createStream('filename')->willReturn($stream);
  18. $map->get('some')->willReturn($filesystem);
  19. $this->setFilesystemMap($map);
  20. }
  21. function it_is_initializable()
  22. {
  23. $this->shouldHaveType('Gaufrette\StreamWrapper');
  24. }
  25. /**
  26. * @param \Gaufrette\Stream $stream
  27. */
  28. function it_opens_stream(Stream $stream)
  29. {
  30. $stream->open(Argument::any())->willReturn(true);
  31. $this->stream_open('gaufrette://some/filename', 'r+')->shouldReturn(true);
  32. }
  33. function it_does_not_open_stream_when_key_is_not_defined()
  34. {
  35. $this
  36. ->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette://some) is invalid.'))
  37. ->duringStream_open('gaufrette://some', 'r+');
  38. }
  39. function it_does_not_open_stream_when_host_is_not_defined()
  40. {
  41. $this
  42. ->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette:///somefile) is invalid.'))
  43. ->duringStream_open('gaufrette:///somefile', 'r+')
  44. ;
  45. }
  46. function it_does_not_read_from_stream_when_is_not_opened()
  47. {
  48. $this->stream_read(10)->shouldReturn(false);
  49. }
  50. /**
  51. * @param \Gaufrette\Stream $stream
  52. */
  53. function it_does_not_read_from_stream(Stream $stream)
  54. {
  55. $stream->open(Argument::any())->willReturn(true);
  56. $stream->read(4)->willReturn('some');
  57. $this->stream_open('gaufrette://some/filename', 'r+');
  58. $this->stream_read(4)->shouldReturn('some');
  59. }
  60. function it_does_not_write_to_stream_when_is_not_opened()
  61. {
  62. $this->stream_write('some content')->shouldReturn(0);
  63. }
  64. /**
  65. * @param \Gaufrette\Stream $stream
  66. */
  67. function it_writes_to_stream(Stream $stream)
  68. {
  69. $stream->open(Argument::any())->willReturn(true);
  70. $stream->write('some content')->shouldBeCalled()->willReturn(12);
  71. $this->stream_open('gaufrette://some/filename', 'w+');
  72. $this->stream_write('some content')->shouldReturn(12);
  73. }
  74. /**
  75. * @param \Gaufrette\Stream $stream
  76. */
  77. function it_does_not_close_stream_when_is_not_opened($stream)
  78. {
  79. $stream->close()->shouldNotBeCalled();
  80. $this->stream_close();
  81. }
  82. /**
  83. * @param \Gaufrette\Stream $stream
  84. */
  85. function it_closes_stream(Stream $stream)
  86. {
  87. $stream->open(Argument::any())->willReturn(true);
  88. $stream->close()->shouldBeCalled();
  89. $this->stream_open('gaufrette://some/filename', 'w+');
  90. $this->stream_close();
  91. }
  92. /**
  93. * @param \Gaufrette\Stream $stream
  94. */
  95. function it_does_not_flush_stream_when_is_not_opened(Stream $stream)
  96. {
  97. $stream->flush()->shouldNotBeCalled();
  98. $this->stream_flush();
  99. }
  100. /**
  101. * @param \Gaufrette\Stream $stream
  102. */
  103. function it_flushes_stream(Stream $stream)
  104. {
  105. $stream->open(Argument::any())->willReturn(true);
  106. $stream->flush()->shouldBeCalled();
  107. $this->stream_open('gaufrette://some/filename', 'w+');
  108. $this->stream_flush();
  109. }
  110. /**
  111. * @param \Gaufrette\Stream $stream
  112. */
  113. function it_does_not_seek_in_stream_when_is_not_opened(Stream $stream)
  114. {
  115. $stream->seek(12, SEEK_SET)->shouldNotBeCalled();
  116. $this->stream_seek(12, SEEK_SET);
  117. }
  118. /**
  119. * @param \Gaufrette\Stream $stream
  120. */
  121. function it_seeks_in_stream(Stream $stream)
  122. {
  123. $stream->open(Argument::any())->willReturn(true);
  124. $stream->seek(12, SEEK_SET)->shouldBeCalled()->willReturn(true);
  125. $this->stream_open('gaufrette://some/filename', 'w+');
  126. $this->stream_seek(12, SEEK_SET)->shouldReturn(true);
  127. }
  128. /**
  129. * @param \Gaufrette\Stream $stream
  130. */
  131. function it_does_not_tell_about_position_in_stream_when_is_not_opened(Stream $stream)
  132. {
  133. $stream->tell()->shouldNotBeCalled();
  134. $this->stream_tell();
  135. }
  136. /**
  137. * @param \Gaufrette\Stream $stream
  138. */
  139. function it_does_tell_about_position_in_stream(Stream $stream)
  140. {
  141. $stream->open(Argument::any())->willReturn(true);
  142. $stream->tell()->shouldBeCalled()->willReturn(12);
  143. $this->stream_open('gaufrette://some/filename', 'w+');
  144. $this->stream_tell()->shouldReturn(12);
  145. }
  146. /**
  147. * @param \Gaufrette\Stream $stream
  148. */
  149. function it_does_not_mark_as_eof_if_stream_is_not_opened(Stream $stream)
  150. {
  151. $stream->eof()->shouldNotBeCalled();
  152. $this->stream_eof();
  153. }
  154. /**
  155. * @param \Gaufrette\Stream $stream
  156. */
  157. function it_checks_if_eof(Stream $stream)
  158. {
  159. $stream->open(Argument::any())->willReturn(true);
  160. $this->stream_open('gaufrette://some/filename', 'w+');
  161. $stream->eof()->willReturn(false);
  162. $this->stream_eof()->shouldReturn(false);
  163. $stream->eof()->willReturn(true);
  164. $this->stream_eof()->shouldReturn(true);
  165. }
  166. function it_does_not_get_stat_when_is_not_open()
  167. {
  168. $this->stream_stat()->shouldReturn(false);
  169. }
  170. /**
  171. * @param \Gaufrette\Stream $stream
  172. */
  173. function it_stats_file(Stream $stream)
  174. {
  175. $stat = array(
  176. 'dev' => 1,
  177. 'ino' => 12,
  178. 'mode' => 0777,
  179. 'nlink' => 0,
  180. 'uid' => 123,
  181. 'gid' => 1,
  182. 'rdev' => 0,
  183. 'size' => 666,
  184. 'atime' => 1348030800,
  185. 'mtime' => 1348030800,
  186. 'ctime' => 1348030800,
  187. 'blksize' => 5,
  188. 'blocks' => 1,
  189. );
  190. $stream->open(Argument::any())->willReturn(true);
  191. $stream->stat()->willReturn($stat);
  192. $this->stream_open('gaufrette://some/filename', 'w+');
  193. $this->stream_stat()->shouldReturn($stat);
  194. }
  195. /**
  196. * @param \Gaufrette\Stream $stream
  197. */
  198. function it_should_stat_from_url(Stream $stream)
  199. {
  200. $stat = array(
  201. 'dev' => 1,
  202. 'ino' => 12,
  203. 'mode' => 0777,
  204. 'nlink' => 0,
  205. 'uid' => 123,
  206. 'gid' => 1,
  207. 'rdev' => 0,
  208. 'size' => 666,
  209. 'atime' => 1348030800,
  210. 'mtime' => 1348030800,
  211. 'ctime' => 1348030800,
  212. 'blksize' => 5,
  213. 'blocks' => 1,
  214. );
  215. $stream->open(Argument::any())->willReturn(true);
  216. $stream->stat()->willReturn($stat);
  217. $this->url_stat('gaufrette://some/filename', STREAM_URL_STAT_LINK)->shouldReturn($stat);
  218. }
  219. /**
  220. * @param \Gaufrette\Filesystem $stream
  221. * @param \Gaufrette\Stream $stream
  222. */
  223. function it_stats_even_if_it_cannot_be_open(Filesystem $filesystem, Stream $stream)
  224. {
  225. $filesystem->createStream('dir/')->willReturn($stream);
  226. $stream->open(Argument::any())->willThrow(new \RuntimeException);
  227. $stream->stat(Argument::any())->willReturn(array('mode' => 16893));
  228. $this->url_stat('gaufrette://some/dir/', STREAM_URL_STAT_LINK)->shouldReturn(array('mode' => 16893));
  229. }
  230. /**
  231. * @param \Gaufrette\Stream $stream
  232. */
  233. function it_does_not_unlink_when_cannot_open(Stream $stream)
  234. {
  235. $stream->open(Argument::any())->willThrow(new \RuntimeException);
  236. $this->unlink('gaufrette://some/filename')->shouldReturn(false);
  237. }
  238. /**
  239. * @param \Gaufrette\Stream $stream
  240. */
  241. function it_unlinks_file(Stream $stream)
  242. {
  243. $stream->open(Argument::any())->willReturn(true);
  244. $stream->unlink()->willReturn(true);
  245. $this->unlink('gaufrette://some/filename')->shouldReturn(true);
  246. }
  247. function it_does_not_cast_stream_if_is_not_opened()
  248. {
  249. $this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn(false);
  250. }
  251. /**
  252. * @param \Gaufrette\Stream $stream
  253. */
  254. function it_casts_stream(Stream $stream)
  255. {
  256. $stream->open(Argument::any())->willReturn(true);
  257. $stream->cast(STREAM_CAST_FOR_SELECT)->willReturn('resource');
  258. $this->stream_open('gaufrette://some/filename', 'w+');
  259. $this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn('resource');
  260. }
  261. }