Skip to content

Commit 67b7e7b

Browse files
authored
Merge pull request #573 from FriendsOfCake/6.0
6.0
2 parents 09e74b9 + 3fd416b commit 67b7e7b

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
],
1414
"require": {
1515
"cakephp/orm": "^4.0.2",
16-
"league/flysystem": "^1.0"
16+
"league/flysystem": "^2.2"
1717
},
1818
"require-dev": {
1919
"cakephp/cakephp": "^4.0.2",
2020
"phpunit/phpunit": "~8.5.0",
21-
"league/flysystem-vfs": "*",
22-
"cakephp/cakephp-codesniffer": "^4.0"
21+
"cakephp/cakephp-codesniffer": "^4.0",
22+
"league/flysystem-memory": "^2.0",
23+
"php-vfs/php-vfs": "^1.4.2"
2324
},
2425
"autoload": {
2526
"psr-4": {

src/File/Writer/DefaultWriter.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use Cake\Datasource\EntityInterface;
77
use Cake\ORM\Table;
88
use Cake\Utility\Hash;
9-
use League\Flysystem\Adapter\Local;
10-
use League\Flysystem\AdapterInterface;
11-
use League\Flysystem\FileNotFoundException;
129
use League\Flysystem\Filesystem;
13-
use League\Flysystem\FilesystemInterface;
10+
use League\Flysystem\FilesystemAdapter;
11+
use League\Flysystem\FilesystemException;
12+
use League\Flysystem\FilesystemOperator;
13+
use League\Flysystem\Local\LocalFilesystemAdapter;
14+
use League\Flysystem\Visibility;
1415
use Psr\Http\Message\UploadedFileInterface;
1516
use UnexpectedValueException;
1617

@@ -111,12 +112,12 @@ public function delete(array $files): array
111112
/**
112113
* Writes a set of files to an output
113114
*
114-
* @param \League\Flysystem\FilesystemInterface $filesystem a filesystem wrapper
115+
* @param \League\Flysystem\FilesystemOperator $filesystem a filesystem wrapper
115116
* @param string $file a full path to a temp file
116117
* @param string $path that path to which the file should be written
117118
* @return bool
118119
*/
119-
public function writeFile(FilesystemInterface $filesystem, $file, $path): bool
120+
public function writeFile(FilesystemOperator $filesystem, $file, $path): bool
120121
{
121122
// phpcs:ignore
122123
$stream = @fopen($file, 'r');
@@ -127,10 +128,19 @@ public function writeFile(FilesystemInterface $filesystem, $file, $path): bool
127128
$success = false;
128129
$tempPath = $path . '.temp';
129130
$this->deletePath($filesystem, $tempPath);
130-
if ($filesystem->writeStream($tempPath, $stream)) {
131+
try {
132+
$filesystem->writeStream($tempPath, $stream);
131133
$this->deletePath($filesystem, $path);
132-
$success = $filesystem->rename($tempPath, $path);
134+
try {
135+
$filesystem->move($tempPath, $path);
136+
$success = true;
137+
} catch (FilesystemException $e) {
138+
// noop
139+
}
140+
} catch (FilesystemException $e) {
141+
// noop
133142
}
143+
134144
$this->deletePath($filesystem, $tempPath);
135145
is_resource($stream) && fclose($stream);
136146

@@ -140,16 +150,17 @@ public function writeFile(FilesystemInterface $filesystem, $file, $path): bool
140150
/**
141151
* Deletes a path from a filesystem
142152
*
143-
* @param \League\Flysystem\FilesystemInterface $filesystem a filesystem writer
153+
* @param \League\Flysystem\FilesystemOperator $filesystem a filesystem writer
144154
* @param string $path the path that should be deleted
145155
* @return bool
146156
*/
147-
public function deletePath(FilesystemInterface $filesystem, string $path): bool
157+
public function deletePath(FilesystemOperator $filesystem, string $path): bool
148158
{
149-
$success = false;
159+
$success = true;
150160
try {
151-
$success = $filesystem->delete($path);
152-
} catch (FileNotFoundException $e) {
161+
$filesystem->delete($path);
162+
} catch (FilesystemException $e) {
163+
$success = false;
153164
// TODO: log this?
154165
}
155166

@@ -161,19 +172,19 @@ public function deletePath(FilesystemInterface $filesystem, string $path): bool
161172
*
162173
* @param string $field the field for which data will be saved
163174
* @param array $settings the settings for the current field
164-
* @return \League\Flysystem\FilesystemInterface
175+
* @return \League\Flysystem\FilesystemOperator
165176
*/
166-
public function getFilesystem(string $field, array $settings = []): FilesystemInterface
177+
public function getFilesystem(string $field, array $settings = []): FilesystemOperator
167178
{
168-
$adapter = new Local(Hash::get($settings, 'filesystem.root', ROOT . DS));
179+
$adapter = new LocalFilesystemAdapter(Hash::get($settings, 'filesystem.root', ROOT . DS));
169180
$adapter = Hash::get($settings, 'filesystem.adapter', $adapter);
170181
if (is_callable($adapter)) {
171182
$adapter = $adapter();
172183
}
173184

174-
if ($adapter instanceof AdapterInterface) {
185+
if ($adapter instanceof FilesystemAdapter) {
175186
return new Filesystem($adapter, Hash::get($settings, 'filesystem.options', [
176-
'visibility' => AdapterInterface::VISIBILITY_PUBLIC,
187+
'visibility' => Visibility::PUBLIC,
177188
]));
178189
}
179190

tests/TestCase/File/Writer/DefaultWriterTest.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use Cake\TestSuite\TestCase;
77
use Josegonzalez\Upload\File\Writer\DefaultWriter;
88
use Laminas\Diactoros\UploadedFile;
9-
use League\Flysystem\Adapter\NullAdapter;
10-
use League\Flysystem\Vfs\VfsAdapter;
9+
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
10+
use League\Flysystem\UnableToDeleteFile;
11+
use League\Flysystem\UnableToMoveFile;
12+
use League\Flysystem\UnableToWriteFile;
1113
use VirtualFileSystem\FileSystem as Vfs;
1214

1315
class DefaultWriterTest extends TestCase
@@ -29,7 +31,7 @@ public function setUp(): void
2931
$this->settings = [
3032
'filesystem' => [
3133
'adapter' => function () {
32-
return new VfsAdapter(new Vfs());
34+
return new InMemoryFilesystemAdapter();
3335
},
3436
],
3537
];
@@ -65,9 +67,9 @@ public function testInvoke()
6567

6668
public function testDelete()
6769
{
68-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
69-
$filesystem->expects($this->at(0))->method('delete')->will($this->returnValue(true));
70-
$filesystem->expects($this->at(1))->method('delete')->will($this->returnValue(false));
70+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
71+
$filesystem->expects($this->at(0))->method('delete');
72+
$filesystem->expects($this->at(1))->method('delete')->will($this->throwException(new UnableToDeleteFile()));
7173
$writer = $this->getMockBuilder('Josegonzalez\Upload\File\Writer\DefaultWriter')
7274
->setMethods(['getFilesystem'])
7375
->setConstructorArgs([$this->table, $this->entity, $this->data, $this->field, $this->settings])
@@ -86,51 +88,51 @@ public function testDelete()
8688

8789
public function testWriteFile()
8890
{
89-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
90-
$filesystem->expects($this->once())->method('writeStream')->will($this->returnValue(true));
91-
$filesystem->expects($this->exactly(3))->method('delete')->will($this->returnValue(true));
92-
$filesystem->expects($this->once())->method('rename')->will($this->returnValue(true));
91+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
92+
$filesystem->expects($this->once())->method('writeStream');
93+
$filesystem->expects($this->exactly(3))->method('delete');
94+
$filesystem->expects($this->once())->method('move');
9395
$this->assertTrue($this->writer->writeFile($filesystem, $this->vfs->path('/tmp/tempfile'), 'path'));
9496

95-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
96-
$filesystem->expects($this->once())->method('writeStream')->will($this->returnValue(false));
97-
$filesystem->expects($this->exactly(2))->method('delete')->will($this->returnValue(true));
98-
$filesystem->expects($this->never())->method('rename');
97+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
98+
$filesystem->expects($this->once())->method('writeStream')->will($this->throwException(new UnableToWriteFile()));
99+
$filesystem->expects($this->exactly(2))->method('delete');
100+
$filesystem->expects($this->never())->method('move');
99101
$this->assertFalse($this->writer->writeFile($filesystem, $this->vfs->path('/tmp/tempfile'), 'path'));
100102

101-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
102-
$filesystem->expects($this->once())->method('writeStream')->will($this->returnValue(true));
103-
$filesystem->expects($this->exactly(3))->method('delete')->will($this->returnValue(true));
104-
$filesystem->expects($this->once())->method('rename')->will($this->returnValue(false));
103+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
104+
$filesystem->expects($this->once())->method('writeStream');
105+
$filesystem->expects($this->exactly(3))->method('delete');
106+
$filesystem->expects($this->once())->method('move')->will($this->throwException(new UnableToMoveFile()));
105107
$this->assertFalse($this->writer->writeFile($filesystem, $this->vfs->path('/tmp/tempfile'), 'path'));
106108
}
107109

108110
public function testDeletePath()
109111
{
110-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
111-
$filesystem->expects($this->any())->method('delete')->will($this->returnValue(true));
112+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
113+
$filesystem->expects($this->any())->method('delete');
112114
$this->assertTrue($this->writer->deletePath($filesystem, 'path'));
113115

114-
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemInterface')->getMock();
115-
$filesystem->expects($this->any())->method('delete')->will($this->returnValue(false));
116+
$filesystem = $this->getMockBuilder('League\Flysystem\FilesystemOperator')->getMock();
117+
$filesystem->expects($this->any())->method('delete')->will($this->throwException(new UnableToDeleteFile()));
116118
$this->assertFalse($this->writer->deletePath($filesystem, 'path'));
117119
}
118120

119121
public function testGetFilesystem()
120122
{
121-
$this->assertInstanceOf('League\Flysystem\FilesystemInterface', $this->writer->getFilesystem('field', []));
122-
$this->assertInstanceOf('League\Flysystem\FilesystemInterface', $this->writer->getFilesystem('field', [
123+
$this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->writer->getFilesystem('field', []));
124+
$this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->writer->getFilesystem('field', [
123125
'key' => 'value',
124126
]));
125-
$this->assertInstanceOf('League\Flysystem\FilesystemInterface', $this->writer->getFilesystem('field', [
127+
$this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->writer->getFilesystem('field', [
126128
'filesystem' => [
127-
'adapter' => new NullAdapter(),
129+
'adapter' => new InMemoryFilesystemAdapter(),
128130
],
129131
]));
130-
$this->assertInstanceOf('League\Flysystem\FilesystemInterface', $this->writer->getFilesystem('field', [
132+
$this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->writer->getFilesystem('field', [
131133
'filesystem' => [
132134
'adapter' => function () {
133-
return new NullAdapter();
135+
return new InMemoryFilesystemAdapter();
134136
},
135137
],
136138
]));

0 commit comments

Comments
 (0)