/
Filesystem.php
399 lines (336 loc) · 12.1 KB
/
Filesystem.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
namespace spec\Gaufrette;
use PHPSpec2\ObjectBehavior;
class Filesystem extends ObjectBehavior
{
/**
* @param \Gaufrette\Adapter $adapter
*/
function let($adapter)
{
$this->beConstructedWith($adapter);
}
function it_should_be_initializable()
{
$this->shouldBeAnInstanceOf('Gaufrette\Filesystem');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_gives_access_to_adapter($adapter)
{
$this->getAdapter()->shouldBe($adapter);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_check_is_file_exists_using_adapter($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(false);
$this->has('filename')->shouldReturn(true);
$this->has('otherFilename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_rename_file($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->exists('otherFilename')->shouldBeCalled()->willReturn(false);
$adapter->rename('filename', 'otherFilename')->shouldBeCalled()->willReturn(true);
$this->rename('filename', 'otherFilename')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_renamed_source_file_does_not_exist($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringRename('filename', 'otherFilename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_renamed_target_file_exists($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(true);
$this
->shouldThrow(new \Gaufrette\Exception\UnexpectedFile('otherFilename'))
->duringRename('filename', 'otherFilename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_rename_is_not_successful($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(false);
$adapter->rename('filename', 'otherFilename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not rename the "filename" key to "otherFilename".'))
->duringRename('filename', 'otherFilename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_get_file_object($adapter)
{
$adapter->exists('filename')->willReturn(true);
$this->get('filename')->shouldBeAnInstanceOf('Gaufrette\File');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_get_file_object_when_file_does_not_exist($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringGet('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_get_file_object_when_file_does_not_exist_but_can_be_created($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this->get('filename', true)->shouldBeAnInstanceOf('Gaufrette\File');
}
/**
* @param \spec\Gaufrette\Adapter $extendedAdapter
* @param \Gaufrette\File $file
*/
function it_should_delegate_file_creation_to_adapter_when_adapter_is_file_factory($extendedAdapter, $file)
{
$this->beConstructedWith($extendedAdapter);
$extendedAdapter->exists('filename')->willReturn(true);
$extendedAdapter->createFile('filename', $this)->willReturn($file);
$this->get('filename')->shouldBe($file);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_write_content_to_new_file($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(false);
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
$this->write('filename', 'some content to write')->shouldReturn(21);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_update_content_of_file($adapter)
{
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
$this->write('filename', 'some content to write', true)->shouldReturn(21);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_update_content_of_file_when_file_cannot_be_overwrite($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->write('filename', 'some content to write')->shouldNotBeCalled();
$this
->shouldThrow(new \InvalidArgumentException('The key "filename" already exists and can not be overwritten.'))
->duringWrite('filename', 'some content to write');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_write_is_not_successful($adapter)
{
$adapter->exists('filename')->willReturn(false);
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not write the "filename" key content.'))
->duringWrite('filename', 'some content to write');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_read_file($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->read('filename')->shouldBeCalled()->willReturn('Some content');
$this->read('filename')->shouldReturn('Some content');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_read_file_which_does_not_exist($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringRead('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_read_is_not_successful($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->read('filename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not read the "filename" key content.'))
->duringRead('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_delete_file($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->delete('filename')->shouldBeCalled()->willReturn(true);
$this->delete('filename')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_delete_file_which_does_not_exist($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringDelete('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_fail_when_delete_is_not_successful($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->delete('filename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not remove the "filename" key.'))
->duringDelete('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_get_all_keys($adapter)
{
$keys = array('filename', 'filename1', 'filename2');
$adapter->keys()->willReturn($keys);
$this->keys()->shouldReturn($keys);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_match_listed_keys_using_specified_pattern($adapter)
{
$keys = array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey');
$adapter->keys()->willReturn($keys);
$adapter->isDirectory(ANY_ARGUMENT)->willReturn(false);
$this->listKeys()->shouldReturn(
array(
'keys' => array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'),
'dirs' => array()
)
);
$this->listKeys('filename')->shouldReturn(
array(
'keys' => array('filename', 'filename1', 'filename2'),
'dirs' => array()
)
);
$this->listKeys('Key')->shouldReturn(
array(
'keys' => array('testKey', 'KeyTest'),
'dirs' => array()
)
);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_decide_which_key_is_dir_using_adapter($adapter)
{
$keys = array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey');
$adapter->keys()->willReturn($keys);
$adapter->isDirectory('filename')->willReturn(false);
$adapter->isDirectory('filename2')->willReturn(false);
$adapter->isDirectory('KeyTest')->willReturn(false);
$adapter->isDirectory('testkey')->willReturn(false);
$adapter->isDirectory('filename1')->willReturn(true);
$adapter->isDirectory('testKey')->willReturn(true);
$this->listKeys()->shouldReturn(
array(
'keys' => array('filename', 'filename2', 'KeyTest', 'testkey'),
'dirs' => array('filename1', 'testKey')
)
);
$this->listKeys('filename')->shouldReturn(
array(
'keys' => array('filename', 'filename2'),
'dirs' => array('filename1')
)
);
$this->listKeys('Key')->shouldReturn(
array(
'keys' => array('KeyTest'),
'dirs' => array('testKey')
)
);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_get_mtime_of_file_using_adapter($adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->mtime('filename')->willReturn(1234567);
$this->mtime('filename')->shouldReturn(1234567);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_get_mtime_of_file_which_does_not_exist($adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringMtime('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_calculate_file_checksum($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->read('filename')->willReturn('some content');
$this->checksum('filename')->shouldReturn(md5('some content'));
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_not_calculate_checksum_of_file_which_does_not_exist($adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringChecksum('filename');
}
/**
* @param \spec\Gaufrette\Adapter $extendedAdapter
*/
function it_should_delegate_checksum_calculation_to_adapter_when_adapter_is_checksum_calculator($extendedAdapter)
{
$this->beConstructedWith($extendedAdapter);
$extendedAdapter->exists('filename')->shouldBeCalled()->willReturn(true);
$extendedAdapter->read('filename')->shouldNotBeCalled();
$extendedAdapter->checksum('filename')->shouldBeCalled()->willReturn(12);
$this->checksum('filename')->shouldReturn(12);
}
}
interface Adapter extends \Gaufrette\Adapter,
\Gaufrette\Adapter\FileFactory,
\Gaufrette\Adapter\StreamFactory,
\Gaufrette\Adapter\ChecksumCalculator,
\Gaufrette\Adapter\MetadataSupporter
{}