FtpSpec.php 6.0 KB

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