-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadOnlyFilesystemAdapterTest.php
190 lines (148 loc) · 5.21 KB
/
ReadOnlyFilesystemAdapterTest.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
<?php
namespace League\Flysystem\ReadOnly;
use League\Flysystem\Config;
use League\Flysystem\FileAttributes;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToGeneratePublicUrl;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
use PHPUnit\Framework\TestCase;
use function ltrim;
class ReadOnlyFilesystemAdapterTest extends TestCase
{
/**
* @test
*/
public function can_perform_read_operations(): void
{
$adapter = $this->realAdapter();
$adapter->write('foo/bar.txt', 'content', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->assertTrue($adapter->fileExists('foo/bar.txt'));
$this->assertTrue($adapter->directoryExists('foo'));
$this->assertSame('content', $adapter->read('foo/bar.txt'));
$this->assertSame('content', \stream_get_contents($adapter->readStream('foo/bar.txt')));
$this->assertInstanceOf(FileAttributes::class, $adapter->visibility('foo/bar.txt'));
$this->assertInstanceOf(FileAttributes::class, $adapter->mimeType('foo/bar.txt'));
$this->assertInstanceOf(FileAttributes::class, $adapter->lastModified('foo/bar.txt'));
$this->assertInstanceOf(FileAttributes::class, $adapter->fileSize('foo/bar.txt'));
$this->assertCount(1, iterator_to_array($adapter->listContents('foo', true)));
}
/**
* @test
*/
public function cannot_write_stream(): void
{
$adapter = new ReadOnlyFilesystemAdapter($this->realAdapter());
$this->expectException(UnableToWriteFile::class);
// @phpstan-ignore-next-line
$adapter->writeStream('foo', 'content', new Config());
}
/**
* @test
*/
public function cannot_write(): void
{
$adapter = new ReadOnlyFilesystemAdapter($this->realAdapter());
$this->expectException(UnableToWriteFile::class);
$adapter->write('foo', 'content', new Config());
}
/**
* @test
*/
public function cannot_delete_file(): void
{
$adapter = $this->realAdapter();
$adapter->write('foo', 'content', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->expectException(UnableToDeleteFile::class);
$adapter->delete('foo');
}
/**
* @test
*/
public function cannot_delete_directory(): void
{
$adapter = $this->realAdapter();
$adapter->createDirectory('foo', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->expectException(UnableToDeleteDirectory::class);
$adapter->deleteDirectory('foo');
}
/**
* @test
*/
public function cannot_create_directory(): void
{
$adapter = new ReadOnlyFilesystemAdapter($this->realAdapter());
$this->expectException(UnableToCreateDirectory::class);
$adapter->createDirectory('foo', new Config());
}
/**
* @test
*/
public function cannot_set_visibility(): void
{
$adapter = $this->realAdapter();
$adapter->write('foo', 'content', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->expectException(UnableToSetVisibility::class);
$adapter->setVisibility('foo', 'private');
}
/**
* @test
*/
public function cannot_move(): void
{
$adapter = $this->realAdapter();
$adapter->write('foo', 'content', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->expectException(UnableToMoveFile::class);
$adapter->move('foo', 'bar', new Config());
}
/**
* @test
*/
public function cannot_copy(): void
{
$adapter = $this->realAdapter();
$adapter->write('foo', 'content', new Config());
$adapter = new ReadOnlyFilesystemAdapter($adapter);
$this->expectException(UnableToCopyFile::class);
$adapter->copy('foo', 'bar', new Config());
}
/**
* @test
*/
public function generating_a_public_url(): void
{
$adapter = new class() extends InMemoryFilesystemAdapter implements PublicUrlGenerator {
public function publicUrl(string $path, Config $config): string
{
return 'memory://' . ltrim($path, '/');
}
};
$readOnlyAdapter = new ReadOnlyFilesystemAdapter($adapter);
$url = $readOnlyAdapter->publicUrl('/path.txt', new Config());
self::assertEquals('memory://path.txt', $url);
}
/**
* @test
*/
public function failing_to_generate_a_public_url(): void
{
$adapter = new ReadOnlyFilesystemAdapter(new InMemoryFilesystemAdapter());
$this->expectException(UnableToGeneratePublicUrl::class);
$adapter->publicUrl('/path.txt', new Config());
}
private function realAdapter(): InMemoryFilesystemAdapter
{
return new InMemoryFilesystemAdapter();
}
}