/
FilesystemAdapterTest.php
304 lines (242 loc) · 11.4 KB
/
FilesystemAdapterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace Illuminate\Tests\Filesystem;
use GuzzleHttp\Psr7\Stream;
use Illuminate\Contracts\Filesystem\FileExistsException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Http\UploadedFile;
use InvalidArgumentException;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\StreamedResponse;
class FilesystemAdapterTest extends TestCase
{
private $tempDir;
private $filesystem;
protected function setUp(): void
{
$this->tempDir = __DIR__.'/tmp';
$this->filesystem = new Filesystem(new Local($this->tempDir));
}
protected function tearDown(): void
{
$filesystem = new Filesystem(new Local(dirname($this->tempDir)));
$filesystem->deleteDir(basename($this->tempDir));
m::close();
}
public function testResponse()
{
$this->filesystem->write('file.txt', 'Hello World');
$files = new FilesystemAdapter($this->filesystem);
$response = $files->response('file.txt');
ob_start();
$response->sendContent();
$content = ob_get_clean();
$this->assertInstanceOf(StreamedResponse::class, $response);
$this->assertSame('Hello World', $content);
// $this->assertSame('inline; filename="file.txt"', $response->headers->get('content-disposition'));
}
public function testDownload()
{
$this->filesystem->write('file.txt', 'Hello World');
$files = new FilesystemAdapter($this->filesystem);
$response = $files->download('file.txt', 'hello.txt');
$this->assertInstanceOf(StreamedResponse::class, $response);
// $this->assertSame('attachment; filename="hello.txt"', $response->headers->get('content-disposition'));
}
public function testDownloadNonAsciiFilename()
{
$this->filesystem->write('file.txt', 'Hello World');
$files = new FilesystemAdapter($this->filesystem);
$response = $files->download('file.txt', 'пиздюк.txt');
$this->assertInstanceOf(StreamedResponse::class, $response);
$this->assertSame("attachment; filename=pizdyuk.txt; filename*=utf-8''%D0%BF%D0%B8%D0%B7%D0%B4%D1%8E%D0%BA.txt", $response->headers->get('content-disposition'));
}
public function testDownloadNonAsciiEmptyFilename()
{
$this->filesystem->write('пиздюк.txt', 'Hello World');
$files = new FilesystemAdapter($this->filesystem);
$response = $files->download('пиздюк.txt');
$this->assertInstanceOf(StreamedResponse::class, $response);
$this->assertSame('attachment; filename=pizdyuk.txt; filename*=utf-8\'\'%D0%BF%D0%B8%D0%B7%D0%B4%D1%8E%D0%BA.txt', $response->headers->get('content-disposition'));
}
public function testDownloadPercentInFilename()
{
$this->filesystem->write('Hello%World.txt', 'Hello World');
$files = new FilesystemAdapter($this->filesystem);
$response = $files->download('Hello%World.txt', 'Hello%World.txt');
$this->assertInstanceOf(StreamedResponse::class, $response);
$this->assertSame('attachment; filename=HelloWorld.txt; filename*=utf-8\'\'Hello%25World.txt', $response->headers->get('content-disposition'));
}
public function testExists()
{
$this->filesystem->write('file.txt', 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertTrue($filesystemAdapter->exists('file.txt'));
}
public function testMissing()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertTrue($filesystemAdapter->missing('file.txt'));
}
public function testPath()
{
$this->filesystem->write('file.txt', 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertEquals($this->tempDir.DIRECTORY_SEPARATOR.'file.txt', $filesystemAdapter->path('file.txt'));
}
public function testGet()
{
$this->filesystem->write('file.txt', 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertSame('Hello World', $filesystemAdapter->get('file.txt'));
}
public function testGetFileNotFound()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->expectException(FileNotFoundException::class);
$filesystemAdapter->get('file.txt');
}
public function testPut()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->put('file.txt', 'Something inside');
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Something inside');
}
public function testPrepend()
{
file_put_contents($this->tempDir.'/file.txt', 'World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->prepend('file.txt', 'Hello ');
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Hello '.PHP_EOL.'World');
}
public function testAppend()
{
file_put_contents($this->tempDir.'/file.txt', 'Hello ');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->append('file.txt', 'Moon');
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Hello '.PHP_EOL.'Moon');
}
public function testDelete()
{
file_put_contents($this->tempDir.'/file.txt', 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertTrue($filesystemAdapter->delete('file.txt'));
$this->assertFileNotExists($this->tempDir.'/file.txt');
}
public function testDeleteReturnsFalseWhenFileNotFound()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$this->assertFalse($filesystemAdapter->delete('file.txt'));
}
public function testCopy()
{
$data = '33232';
mkdir($this->tempDir.'/foo');
file_put_contents($this->tempDir.'/foo/foo.txt', $data);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->copy('/foo/foo.txt', '/foo/foo2.txt');
$this->assertFileExists($this->tempDir.'/foo/foo.txt');
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo.txt'));
$this->assertFileExists($this->tempDir.'/foo/foo2.txt');
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo2.txt'));
}
public function testMove()
{
$data = '33232';
mkdir($this->tempDir.'/foo');
file_put_contents($this->tempDir.'/foo/foo.txt', $data);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->move('/foo/foo.txt', '/foo/foo2.txt');
$this->assertFileNotExists($this->tempDir.'/foo/foo.txt');
$this->assertFileExists($this->tempDir.'/foo/foo2.txt');
$this->assertEquals($data, file_get_contents($this->tempDir.'/foo/foo2.txt'));
}
public function testStream()
{
$this->filesystem->write('file.txt', $original_content = 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$filesystemAdapter->writeStream('copy.txt', $readStream);
$this->assertEquals($original_content, $filesystemAdapter->get('copy.txt'));
}
public function testStreamBetweenFilesystems()
{
$secondFilesystem = new Filesystem(new Local($this->tempDir.'/second'));
$this->filesystem->write('file.txt', $original_content = 'Hello World');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$secondFilesystemAdapter = new FilesystemAdapter($secondFilesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$secondFilesystemAdapter->writeStream('copy.txt', $readStream);
$this->assertEquals($original_content, $secondFilesystemAdapter->get('copy.txt'));
}
public function testStreamToExistingFileThrows()
{
$this->expectException(FileExistsException::class);
$this->filesystem->write('file.txt', 'Hello World');
$this->filesystem->write('existing.txt', 'Dear Kate');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$readStream = $filesystemAdapter->readStream('file.txt');
$filesystemAdapter->writeStream('existing.txt', $readStream);
}
public function testReadStreamNonExistentFileThrows()
{
$this->expectException(FileNotFoundException::class);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->readStream('nonexistent.txt');
}
public function testStreamInvalidResourceThrows()
{
$this->expectException(InvalidArgumentException::class);
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$filesystemAdapter->writeStream('file.txt', 'foo bar');
}
public function testPutWithStreamInterface()
{
file_put_contents($this->tempDir.'/foo.txt', 'some-data');
$spy = m::spy($this->filesystem);
$filesystemAdapter = new FilesystemAdapter($spy);
$stream = new Stream(fopen($this->tempDir.'/foo.txt', 'r'));
$filesystemAdapter->put('bar.txt', $stream);
$spy->shouldHaveReceived('putStream');
$this->assertEquals('some-data', $filesystemAdapter->get('bar.txt'));
}
public function testPutFileAs()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$uploadedFile = new UploadedFile($filePath, 'org.txt', null, null, true);
$storagePath = $filesystemAdapter->putFileAs('/', $uploadedFile, 'new.txt');
$this->assertSame('new.txt', $storagePath);
$this->assertFileExists($filePath);
$filesystemAdapter->assertExists($storagePath);
$this->assertSame('uploaded file content', $filesystemAdapter->read($storagePath));
}
public function testPutFileAsWithAbsoluteFilePath()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$storagePath = $filesystemAdapter->putFileAs('/', $filePath, 'new.txt');
$this->assertSame('normal file content', $filesystemAdapter->read($storagePath));
}
public function testPutFile()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$uploadedFile = new UploadedFile($filePath, 'org.txt', null, null, true);
$storagePath = $filesystemAdapter->putFile('/', $uploadedFile);
$this->assertSame(44, strlen($storagePath)); // random 40 characters + ".txt"
$this->assertFileExists($filePath);
$filesystemAdapter->assertExists($storagePath);
}
public function testPutFileWithAbsoluteFilePath()
{
file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content');
$filesystemAdapter = new FilesystemAdapter($this->filesystem);
$storagePath = $filesystemAdapter->putFile('/', $filePath);
$this->assertSame(44, strlen($storagePath)); // random 40 characters + ".txt"
$filesystemAdapter->assertExists($storagePath);
}
}