Skip to content

Commit

Permalink
feat: correctly handle errors in ftpRawlist
Browse files Browse the repository at this point in the history
- throw exception compatible with flysystem listContents api
- add tests with subfolders
- fixes #6
  • Loading branch information
oprudkyi committed Dec 10, 2024
1 parent c3ec379 commit 3b99dd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/CurlFtpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToListContents;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
Expand Down Expand Up @@ -508,6 +509,14 @@ private function ftpRawlist(string $options, string $path): array
$request = rtrim('LIST '.$options.$path);
$listing = $this->connection()->exec([CURLOPT_CUSTOMREQUEST => $request]);

if ($listing === false) {
throw UnableToListContents::atLocation(
$path,
strpos($options, 'R') !== false,
new RuntimeException($this->connection->lastError()),
);
}

return explode(PHP_EOL, $listing);
}

Expand Down
40 changes: 40 additions & 0 deletions tests/CurlFtpAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,30 @@ public function testListContentsEmptyPath(string $path): void
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname($path), false), false));
}

/**
* @dataProvider withSubSubFolderProvider
*/
public function testListContentsDeep(string $path): void
{
$contents = $this->faker()->text;
$this->createResourceFile($path, $contents);

$this->assertCount(1, iterator_to_array($this->adapter->listContents(dirname($path), true), false));
$this->assertCount(2, iterator_to_array($this->adapter->listContents(dirname(dirname($path)), true), false));
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname(dirname($path)) . '/not exists folder', true), false));
}

/**
* @dataProvider withSubSubFolderProvider
*/
public function testListContentsDeepEmptyPath(string $path): void
{
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname($path), true), false));
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname(dirname($path)), true), false));
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname(dirname($path)) . '/not exists folder', true), false));
$this->assertCount(0, iterator_to_array($this->adapter->listContents(dirname(dirname($path)) . '/not exists folder', false), false));
}

/**
* Tests that a FTP server is still in root directory as its working directory
* after reading a file, especially if this file is in a subfolder.
Expand Down Expand Up @@ -364,4 +388,20 @@ public static function withSubFolderProvider(): array
[self::faker()->word.'/'.self::randomFileName()],
];
}

public static function withSubSubFolderProvider(): array
{
return [
['test/test/test.txt'],
['тёст/тёст/тёст.txt'],
['test 1/test 1/test.txt'],
['test/test/test 1.txt'],
['test 1/test 2/test 3.txt'],
[self::faker()->word.'/'.self::faker()->word.'/'.self::randomFileName()],
[self::faker()->word.'/'.self::faker()->word.'/'.self::randomFileName()],
[self::faker()->word.'/'.self::faker()->word.'/'.self::randomFileName()],
[self::faker()->word.'/'.self::faker()->word.'/'.self::randomFileName()],
[self::faker()->word.'/'.self::faker()->word.'/'.self::randomFileName()],
];
}
}

0 comments on commit 3b99dd5

Please sign in to comment.