PhpseclibSftpSpec.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. if (!defined('NET_SFTP_TYPE_REGULAR')) {
  4. define('NET_SFTP_TYPE_REGULAR', 1);
  5. }
  6. if (!defined('NET_SFTP_TYPE_DIRECTORY')) {
  7. define('NET_SFTP_TYPE_DIRECTORY', 2);
  8. }
  9. use Gaufrette\Filesystem;
  10. use phpseclib\Net\SFTP as Base;
  11. use PhpSpec\ObjectBehavior;
  12. class SFTP extends Base
  13. {
  14. public function __construct()
  15. {
  16. }
  17. }
  18. class PhpseclibSftpSpec extends ObjectBehavior
  19. {
  20. /**
  21. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  22. */
  23. function let(SFTP $sftp)
  24. {
  25. $this->beConstructedWith($sftp, '/home/l3l0', false, 'l3lo', 'password');
  26. }
  27. function it_is_adapter()
  28. {
  29. $this->shouldHaveType('Gaufrette\Adapter');
  30. }
  31. function it_is_file_factory()
  32. {
  33. $this->shouldHaveType('Gaufrette\Adapter\FileFactory');
  34. }
  35. function it_supports_native_list_keys()
  36. {
  37. $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
  38. }
  39. /**
  40. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  41. */
  42. function it_fetches_keys(SFTP $sftp)
  43. {
  44. $sftp
  45. ->file_exists('/home/l3l0/')
  46. ->willReturn(true);
  47. $sftp
  48. ->rawlist('/home/l3l0/')
  49. ->willReturn(array(
  50. 'filename' => array('type' => NET_SFTP_TYPE_REGULAR),
  51. 'filename1' => array('type' => NET_SFTP_TYPE_REGULAR),
  52. 'aaa' => array('type' => NET_SFTP_TYPE_DIRECTORY)
  53. ));
  54. $sftp
  55. ->file_exists('/home/l3l0/aaa')
  56. ->willReturn(true);
  57. $sftp
  58. ->rawlist('/home/l3l0/aaa')
  59. ->willReturn(array(
  60. 'filename' => array('type' => NET_SFTP_TYPE_REGULAR),
  61. ));
  62. $this->keys()->shouldReturn(array('filename', 'filename1', 'aaa', 'aaa/filename'));
  63. }
  64. /**
  65. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  66. */
  67. function it_reads_file(SFTP $sftp)
  68. {
  69. $sftp->get('/home/l3l0/filename')->willReturn('some content');
  70. $this->read('filename')->shouldReturn('some content');
  71. }
  72. /**
  73. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  74. */
  75. function it_creates_and_writes_file(SFTP $sftp)
  76. {
  77. $sftp->pwd()->willReturn('/home/l3l0');
  78. $sftp->chdir('/home/l3l0')->willReturn(true);
  79. $sftp->put('/home/l3l0/filename', 'some content')->willReturn(true);
  80. $sftp->size('/home/l3l0/filename')->willReturn(12);
  81. $this->write('filename', 'some content')->shouldReturn(12);
  82. }
  83. /**
  84. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  85. */
  86. function it_renames_file(SFTP $sftp)
  87. {
  88. $sftp->pwd()->willReturn('/home/l3l0');
  89. $sftp->chdir('/home/l3l0')->willReturn(true);
  90. $sftp
  91. ->rename('/home/l3l0/filename', '/home/l3l0/filename1')
  92. ->willReturn(true)
  93. ;
  94. $this->rename('filename', 'filename1')->shouldReturn(true);
  95. }
  96. /**
  97. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  98. */
  99. function it_should_check_if_file_exists(SFTP $sftp)
  100. {
  101. $sftp->pwd()->willReturn('/home/l3l0');
  102. $sftp->chdir('/home/l3l0')->willReturn(true);
  103. $sftp->stat('/home/l3l0/filename')->willReturn(array(
  104. 'name' => '/home/l3l0/filename'
  105. ));
  106. $sftp->stat('/home/l3l0/filename1')->willReturn(false);
  107. $this->exists('filename')->shouldReturn(true);
  108. $this->exists('filename1')->shouldReturn(false);
  109. }
  110. /**
  111. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  112. */
  113. function it_should_check_is_directory(SFTP $sftp)
  114. {
  115. $sftp->pwd()->willReturn('/home/l3l0');
  116. $sftp->chdir('/home/l3l0')->willReturn(true);
  117. $sftp->chdir('/home/l3l0/aaa')->willReturn(true);
  118. $sftp->chdir('/home/l3l0/filename')->willReturn(false);
  119. $this->isDirectory('aaa')->shouldReturn(true);
  120. $this->isDirectory('filename')->shouldReturn(false);
  121. }
  122. /**
  123. * @param \spec\Gaufrette\Adapter\SFTP $sftp
  124. * @param \Gaufrette\Filesystem $filesystem
  125. */
  126. function it_should_create_file(SFTP $sftp, Filesystem $filesystem)
  127. {
  128. $sftp->stat('/home/l3l0/filename')->willReturn(array(
  129. 'name' => '/home/l3l0/filename',
  130. 'size' => '30',
  131. ));
  132. $this->createFile('filename', $filesystem)->beAnInstanceOf('Gaufrette\File');
  133. }
  134. }