FtpSpec.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. //hack - mock php built-in functions
  4. require_once 'functions.php';
  5. use Gaufrette\Filesystem;
  6. use PhpSpec\ObjectBehavior;
  7. class FtpSpec extends ObjectBehavior
  8. {
  9. function let()
  10. {
  11. $this->beConstructedWith('/home/l3l0', 'localhost');
  12. }
  13. function it_is_adapter()
  14. {
  15. $this->shouldHaveType('Gaufrette\Adapter');
  16. }
  17. function it_supports_native_list_keys()
  18. {
  19. $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
  20. }
  21. function it_checks_if_file_exists_for_absolute_path()
  22. {
  23. $this->exists('filename')->shouldReturn(true);
  24. $this->exists('aa/filename')->shouldReturn(false);
  25. }
  26. function it_checks_if_file_exists_for_relative_path()
  27. {
  28. $this->beConstructedWith('/home/l3l0/relative', 'localhost');
  29. $this->exists('filename')->shouldReturn(true);
  30. $this->exists('filename2')->shouldReturn(false);
  31. $this->exists('aa/filename')->shouldReturn(false);
  32. $this->exists('some/otherfilename')->shouldReturn(true);
  33. }
  34. function it_checks_if_dir_exists_for_symlink()
  35. {
  36. $this->exists('www')->shouldReturn(true);
  37. $this->exists('vendor')->shouldReturn(true);
  38. $this->exists('bbb')->shouldReturn(false);
  39. }
  40. function it_checks_if_dir_exists_with_special_and_unicode_chars_in_name()
  41. {
  42. $this->beConstructedWith('/home/l3l2', 'localhost');
  43. $this->exists('a b c d -> žežulička')->shouldReturn(true);
  44. }
  45. function it_reads_file()
  46. {
  47. $this->read('filename')->shouldReturn('some content');
  48. }
  49. function it_does_not_read_file()
  50. {
  51. $this->read('filename2')->shouldReturn(false);
  52. }
  53. function it_writes_file()
  54. {
  55. $this->write('filename', 'some content')->shouldReturn(12);
  56. }
  57. function it_does_not_write_file()
  58. {
  59. $this->write('filename2', 'some content')->shouldReturn(false);
  60. }
  61. function it_renames_file()
  62. {
  63. $this->rename('filename', 'filename2')->shouldReturn(true);
  64. }
  65. function it_does_not_not_rename_file_when_target_file_is_invalid()
  66. {
  67. $this->rename('filename', 'invalid')->shouldReturn(false);
  68. }
  69. function it_fetches_keys_without_directories_dots()
  70. {
  71. $this->keys()->shouldReturn(array('filename', 'filename.exe', '.htaccess', 'aaa', 'aaa/filename'));
  72. }
  73. function it_fetches_keys_with_spaces_and_unicode_chars()
  74. {
  75. $this->beConstructedWith('/home/l3l2', 'localhost');
  76. $this->keys()->shouldReturn(array('Žľuťoučký kůň.pdf', 'a b c d -> žežulička', 'a b c d -> žežulička/do re mi.pdf'));
  77. }
  78. function it_fetches_keys_recursive()
  79. {
  80. $this->beConstructedWith('/home/l3l3', 'localhost');
  81. $this->keys()->shouldReturn(array('filename', 'filename.exe', '.htaccess', 'aaa', 'www', 'aaa/filename', 'www/filename', 'www/some', 'www/some/otherfilename'));
  82. }
  83. function it_lists_keys()
  84. {
  85. $this->listKeys()->shouldReturn(array(
  86. 'keys' => array('filename', 'filename.exe', '.htaccess', 'aaa/filename'),
  87. 'dirs' => array('aaa')
  88. ));
  89. $this->listKeys('file')->shouldReturn(array(
  90. 'keys' => array('filename', 'filename.exe'),
  91. 'dirs' => array()
  92. ));
  93. $this->listKeys('name')->shouldReturn(array(
  94. 'keys' => array(),
  95. 'dirs' => array()
  96. ));
  97. $this->listKeys('aaa')->shouldReturn(array(
  98. 'keys' => array('aaa/filename'),
  99. 'dirs' => array('aaa')
  100. ));
  101. $this->listKeys('aaa/')->shouldReturn(array(
  102. 'keys' => array('aaa/filename'),
  103. 'dirs' => array()
  104. ));
  105. }
  106. function it_fetches_mtime()
  107. {
  108. $this->mtime('filename')->shouldReturn(strtotime('2010-10-10 23:10:10'));
  109. }
  110. function it_throws_exception_when_mtime_is_not_supported_by_server()
  111. {
  112. $this->shouldThrow(new \RuntimeException('Server does not support ftp_mdtm function.'))->duringMtime('invalid');
  113. }
  114. function it_deletes_file()
  115. {
  116. $this->delete('filename')->shouldReturn(true);
  117. }
  118. function it_does_not_delete_file()
  119. {
  120. $this->delete('invalid')->shouldReturn(false);
  121. }
  122. /**
  123. * @param \Gaufrette\Filesystem $filesystem
  124. */
  125. function it_creates_file(Filesystem $filesystem)
  126. {
  127. $this->createFile('filename', $filesystem)->shouldReturnAnInstanceOf('\Gaufrette\File');
  128. }
  129. /**
  130. * @param \Gaufrette\Filesystem $filesystem
  131. */
  132. function it_creates_file_in_not_existing_directory(Filesystem $filesystem)
  133. {
  134. $this->createFile('bb/cc/dd/filename', $filesystem)->shouldReturnAnInstanceOf('\Gaufrette\File');
  135. }
  136. function it_checks_if_given_key_is_directory()
  137. {
  138. $this->isDirectory('aaa')->shouldReturn(true);
  139. $this->isDirectory('filename')->shouldReturn(false);
  140. }
  141. function it_fetches_keys_with_hidden_files()
  142. {
  143. $this->beConstructedWith('/home/l3l1', 'localhost');
  144. $this->keys()->shouldReturn(array('filename', '.htaccess'));
  145. }
  146. function it_checks_if_hidden_file_exists()
  147. {
  148. $this->beConstructedWith('/home/l3l1', 'localhost');
  149. $this->exists('.htaccess')->shouldReturn(true);
  150. }
  151. function it_creates_base_directory_without_warning()
  152. {
  153. global $createdDirectory;
  154. $createdDirectory = '';
  155. $this->beConstructedWith('/home/l3l0/new', 'localhost', array('create' => true));
  156. $this->listDirectory()->shouldReturn(array('keys' => array(), 'dirs' => array()));
  157. }
  158. function it_does_not_create_base_directory_and_should_throw_exception()
  159. {
  160. global $createdDirectory;
  161. $createdDirectory = '';
  162. $this->beConstructedWith('/home/l3l0/new', 'localhost', array('create' => false));
  163. $this->shouldThrow(new \RuntimeException("The directory '/home/l3l0/new' does not exist."))->during('listDirectory', array());
  164. }
  165. function it_fetches_keys_for_windows()
  166. {
  167. $this->beConstructedWith('C:\Ftp', 'localhost');
  168. $this->keys()->shouldReturn(array('archive', 'file1.zip', 'file2.zip'));
  169. }
  170. function it_supports_sizecalculator()
  171. {
  172. $this->shouldImplement('Gaufrette\Adapter\SizeCalculator');
  173. $this->size('/path')->shouldReturn(5000);
  174. }
  175. function it_throws_an_exception_when_size_cant_be_fetched()
  176. {
  177. $this->shouldThrow('RuntimeException')->during('size', ['/erroneous']);
  178. }
  179. }