StreamWrapperSpec.php 8.0 KB

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