1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Spiral Framework package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Files; |
13
|
|
|
|
14
|
|
|
use Spiral\Files\Exception\FileNotFoundException; |
15
|
|
|
use Spiral\Files\Exception\FilesException; |
16
|
|
|
use Spiral\Files\Files; |
17
|
|
|
use Spiral\Files\FilesInterface; |
18
|
|
|
|
19
|
|
|
class IOTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$files = new Files(); |
24
|
|
|
$files->ensureDirectory(self::FIXTURE_DIRECTORY, FilesInterface::RUNTIME); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function tearDown(): void |
28
|
|
|
{ |
29
|
|
|
$files = new Files(); |
30
|
|
|
$files->deleteDirectory(self::FIXTURE_DIRECTORY, true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testWrite(): void |
34
|
|
|
{ |
35
|
|
|
$files = new Files(); |
36
|
|
|
|
37
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
38
|
|
|
$this->assertFalse($files->exists($filename)); |
39
|
|
|
|
40
|
|
|
$files->write($filename, 'some-data'); |
41
|
|
|
$this->assertTrue($files->exists($filename)); |
42
|
|
|
|
43
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testWriteAndEnsureDirectory(): void |
47
|
|
|
{ |
48
|
|
|
$files = new Files(); |
49
|
|
|
|
50
|
|
|
$directory = self::FIXTURE_DIRECTORY . '/directory/abc/'; |
51
|
|
|
$filename = $directory . 'test.txt'; |
52
|
|
|
|
53
|
|
|
$this->assertFalse($files->exists($directory)); |
54
|
|
|
$this->assertFalse($files->exists($filename)); |
55
|
|
|
|
56
|
|
|
$this->assertFalse($files->isDirectory($directory)); |
57
|
|
|
|
58
|
|
|
$files->write($filename, 'some-data', FilesInterface::READONLY, true); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($files->isDirectory($directory)); |
61
|
|
|
$this->assertTrue($files->exists($filename)); |
62
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testRead(): void |
66
|
|
|
{ |
67
|
|
|
$files = new Files(); |
68
|
|
|
|
69
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
70
|
|
|
$this->assertFalse($files->exists($filename)); |
71
|
|
|
|
72
|
|
|
$files->write($filename, 'some-data'); |
73
|
|
|
$this->assertTrue($files->exists($filename)); |
74
|
|
|
|
75
|
|
|
$this->assertSame('some-data', $files->read($filename)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testReadMissingFile(): void |
79
|
|
|
{ |
80
|
|
|
$this->expectExceptionMessageMatches("/File '.*test.txt' not found/"); |
81
|
|
|
$this->expectException(FileNotFoundException::class); |
82
|
|
|
|
83
|
|
|
$files = new Files(); |
84
|
|
|
|
85
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
86
|
|
|
$this->assertFalse($files->exists($filename)); |
87
|
|
|
|
88
|
|
|
$files->read($filename); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testWriteForbidden(): void |
92
|
|
|
{ |
93
|
|
|
$this->expectException(FilesException::class); |
94
|
|
|
|
95
|
|
|
$files = new Files(); |
96
|
|
|
$files->write(self::FIXTURE_DIRECTORY, 'data'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetPermissionsException(): void |
100
|
|
|
{ |
101
|
|
|
$this->expectException(FileNotFoundException::class); |
102
|
|
|
|
103
|
|
|
$files = new Files(); |
104
|
|
|
$files->getPermissions(self::FIXTURE_DIRECTORY . '/missing'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testAppend(): void |
108
|
|
|
{ |
109
|
|
|
$files = new Files(); |
110
|
|
|
|
111
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
112
|
|
|
$this->assertFalse($files->exists($filename)); |
113
|
|
|
|
114
|
|
|
$files->append($filename, 'some-data'); |
115
|
|
|
$this->assertTrue($files->exists($filename)); |
116
|
|
|
|
117
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
118
|
|
|
|
119
|
|
|
$files->append($filename, ';other-data'); |
120
|
|
|
$this->assertSame('some-data;other-data', file_get_contents($filename)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testAppendEnsureDirectory(): void |
124
|
|
|
{ |
125
|
|
|
$files = new Files(); |
126
|
|
|
|
127
|
|
|
$directory = self::FIXTURE_DIRECTORY . '/directory/abc/'; |
128
|
|
|
$filename = $directory . 'test.txt'; |
129
|
|
|
|
130
|
|
|
$this->assertFalse($files->exists($directory)); |
131
|
|
|
$this->assertFalse($files->exists($filename)); |
132
|
|
|
|
133
|
|
|
$this->assertFalse($files->isDirectory($directory)); |
134
|
|
|
|
135
|
|
|
$files->append($filename, 'some-data', null, true); |
136
|
|
|
|
137
|
|
|
$this->assertTrue($files->isDirectory($directory)); |
138
|
|
|
$this->assertTrue($files->exists($filename)); |
139
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
140
|
|
|
|
141
|
|
|
$files->append($filename, ';other-data', null, true); |
142
|
|
|
$this->assertSame('some-data;other-data', file_get_contents($filename)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testTouch(): void |
146
|
|
|
{ |
147
|
|
|
$files = new Files(); |
148
|
|
|
|
149
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
150
|
|
|
|
151
|
|
|
$this->assertFalse($files->exists($filename)); |
152
|
|
|
$files->touch($filename); |
153
|
|
|
$this->assertTrue($files->exists($filename)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testDelete(): void |
157
|
|
|
{ |
158
|
|
|
$files = new Files(); |
159
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
160
|
|
|
|
161
|
|
|
$this->assertFalse($files->exists($filename)); |
162
|
|
|
|
163
|
|
|
$files->touch($filename); |
164
|
|
|
$this->assertTrue($files->exists($filename)); |
165
|
|
|
|
166
|
|
|
$files->delete($filename); |
167
|
|
|
$this->assertFalse($files->exists($filename)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testDeleteMissingFile(): void |
171
|
|
|
{ |
172
|
|
|
$files = new Files(); |
173
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
174
|
|
|
|
175
|
|
|
$this->assertFalse($files->exists($filename)); |
176
|
|
|
$files->delete($filename); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testCopy(): void |
180
|
|
|
{ |
181
|
|
|
$files = new Files(); |
182
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
183
|
|
|
$destination = self::FIXTURE_DIRECTORY . '/new.txt'; |
184
|
|
|
|
185
|
|
|
$this->assertFalse($files->exists($filename)); |
186
|
|
|
$files->write($filename, 'some-data'); |
187
|
|
|
|
188
|
|
|
$this->assertTrue($files->exists($filename)); |
189
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
190
|
|
|
|
191
|
|
|
$this->assertFalse($files->exists($destination)); |
192
|
|
|
|
193
|
|
|
$this->assertTrue($files->copy($filename, $destination)); |
194
|
|
|
$this->assertTrue($files->exists($destination)); |
195
|
|
|
$this->assertTrue($files->exists($filename)); |
196
|
|
|
|
197
|
|
|
$this->assertSame(file_get_contents($filename), file_get_contents($destination)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testCopyMissingFile(): void |
201
|
|
|
{ |
202
|
|
|
$this->expectExceptionMessageMatches("/File '.*test.txt' not found/"); |
203
|
|
|
$this->expectException(FileNotFoundException::class); |
204
|
|
|
|
205
|
|
|
$files = new Files(); |
206
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
207
|
|
|
$destination = self::FIXTURE_DIRECTORY . '/new.txt'; |
208
|
|
|
|
209
|
|
|
$this->assertFalse($files->exists($filename)); |
210
|
|
|
$files->copy($filename, $destination); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testMove(): void |
214
|
|
|
{ |
215
|
|
|
$files = new Files(); |
216
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
217
|
|
|
$destination = self::FIXTURE_DIRECTORY . '/new.txt'; |
218
|
|
|
|
219
|
|
|
$this->assertFalse($files->exists($filename)); |
220
|
|
|
$files->write($filename, 'some-data'); |
221
|
|
|
|
222
|
|
|
$this->assertTrue($files->exists($filename)); |
223
|
|
|
$this->assertSame('some-data', file_get_contents($filename)); |
224
|
|
|
|
225
|
|
|
$this->assertFalse($files->exists($destination)); |
226
|
|
|
|
227
|
|
|
$this->assertTrue($files->move($filename, $destination)); |
228
|
|
|
$this->assertTrue($files->exists($destination)); |
229
|
|
|
$this->assertFalse($files->exists($filename)); |
230
|
|
|
|
231
|
|
|
$this->assertSame('some-data', file_get_contents($destination)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testMoveMissingFile(): void |
235
|
|
|
{ |
236
|
|
|
$this->expectExceptionMessageMatches("/File '.*test.txt' not found/"); |
237
|
|
|
$this->expectException(FileNotFoundException::class); |
238
|
|
|
|
239
|
|
|
$files = new Files(); |
240
|
|
|
$filename = self::FIXTURE_DIRECTORY . '/test.txt'; |
241
|
|
|
$destination = self::FIXTURE_DIRECTORY . '/new.txt'; |
242
|
|
|
|
243
|
|
|
$this->assertFalse($files->exists($filename)); |
244
|
|
|
$files->move($filename, $destination); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|