Skip to content

Commit cfe85df

Browse files
authored
An opportunity to create directories (#6)
* An opportunity to create directories
1 parent 2a2e414 commit cfe85df

File tree

2 files changed

+125
-25
lines changed

2 files changed

+125
-25
lines changed

src/Extension/FileSystem.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,48 @@ public function __construct(FilesystemInterface $flySystem)
2525
* @param array $config
2626
*
2727
* @throws FileExistsException
28+
*
29+
* @return bool
2830
*/
2931
public function writeFile($path, $contents, array $config = [])
3032
{
31-
$this->flySystem->write($path, $contents, $config);
33+
return $this->flySystem->write($path, $contents, $config);
3234
}
3335

3436
/**
3537
* @param string $path
3638
*
3739
* @throws FileNotFoundException
40+
*
41+
* @return bool
3842
*/
3943
public function deleteFile($path)
4044
{
41-
$this->flySystem->delete($path);
45+
return $this->flySystem->delete($path);
4246
}
4347

4448
/**
4549
* @param string $path
50+
*
51+
* @return bool
4652
*/
4753
public function clearDir($path)
4854
{
49-
$this->flySystem->deleteDir($path);
50-
$this->flySystem->createDir($path);
55+
if (!$this->flySystem->deleteDir($path)) {
56+
return false;
57+
}
58+
59+
return $this->flySystem->createDir($path);
60+
}
61+
62+
/**
63+
* @param string $path
64+
*
65+
* @return bool
66+
*/
67+
public function createDir($path)
68+
{
69+
return $this->flySystem->createDir($path);
5170
}
5271

5372
/**
@@ -56,10 +75,12 @@ public function clearDir($path)
5675
*
5776
* @throws FileExistsException
5877
* @throws FileNotFoundException
78+
*
79+
* @return bool
5980
*/
6081
public function copyFile($path, $newPath)
6182
{
62-
$this->flySystem->copy($path, $newPath);
83+
return $this->flySystem->copy($path, $newPath);
6384
}
6485

6586
/**

tests/unit/Extension/FileSystemTest.php

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,112 +13,191 @@ class FileSystemTest extends Unit
1313
/**
1414
* @param array $parameters
1515
* @param array $expectedParameters
16+
* @param bool $operationResult
1617
*
1718
* @throws \League\Flysystem\FileExistsException
1819
*
1920
* @dataProvider dataWriteFile
2021
*/
21-
public function testWriteFile($parameters, $expectedParameters)
22+
public function testWriteFile($parameters, $expectedParameters, $operationResult)
2223
{
2324
$flySystem = $this->createMock(FilesystemInterface::class);
2425
$flySystem
2526
->expects($this->once())
2627
->method('write')
27-
->with(...$expectedParameters);
28+
->with(...$expectedParameters)
29+
->willReturn($operationResult);
2830

2931
$fileSystem = new FileSystem($flySystem);
30-
$fileSystem->writeFile(...$parameters);
32+
33+
$this->assertEquals($operationResult, $fileSystem->writeFile(...$parameters));
3134
}
3235

3336
public function dataWriteFile()
3437
{
3538
return [
3639
[
37-
['path/to/file', 'file content', ['key' => 'value']],
38-
['path/to/file', 'file content', ['key' => 'value']],
40+
'Parameters' => ['path/to/file', 'file content', ['key' => 'value']],
41+
'Expected parameters' => ['path/to/file', 'file content', ['key' => 'value']],
42+
'Operation result' => true,
43+
],
44+
[
45+
'Parameters' => ['path/to/file', 'file content'],
46+
'Expected parameters' => ['path/to/file', 'file content', []],
47+
'Operation result' => true,
3948
],
4049
[
41-
['path/to/file', 'file content'],
42-
['path/to/file', 'file content', []],
50+
'Parameters' => ['path/to/file', 'file content'],
51+
'Expected parameters' => ['path/to/file', 'file content', []],
52+
'Operation result' => false,
4353
],
4454
];
4555
}
4656

4757
/**
4858
* @param string $path
59+
* @param bool $operationResult
4960
*
5061
* @throws FileNotFoundException
5162
*
5263
* @dataProvider dataFilePath
5364
*/
54-
public function testDeleteFile($path)
65+
public function testDeleteFile($path, $operationResult)
5566
{
5667
$flySystem = $this->createMock(FilesystemInterface::class);
5768
$flySystem
5869
->expects($this->once())
5970
->method('delete')
60-
->with($path);
71+
->with($path)
72+
->willReturn($operationResult);
6173

6274
$fileSystem = new FileSystem($flySystem);
63-
$fileSystem->deleteFile($path);
75+
76+
$this->assertEquals($operationResult, $fileSystem->deleteFile($path));
6477
}
6578

6679
/**
6780
* @param string $path
81+
* @param bool $operationResult
6882
*
6983
* @dataProvider dataFilePath
7084
*/
71-
public function testClearDir($path)
85+
public function testClearDir($path, $operationResult)
7286
{
7387
$flySystem = $this->createMock(FilesystemInterface::class);
7488
$flySystem
7589
->expects($this->once())
7690
->method('deleteDir')
77-
->with($path);
91+
->with($path)
92+
->willReturn(true);
7893
$flySystem
7994
->expects($this->once())
8095
->method('createDir')
81-
->with($path);
96+
->with($path)
97+
->willReturn($operationResult);
8298

8399
$fileSystem = new FileSystem($flySystem);
84-
$fileSystem->clearDir($path);
100+
$this->assertEquals($operationResult, $fileSystem->clearDir($path));
85101
}
86102

87103
public function dataFilePath()
88104
{
89105
return [
90106
[
91-
'path/to/file',
107+
'Path' => 'path/to/file',
108+
'Operation result' => true,
109+
],
110+
[
111+
'Path' => 'path/to/file',
112+
'Operation result' => false,
92113
],
93114
];
94115
}
95116

117+
public function testClearDirErrorOnDeleting()
118+
{
119+
$flySystem = $this->createMock(FilesystemInterface::class);
120+
$flySystem
121+
->expects($this->once())
122+
->method('deleteDir')
123+
->with('/some/path')
124+
->willReturn(false);
125+
$flySystem
126+
->expects($this->never())
127+
->method('createDir');
128+
129+
$fileSystem = new FileSystem($flySystem);
130+
$this->assertFalse($fileSystem->clearDir('/some/path'));
131+
}
132+
96133
/**
97-
* @param $path
134+
* @param string $path
135+
* @param bool $operationResult
136+
*
137+
* @dataProvider dataDirPath
138+
*/
139+
public function testCreateDir($path, $operationResult)
140+
{
141+
$flySystem = $this->createMock(FilesystemInterface::class);
142+
$flySystem
143+
->expects($this->once())
144+
->method('createDir')
145+
->with($path)
146+
->willReturn($operationResult);
147+
148+
$fileSystem = new FileSystem($flySystem);
149+
$this->assertEquals($operationResult, $fileSystem->createDir($path));
150+
}
151+
152+
public function dataDirPath()
153+
{
154+
return [
155+
[
156+
'Path' => 'path/to/dir',
157+
'Operation result' => true,
158+
],
159+
[
160+
'Path' => 'path/to/dir',
161+
'Operation result' => false,
162+
],
163+
];
164+
}
165+
166+
/**
167+
* @param string $path
98168
* @param $newPath
169+
* @param bool $operationResult
99170
*
100171
* @throws FileNotFoundException
101172
* @throws \League\Flysystem\FileExistsException
102173
*
103174
* @dataProvider dataFilePaths
104175
*/
105-
public function testCopyFile($path, $newPath)
176+
public function testCopyFile($path, $newPath, $operationResult)
106177
{
107178
$flySystem = $this->createMock(FilesystemInterface::class);
108179
$flySystem
109180
->expects($this->once())
110181
->method('copy')
111-
->with($path, $newPath);
182+
->with($path, $newPath)
183+
->willReturn($operationResult);
112184

113185
$fileSystem = new FileSystem($flySystem);
114-
$fileSystem->copyFile($path, $newPath);
186+
$this->assertEquals($operationResult, $fileSystem->copyFile($path, $newPath));
115187
}
116188

117189
public function dataFilePaths()
118190
{
119191
return [
120192
[
121-
'old/path/to/file', 'new/path/to/file',
193+
'Path' => 'old/path/to/file',
194+
'New path' => 'new/path/to/file',
195+
'Operation result' => true,
196+
],
197+
[
198+
'Path' => 'old/path/to/file',
199+
'New path' => 'new/path/to/file',
200+
'Operation result' => false,
122201
],
123202
];
124203
}

0 commit comments

Comments
 (0)