1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Gaufrette\Adapter\Azure; |
4
|
|
|
|
5
|
|
|
use MicrosoftAzure\Storage\Blob\Models\Blob; |
6
|
|
|
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions; |
7
|
|
|
use PhpSpec\ObjectBehavior; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use WindowsAzure\Common\ServiceException; |
10
|
|
|
|
11
|
|
|
class BlobStorageSpec extends ObjectBehavior |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
15
|
|
|
*/ |
16
|
|
|
public function let($blobProxyFactory) |
17
|
|
|
{ |
18
|
|
|
$this->beConstructedWith($blobProxyFactory, 'containerName'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function it_should_be_initializable() |
22
|
|
|
{ |
23
|
|
|
$this->shouldHaveType('Gaufrette\Adapter\Azure\BlobStorage'); |
24
|
|
|
$this->shouldHaveType('Gaufrette\Adapter'); |
25
|
|
|
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
30
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
31
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Models\GetBlobResult $getBlobResult |
32
|
|
|
*/ |
33
|
|
|
public function it_should_read_file($blobProxyFactory, $blobProxy, $getBlobResult) |
34
|
|
|
{ |
35
|
|
|
$getBlobResult |
36
|
|
|
->getContentStream() |
37
|
|
|
->shouldBeCalled() |
38
|
|
|
//azure blob content is handled as stream so we need to fake it |
39
|
|
|
->willReturn(fopen('data://text/plain,some content','r')); |
40
|
|
|
|
41
|
|
|
$blobProxy |
42
|
|
|
->getBlob('containerName', 'filename') |
43
|
|
|
->shouldBeCalled() |
44
|
|
|
->willReturn($getBlobResult); |
45
|
|
|
|
46
|
|
|
$blobProxyFactory |
47
|
|
|
->create() |
48
|
|
|
->shouldBeCalled() |
49
|
|
|
->willReturn($blobProxy); |
50
|
|
|
|
51
|
|
|
$this->read('filename')->shouldReturn('some content'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
56
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function it_should_return_false_when_cannot_read($blobProxyFactory, $blobProxy) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$blobProxy |
61
|
|
|
->getBlob('containerName', 'filename') |
62
|
|
|
->shouldBeCalled() |
63
|
|
|
->willThrow(new ServiceException(500)); |
64
|
|
|
|
65
|
|
|
$blobProxyFactory |
66
|
|
|
->create() |
67
|
|
|
->shouldBeCalled() |
68
|
|
|
->willReturn($blobProxy); |
69
|
|
|
|
70
|
|
|
$this->read('filename')->shouldReturn(false); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
75
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function it_should_not_mask_exception_when_read($blobProxyFactory, $blobProxy) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$blobProxy |
80
|
|
|
->getBlob('containerName', 'filename') |
81
|
|
|
->shouldBeCalled() |
82
|
|
|
->willThrow(new \RuntimeException('read')); |
83
|
|
|
|
84
|
|
|
$blobProxyFactory |
85
|
|
|
->create() |
86
|
|
|
->shouldBeCalled() |
87
|
|
|
->willReturn($blobProxy); |
88
|
|
|
|
89
|
|
|
$this->shouldThrow(new \RuntimeException('read'))->duringRead('filename'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
94
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function it_should_rename_file($blobProxyFactory, $blobProxy) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$blobProxy |
99
|
|
|
->copyBlob('containerName', 'filename2', 'containerName', 'filename1') |
100
|
|
|
->shouldBeCalled(); |
101
|
|
|
|
102
|
|
|
$blobProxy |
103
|
|
|
->deleteBlob('containerName', 'filename1') |
104
|
|
|
->shouldBeCalled(); |
105
|
|
|
|
106
|
|
|
$blobProxyFactory |
107
|
|
|
->create() |
108
|
|
|
->shouldBeCalled() |
109
|
|
|
->willReturn($blobProxy); |
110
|
|
|
|
111
|
|
|
$this->rename('filename1', 'filename2')->shouldReturn(true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
116
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function it_should_return_false_when_cannot_rename($blobProxyFactory, $blobProxy) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$blobProxy |
121
|
|
|
->copyBlob('containerName', 'filename2', 'containerName', 'filename1') |
122
|
|
|
->shouldBeCalled() |
123
|
|
|
->willThrow(new ServiceException(500)); |
124
|
|
|
|
125
|
|
|
$blobProxyFactory |
126
|
|
|
->create() |
127
|
|
|
->shouldBeCalled() |
128
|
|
|
->willReturn($blobProxy); |
129
|
|
|
|
130
|
|
|
$this->rename('filename1', 'filename2')->shouldReturn(false); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
135
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function it_should_not_mask_exception_when_rename($blobProxyFactory, $blobProxy) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$blobProxy |
140
|
|
|
->copyBlob('containerName', 'filename2', 'containerName', 'filename1') |
141
|
|
|
->shouldBeCalled() |
142
|
|
|
->willThrow(new \RuntimeException('rename')); |
143
|
|
|
|
144
|
|
|
$blobProxyFactory |
145
|
|
|
->create() |
146
|
|
|
->shouldBeCalled() |
147
|
|
|
->willReturn($blobProxy); |
148
|
|
|
|
149
|
|
|
$this->shouldThrow(new \RuntimeException('rename'))->duringRename('filename1', 'filename2'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
154
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function it_should_write_file($blobProxyFactory, $blobProxy) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$blobProxy |
159
|
|
|
->createBlockBlob('containerName', 'filename', 'some content', Argument::type(CreateBlobOptions::class)) |
160
|
|
|
->shouldBeCalled(); |
161
|
|
|
|
162
|
|
|
$blobProxyFactory |
163
|
|
|
->create() |
164
|
|
|
->shouldBeCalled() |
165
|
|
|
->willReturn($blobProxy); |
166
|
|
|
|
167
|
|
|
$this->write('filename', 'some content')->shouldReturn(12); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
172
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function it_should_return_false_when_cannot_write($blobProxyFactory, $blobProxy) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$blobProxy |
177
|
|
|
->createBlockBlob('containerName', 'filename', 'some content', Argument::type(CreateBlobOptions::class)) |
178
|
|
|
->willThrow(new ServiceException(500)); |
179
|
|
|
|
180
|
|
|
$blobProxyFactory |
181
|
|
|
->create() |
182
|
|
|
->shouldBeCalled() |
183
|
|
|
->willReturn($blobProxy); |
184
|
|
|
|
185
|
|
|
$this->write('filename', 'some content')->shouldReturn(false); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
190
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function it_should_not_mask_exception_when_write($blobProxyFactory, $blobProxy) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$blobProxy |
195
|
|
|
->createBlockBlob('containerName', 'filename', 'some content', Argument::type(CreateBlobOptions::class)) |
196
|
|
|
->willThrow(new \RuntimeException('write')); |
197
|
|
|
|
198
|
|
|
$blobProxyFactory |
199
|
|
|
->create() |
200
|
|
|
->shouldBeCalled() |
201
|
|
|
->willReturn($blobProxy); |
202
|
|
|
|
203
|
|
|
$this->shouldThrow(new \RuntimeException('write'))->duringWrite('filename', 'some content'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
208
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
209
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Models\GetBlobPropertiesResult $getBlobPropertiesResult |
210
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Models\BlobProperties $blobProperties |
211
|
|
|
*/ |
212
|
|
|
public function it_should_get_file_mtime($blobProxyFactory, $blobProxy, $getBlobPropertiesResult, $blobProperties) |
213
|
|
|
{ |
214
|
|
|
$blobProxyFactory |
215
|
|
|
->create() |
216
|
|
|
->shouldBeCalled() |
217
|
|
|
->willReturn($blobProxy); |
218
|
|
|
|
219
|
|
|
$blobProxy |
220
|
|
|
->getBlobProperties('containerName', 'filename') |
221
|
|
|
->shouldBeCalled() |
222
|
|
|
->willReturn($getBlobPropertiesResult); |
223
|
|
|
|
224
|
|
|
$getBlobPropertiesResult |
225
|
|
|
->getProperties() |
226
|
|
|
->shouldBeCalled() |
227
|
|
|
->willReturn($blobProperties); |
228
|
|
|
|
229
|
|
|
$blobProperties |
230
|
|
|
->getLastModified() |
231
|
|
|
->shouldBeCalled() |
232
|
|
|
->willReturn(new \DateTime('1987-12-28 20:00:00')); |
233
|
|
|
|
234
|
|
|
$this->mtime('filename')->shouldReturn(strtotime('1987-12-28 20:00:00')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
239
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
240
|
|
|
*/ |
241
|
|
|
public function it_should_return_false_when_cannot_mtime($blobProxyFactory, $blobProxy) |
242
|
|
|
{ |
243
|
|
|
$blobProxyFactory |
244
|
|
|
->create() |
245
|
|
|
->shouldBeCalled() |
246
|
|
|
->willReturn($blobProxy); |
247
|
|
|
|
248
|
|
|
$blobProxy |
249
|
|
|
->getBlobProperties('containerName', 'filename') |
250
|
|
|
->shouldBeCalled() |
251
|
|
|
->willThrow(new ServiceException(500)); |
252
|
|
|
|
253
|
|
|
$this->mtime('filename')->shouldReturn(false); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
258
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function it_should_not_mask_exception_when_get_mtime($blobProxyFactory, $blobProxy) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$blobProxyFactory |
263
|
|
|
->create() |
264
|
|
|
->shouldBeCalled() |
265
|
|
|
->willReturn($blobProxy); |
266
|
|
|
|
267
|
|
|
$blobProxy |
268
|
|
|
->getBlobProperties('containerName', 'filename') |
269
|
|
|
->shouldBeCalled() |
270
|
|
|
->willThrow(new \RuntimeException('mtime')); |
271
|
|
|
|
272
|
|
|
$this->shouldThrow(new \RuntimeException('mtime'))->duringMtime('filename'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
277
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
278
|
|
|
*/ |
279
|
|
|
public function it_should_delete_file($blobProxyFactory, $blobProxy) |
280
|
|
|
{ |
281
|
|
|
$blobProxyFactory |
282
|
|
|
->create() |
283
|
|
|
->shouldBeCalled() |
284
|
|
|
->willReturn($blobProxy); |
285
|
|
|
|
286
|
|
|
$blobProxy |
287
|
|
|
->deleteBlob('containerName', 'filename') |
288
|
|
|
->shouldBeCalled(); |
289
|
|
|
|
290
|
|
|
$this->delete('filename')->shouldReturn(true); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
295
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
296
|
|
|
*/ |
297
|
|
View Code Duplication |
public function it_should_return_false_when_cannot_delete_file($blobProxyFactory, $blobProxy) |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$blobProxyFactory |
300
|
|
|
->create() |
301
|
|
|
->shouldBeCalled() |
302
|
|
|
->willReturn($blobProxy); |
303
|
|
|
|
304
|
|
|
$blobProxy |
305
|
|
|
->deleteBlob('containerName', 'filename') |
306
|
|
|
->shouldBeCalled() |
307
|
|
|
->willThrow(new ServiceException(500)); |
308
|
|
|
|
309
|
|
|
$this->delete('filename')->shouldReturn(false); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
314
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function it_should_not_mask_exception_when_delete($blobProxyFactory, $blobProxy) |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
$blobProxyFactory |
319
|
|
|
->create() |
320
|
|
|
->shouldBeCalled() |
321
|
|
|
->willReturn($blobProxy); |
322
|
|
|
|
323
|
|
|
$blobProxy |
324
|
|
|
->deleteBlob('containerName', 'filename') |
325
|
|
|
->shouldBeCalled() |
326
|
|
|
->willThrow(new \RuntimeException('delete')); |
327
|
|
|
|
328
|
|
|
$this->shouldThrow(new \RuntimeException('delete'))->duringDelete('filename'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
333
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
334
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Models\ListBlobsResult $listBlobResult |
335
|
|
|
*/ |
336
|
|
|
public function it_should_get_keys($blobProxyFactory, $blobProxy, $listBlobResult) |
337
|
|
|
{ |
338
|
|
|
$fileNames = array('aaa', 'aaa/filename', 'filename1', 'filename2'); |
339
|
|
|
$blobs = array(); |
340
|
|
|
foreach ($fileNames as $fileName) { |
341
|
|
|
$blob = new Blob(); |
342
|
|
|
$blob->setName($fileName); |
343
|
|
|
$blobs[] = $blob; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$blobProxyFactory |
347
|
|
|
->create() |
348
|
|
|
->shouldBeCalled() |
349
|
|
|
->willReturn($blobProxy); |
350
|
|
|
|
351
|
|
|
$blobProxy |
352
|
|
|
->listBlobs('containerName') |
353
|
|
|
->shouldBeCalled() |
354
|
|
|
->willReturn($listBlobResult); |
355
|
|
|
|
356
|
|
|
$listBlobResult |
357
|
|
|
->getBlobs() |
358
|
|
|
->shouldBeCalled() |
359
|
|
|
->willReturn($blobs); |
360
|
|
|
|
361
|
|
|
$this->keys()->shouldReturn(array('aaa', 'aaa/filename', 'filename1', 'filename2')); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
366
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
367
|
|
|
*/ |
368
|
|
|
public function it_should_not_mask_exception_when_get_keys($blobProxyFactory, $blobProxy) |
369
|
|
|
{ |
370
|
|
|
$blobProxyFactory |
371
|
|
|
->create() |
372
|
|
|
->shouldBeCalled() |
373
|
|
|
->willReturn($blobProxy); |
374
|
|
|
|
375
|
|
|
$blobProxy |
376
|
|
|
->listBlobs('containerName') |
377
|
|
|
->shouldBeCalled() |
378
|
|
|
->willThrow(new \RuntimeException('keys')); |
379
|
|
|
|
380
|
|
|
$this->shouldThrow(new \RuntimeException('keys'))->duringKeys(); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
385
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
386
|
|
|
*/ |
387
|
|
|
public function it_should_create_container($blobProxyFactory, $blobProxy) |
388
|
|
|
{ |
389
|
|
|
$blobProxyFactory |
390
|
|
|
->create() |
391
|
|
|
->shouldBeCalled() |
392
|
|
|
->willReturn($blobProxy); |
393
|
|
|
|
394
|
|
|
$blobProxy |
395
|
|
|
->createContainer('containerName', null) |
396
|
|
|
->shouldBeCalled(); |
397
|
|
|
|
398
|
|
|
$this->createContainer('containerName'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @param \Gaufrette\Adapter\Azure\BlobProxyFactoryInterface $blobProxyFactory |
403
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Internal\IBlob $blobProxy |
404
|
|
|
*/ |
405
|
|
View Code Duplication |
public function it_should_fail_when_cannot_create_container($blobProxyFactory, $blobProxy) |
|
|
|
|
406
|
|
|
{ |
407
|
|
|
$blobProxyFactory |
408
|
|
|
->create() |
409
|
|
|
->shouldBeCalled() |
410
|
|
|
->willReturn($blobProxy); |
411
|
|
|
|
412
|
|
|
$blobProxy |
413
|
|
|
->createContainer('containerName', null) |
414
|
|
|
->shouldBeCalled() |
415
|
|
|
->willThrow(new ServiceException(500)); |
416
|
|
|
|
417
|
|
|
$this->shouldThrow(new \RuntimeException('Failed to create the configured container "containerName": ().', null))->duringCreateContainer('containerName'); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
} |
421
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.