FtpIntegrationTestCase.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. use League\Flysystem\AdapterInterface;
  3. use League\Flysystem\Filesystem;
  4. use PHPUnit\Framework\TestCase;
  5. abstract class FtpIntegrationTestCase extends TestCase
  6. {
  7. /**
  8. * @var AdapterInterface
  9. */
  10. protected static $adapter;
  11. /**
  12. * @var Filesystem
  13. */
  14. protected $filesystem;
  15. /**
  16. * @test
  17. */
  18. public function testInstantiable()
  19. {
  20. if ( ! defined('FTP_BINARY')) {
  21. $this->markTestSkipped('The FTP_BINARY constant is not defined');
  22. }
  23. $this->expectNotToPerformAssertions();
  24. }
  25. /**
  26. * @return AdapterInterface
  27. */
  28. abstract static protected function setup_adapter();
  29. /**
  30. * @beforeClass
  31. */
  32. public static function setupAdapter(): void
  33. {
  34. if ( ! defined('FTP_BINARY')) {
  35. return;
  36. }
  37. static::$adapter = static::setup_adapter();
  38. }
  39. /**
  40. * @before
  41. */
  42. public function setup_filesystem()
  43. {
  44. if ( ! defined('FTP_BINARY')) {
  45. return;
  46. }
  47. $this->filesystem = new Filesystem(static::$adapter, ['disable_asserts' => true]);
  48. foreach ($this->filesystem->listContents('/', false) as $item) {
  49. if ($item['path'] == '') {
  50. continue;
  51. }
  52. if ($item['type'] == 'dir') {
  53. $this->filesystem->deleteDir($item['path']);
  54. } else {
  55. $this->filesystem->delete($item['path']);
  56. }
  57. }
  58. }
  59. /**
  60. * @test
  61. * @depends testInstantiable
  62. */
  63. public function writing_reading_deleting()
  64. {
  65. $filesystem = $this->filesystem;
  66. $this->assertTrue($filesystem->put('path.txt', 'file contents'));
  67. $this->assertEquals('file contents', $filesystem->read('path.txt'));
  68. $this->assertTrue($filesystem->delete('path.txt'));
  69. }
  70. /**
  71. * @test
  72. * @dataProvider filenameProvider
  73. */
  74. public function writing_and_reading_files_with_special_path(string $path): void
  75. {
  76. $this->setup_filesystem();
  77. $filesystem = $this->filesystem;
  78. $filesystem->write($path, 'contents');
  79. $filesystem->listContents('some');
  80. $contents = $filesystem->read($path);
  81. $this->assertEquals('contents', $contents);
  82. }
  83. public function filenameProvider(): Generator
  84. {
  85. yield "a path with square brackets in filename 1" => ["some/file[name].txt"];
  86. yield "a path with square brackets in filename 2" => ["some/file[0].txt"];
  87. yield "a path with square brackets in filename 3" => ["some/file[10].txt"];
  88. yield "a path with square brackets in dirname 1" => ["some[name]/file.txt"];
  89. yield "a path with square brackets in dirname 3" => ["some[10]/file.txt"];
  90. yield "a path with square brackets in dirname 2" => ["some[0]/file.txt"];
  91. yield "a path with curly brackets in filename 1" => ["some/file{name}.txt"];
  92. yield "a path with curly brackets in filename 2" => ["some/file{0}.txt"];
  93. yield "a path with curly brackets in filename 3" => ["some/file{10}.txt"];
  94. yield "a path with curly brackets in dirname 1" => ["some{name}/filename.txt"];
  95. yield "a path with curly brackets in dirname 2" => ["some{0}/filename.txt"];
  96. yield "a path with curly brackets in dirname 3" => ["some{10}/filename.txt"];
  97. yield "a path with plus sign in dirname" => ["some+dir/filename.txt"];
  98. yield "a path with plus sign in filename" => ["some/file+name.txt"];
  99. }
  100. /**
  101. * @test
  102. * @depends testInstantiable
  103. */
  104. public function creating_a_directory()
  105. {
  106. $this->filesystem->createDir('dirname/directory');
  107. $metadata = $this->filesystem->getMetadata('dirname/directory');
  108. self::assertEquals('dir', $metadata['type']);
  109. $this->filesystem->deleteDir('dirname');
  110. }
  111. /**
  112. * @test
  113. * @depends testInstantiable
  114. */
  115. public function writing_in_a_directory_and_deleting_the_directory()
  116. {
  117. $filesystem = $this->filesystem;
  118. $this->assertTrue($filesystem->write('deeply/nested/path.txt', 'contents'));
  119. $this->assertTrue($filesystem->has('deeply/nested'));
  120. $this->assertTrue($filesystem->has('deeply'));
  121. $this->assertTrue($filesystem->has('deeply/nested/path.txt'));
  122. $this->assertTrue($filesystem->deleteDir('deeply/nested'));
  123. $this->assertFalse($filesystem->has('deeply/nested'));
  124. $this->assertFalse($filesystem->has('deeply/nested/path.txt'));
  125. $this->assertTrue($filesystem->has('deeply'));
  126. $this->assertTrue($filesystem->deleteDir('deeply'));
  127. $this->assertFalse($filesystem->has('deeply'));
  128. }
  129. /**
  130. * @test
  131. * @depends testInstantiable
  132. */
  133. public function listing_files_of_a_directory()
  134. {
  135. $filesystem = $this->filesystem;
  136. $filesystem->write('dirname/a.txt', 'contents');
  137. $filesystem->write('dirname/b/b.txt', 'contents');
  138. $filesystem->write('dirname/c.txt', 'contents');
  139. $files = $filesystem->listContents('', true);
  140. $files = array_map(function($i) { return $i['path']; }, $files);
  141. $expected = ['dirname', 'dirname/a.txt', 'dirname/b', 'dirname/b/b.txt', 'dirname/c.txt'];
  142. $filesystem->deleteDir('dirname');
  143. $this->assertEquals($expected, $files);
  144. }
  145. }