Skip to content

An opportunity to create directories #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/Extension/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,48 @@ public function __construct(FilesystemInterface $flySystem)
* @param array $config
*
* @throws FileExistsException
*
* @return bool
*/
public function writeFile($path, $contents, array $config = [])
{
$this->flySystem->write($path, $contents, $config);
return $this->flySystem->write($path, $contents, $config);
}

/**
* @param string $path
*
* @throws FileNotFoundException
*
* @return bool
*/
public function deleteFile($path)
{
$this->flySystem->delete($path);
return $this->flySystem->delete($path);
}

/**
* @param string $path
*
* @return bool
*/
public function clearDir($path)
{
$this->flySystem->deleteDir($path);
$this->flySystem->createDir($path);
if (!$this->flySystem->deleteDir($path)) {
return false;
}

return $this->flySystem->createDir($path);
}

/**
* @param string $path
*
* @return bool
*/
public function createDir($path)
{
return $this->flySystem->createDir($path);
}

/**
Expand All @@ -56,10 +75,12 @@ public function clearDir($path)
*
* @throws FileExistsException
* @throws FileNotFoundException
*
* @return bool
*/
public function copyFile($path, $newPath)
{
$this->flySystem->copy($path, $newPath);
return $this->flySystem->copy($path, $newPath);
}

/**
Expand Down
119 changes: 99 additions & 20 deletions tests/unit/Extension/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,112 +13,191 @@ class FileSystemTest extends Unit
/**
* @param array $parameters
* @param array $expectedParameters
* @param bool $operationResult
*
* @throws \League\Flysystem\FileExistsException
*
* @dataProvider dataWriteFile
*/
public function testWriteFile($parameters, $expectedParameters)
public function testWriteFile($parameters, $expectedParameters, $operationResult)
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('write')
->with(...$expectedParameters);
->with(...$expectedParameters)
->willReturn($operationResult);

$fileSystem = new FileSystem($flySystem);
$fileSystem->writeFile(...$parameters);

$this->assertEquals($operationResult, $fileSystem->writeFile(...$parameters));
}

public function dataWriteFile()
{
return [
[
['path/to/file', 'file content', ['key' => 'value']],
['path/to/file', 'file content', ['key' => 'value']],
'Parameters' => ['path/to/file', 'file content', ['key' => 'value']],
'Expected parameters' => ['path/to/file', 'file content', ['key' => 'value']],
'Operation result' => true,
],
[
'Parameters' => ['path/to/file', 'file content'],
'Expected parameters' => ['path/to/file', 'file content', []],
'Operation result' => true,
],
[
['path/to/file', 'file content'],
['path/to/file', 'file content', []],
'Parameters' => ['path/to/file', 'file content'],
'Expected parameters' => ['path/to/file', 'file content', []],
'Operation result' => false,
],
];
}

/**
* @param string $path
* @param bool $operationResult
*
* @throws FileNotFoundException
*
* @dataProvider dataFilePath
*/
public function testDeleteFile($path)
public function testDeleteFile($path, $operationResult)
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('delete')
->with($path);
->with($path)
->willReturn($operationResult);

$fileSystem = new FileSystem($flySystem);
$fileSystem->deleteFile($path);

$this->assertEquals($operationResult, $fileSystem->deleteFile($path));
}

/**
* @param string $path
* @param bool $operationResult
*
* @dataProvider dataFilePath
*/
public function testClearDir($path)
public function testClearDir($path, $operationResult)
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('deleteDir')
->with($path);
->with($path)
->willReturn(true);
$flySystem
->expects($this->once())
->method('createDir')
->with($path);
->with($path)
->willReturn($operationResult);

$fileSystem = new FileSystem($flySystem);
$fileSystem->clearDir($path);
$this->assertEquals($operationResult, $fileSystem->clearDir($path));
}

public function dataFilePath()
{
return [
[
'path/to/file',
'Path' => 'path/to/file',
'Operation result' => true,
],
[
'Path' => 'path/to/file',
'Operation result' => false,
],
];
}

public function testClearDirErrorOnDeleting()
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('deleteDir')
->with('/some/path')
->willReturn(false);
$flySystem
->expects($this->never())
->method('createDir');

$fileSystem = new FileSystem($flySystem);
$this->assertFalse($fileSystem->clearDir('/some/path'));
}

/**
* @param $path
* @param string $path
* @param bool $operationResult
*
* @dataProvider dataDirPath
*/
public function testCreateDir($path, $operationResult)
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('createDir')
->with($path)
->willReturn($operationResult);

$fileSystem = new FileSystem($flySystem);
$this->assertEquals($operationResult, $fileSystem->createDir($path));
}

public function dataDirPath()
{
return [
[
'Path' => 'path/to/dir',
'Operation result' => true,
],
[
'Path' => 'path/to/dir',
'Operation result' => false,
],
];
}

/**
* @param string $path
* @param $newPath
* @param bool $operationResult
*
* @throws FileNotFoundException
* @throws \League\Flysystem\FileExistsException
*
* @dataProvider dataFilePaths
*/
public function testCopyFile($path, $newPath)
public function testCopyFile($path, $newPath, $operationResult)
{
$flySystem = $this->createMock(FilesystemInterface::class);
$flySystem
->expects($this->once())
->method('copy')
->with($path, $newPath);
->with($path, $newPath)
->willReturn($operationResult);

$fileSystem = new FileSystem($flySystem);
$fileSystem->copyFile($path, $newPath);
$this->assertEquals($operationResult, $fileSystem->copyFile($path, $newPath));
}

public function dataFilePaths()
{
return [
[
'old/path/to/file', 'new/path/to/file',
'Path' => 'old/path/to/file',
'New path' => 'new/path/to/file',
'Operation result' => true,
],
[
'Path' => 'old/path/to/file',
'New path' => 'new/path/to/file',
'Operation result' => false,
],
];
}
Expand Down