Completed
Push — master ( 161dc8...90572a )
by
unknown
27:22 queued 10:00
created

IOServiceTest::loadBinaryFileByUriNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\IO\Tests;
10
11
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
12
use eZ\Publish\Core\IO\IOService;
13
use eZ\Publish\Core\IO\IOBinarydataHandler;
14
use eZ\Publish\Core\IO\IOMetadataHandler;
15
use eZ\Publish\Core\IO\Values\BinaryFile;
16
use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct;
17
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
18
use eZ\Publish\SPI\IO\MimeTypeDetector;
19
use PHPUnit\Framework\TestCase;
20
21
/**
22
 * Test case for IO Service.
23
 */
24
class IOServiceTest extends TestCase
25
{
26
    const PREFIX = 'test-prefix';
27
28
    /** @var IOService */
29
    protected $IOService;
30
31
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit\Framework\MockObject\MockObject */
32
    protected $metadataHandlerMock;
33
34
    /** @var \eZ\Publish\Core\IO\IOBinarydataHandler|\PHPUnit\Framework\MockObject\MockObject */
35
    protected $binarydataHandlerMock;
36
37
    /** @var MimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
38
    protected $mimeTypeDetectorMock;
39
40
    protected function setUp(): void
41
    {
42
        parent::setUp();
43
44
        $this->binarydataHandlerMock = $this->createMock(IOBinarydataHandler::class);
45
        $this->metadataHandlerMock = $this->createMock(IOMetadataHandler::class);
46
        $this->mimeTypeDetectorMock = $this->createMock(MimeTypeDetector::class);
47
48
        $this->IOService = new IOService(
49
            $this->metadataHandlerMock,
50
            $this->binarydataHandlerMock,
51
            $this->mimeTypeDetectorMock,
52
            array('prefix' => self::PREFIX)
53
        );
54
    }
55
56
    /**
57
     * Test creating new BinaryCreateStruct from uploaded file.
58
     *
59
     * @covers \eZ\Publish\Core\IO\IOService::newBinaryCreateStructFromUploadedFile
60
     */
61
    public function testNewBinaryCreateStructFromUploadedFile()
62
    {
63
        self::markTestSkipped('Test skipped as it seems to depend on php-cgi');
64
        $uploadTest = $this->getFileUploadTest();
0 ignored issues
show
Bug introduced by
The method getFileUploadTest() does not seem to exist on object<eZ\Publish\Core\IO\Tests\IOServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
        $result = $uploadTest->run(); // Fails because of unset cgi param and missing php-cgi exe
66
        // Params bellow makes the code execute but fails:
67
        //->run( null, array( 'cgi' => 'php' ) );
68
69
        if ($result->failureCount() > 0) {
70
            self::fail(
71
                'Failed file upload test, failureCount() > 0: ' .
72
                $this->expandFailureMessages($result->failures())
0 ignored issues
show
Bug introduced by
The method expandFailureMessages() does not seem to exist on object<eZ\Publish\Core\IO\Tests\IOServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
            );
74
        }
75
76
        if ($result->errorCount() > 0) {
77
            self::fail(
78
                'Failed file upload test, errorCount() > 0: ' .
79
                $this->expandFailureMessages($result->errors())
0 ignored issues
show
Bug introduced by
The method expandFailureMessages() does not seem to exist on object<eZ\Publish\Core\IO\Tests\IOServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            );
81
        }
82
83
        if ($result->skippedCount() > 0) {
84
            self::fail(
85
                'Failed file upload test, skippedCount() > 0: ' .
86
                $this->expandFailureMessages($result->skipped())
0 ignored issues
show
Bug introduced by
The method expandFailureMessages() does not seem to exist on object<eZ\Publish\Core\IO\Tests\IOServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
            );
88
        }
89
    }
90
91
    /**
92
     * @covers \eZ\Publish\Core\IO\IOService::newBinaryCreateStructFromUploadedFile
93
     */
94
    public function testNewBinaryCreateStructFromLocalFile()
95
    {
96
        $file = __FILE__;
97
98
        $this->mimeTypeDetectorMock
99
            ->expects($this->once())
100
            ->method('getFromPath')
101
            ->with($this->equalTo($file))
102
            ->will($this->returnValue('text/x-php'));
103
104
        $binaryCreateStruct = $this->getIOService()->newBinaryCreateStructFromLocalFile(
105
            $file
106
        );
107
108
        self::assertInstanceOf(BinaryFileCreateStruct::class, $binaryCreateStruct);
109
        self::assertNull($binaryCreateStruct->id);
110
        self::assertTrue(is_resource($binaryCreateStruct->inputStream));
111
        self::assertEquals(filesize(__FILE__), $binaryCreateStruct->size);
112
        self::assertEquals('text/x-php', $binaryCreateStruct->mimeType);
113
114
        return $binaryCreateStruct;
115
    }
116
117
    /**
118
     * @covers \eZ\Publish\Core\IO\IOService::createBinaryFile
119
     * @covers \eZ\Publish\Core\IO\IOService::buildSPIBinaryFileCreateStructObject
120
     * @covers \eZ\Publish\Core\IO\IOService::buildDomainBinaryFileObject
121
     * @depends testNewBinaryCreateStructFromLocalFile
122
     */
123
    public function testCreateBinaryFile(BinaryFileCreateStruct $createStruct)
124
    {
125
        $createStruct->id = 'my/path.php';
126
        $id = $this->getPrefixedUri($createStruct->id);
127
128
        $spiBinaryFile = new SPIBinaryFile();
129
        $spiBinaryFile->id = $id;
130
        $spiBinaryFile->uri = $id;
131
        $spiBinaryFile->size = filesize(__FILE__);
132
        $spiBinaryFile->mimeType = 'text/x-php';
133
134
        $this->binarydataHandlerMock
135
            ->expects($this->once())
136
            ->method('create')
137
            ->with(
138
                $this->callback(
139
                    function ($subject) use ($id) {
140
                        if (!$subject instanceof \eZ\Publish\SPI\IO\BinaryFileCreateStruct) {
141
                            return false;
142
                        }
143
144
                        return $subject->id == $id;
145
                    }
146
                )
147
            );
148
149
        $this->metadataHandlerMock
150
            ->expects($this->once())
151
            ->method('create')
152
            ->with($this->callback($this->getSPIBinaryFileCreateStructCallback($id)))
153
            ->will($this->returnValue($spiBinaryFile));
154
155
        $binaryFile = $this->IOService->createBinaryFile($createStruct);
156
        self::assertInstanceOf(BinaryFile::class, $binaryFile);
157
        self::assertEquals($createStruct->id, $binaryFile->id);
158
        self::assertEquals($createStruct->size, $binaryFile->size);
159
160
        return $binaryFile;
161
    }
162
163
    /**
164
     * @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile
165
     */
166
    public function testLoadBinaryFile()
167
    {
168
        $id = 'my/path.png';
169
        $spiId = $this->getPrefixedUri($id);
170
        $spiBinaryFile = new SPIBinaryFile();
171
        $spiBinaryFile->id = $spiId;
172
        $spiBinaryFile->size = 12345;
173
        $spiBinaryFile->mimeType = 'application/any';
174
        $spiBinaryFile->uri = $spiId;
175
176
        $this->metadataHandlerMock
177
            ->expects($this->once())
178
            ->method('load')
179
            ->with($spiId)
180
            ->will($this->returnValue($spiBinaryFile));
181
182
        $binaryFile = $this->getIOService()->loadBinaryFile($id);
183
        self::assertEquals($id, $binaryFile->id);
184
185
        return $binaryFile;
186
    }
187
188
    /**
189
     * @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile
190
     */
191
    public function testLoadBinaryFileNoMetadataUri()
192
    {
193
        $id = 'my/path.png';
194
        $spiId = $this->getPrefixedUri($id);
195
        $spiBinaryFile = new SPIBinaryFile();
196
        $spiBinaryFile->id = $spiId;
197
        $spiBinaryFile->size = 12345;
198
199
        $this->metadataHandlerMock
200
            ->expects($this->once())
201
            ->method('load')
202
            ->with($spiId)
203
            ->will($this->returnValue($spiBinaryFile));
204
205
        $this->binarydataHandlerMock
206
            ->expects($this->once())
207
            ->method('getUri')
208
            ->with($spiId)
209
            ->will($this->returnValue("/$spiId"));
210
211
        $binaryFile = $this->getIOService()->loadBinaryFile($id);
212
213
        $expectedBinaryFile = new BinaryFile(array('id' => $id, 'size' => 12345, 'uri' => "/$spiId"));
214
215
        self::assertEquals($expectedBinaryFile, $binaryFile);
216
217
        return $binaryFile;
218
    }
219
220
    /**
221
     * @covers \eZ\Publish\Core\IO\IOService::loadBinaryFile
222
     *
223
     * @return mixed Whatever loadBinaryFile returns
224
     */
225
    public function testLoadBinaryFileNotFound()
226
    {
227
        $this->expectException(BinaryFileNotFoundException::class);
228
229
        return $this->loadBinaryFileNotFound();
230
    }
231
232
    public function testLoadBinaryFileByUri()
233
    {
234
        $id = 'my/path.png';
235
        $spiId = $this->getPrefixedUri($id);
236
        $spiBinaryFile = new SPIBinaryFile();
237
        $spiBinaryFile->id = $spiId;
238
        $spiBinaryFile->size = 12345;
239
        $spiBinaryFile->mimeType = 'application/any';
240
        $spiBinaryFile->uri = $spiId;
241
242
        $this->binarydataHandlerMock
243
            ->expects($this->once())
244
            ->method('getIdFromUri')
245
            ->with($spiId)
246
            ->will($this->returnValue($spiId));
247
248
        $this->metadataHandlerMock
249
            ->expects($this->once())
250
            ->method('load')
251
            ->with($spiId)
252
            ->will($this->returnValue($spiBinaryFile));
253
254
        $binaryFile = $this->getIOService()->loadBinaryFileByUri($spiId);
255
        self::assertEquals($id, $binaryFile->id);
256
257
        return $binaryFile;
258
    }
259
260
    /**
261
     * @return mixed Whatever loadBinaryFileByUri returns
262
     */
263
    public function testLoadBinaryFileByUriNotFound()
264
    {
265
        $this->expectException(BinaryFileNotFoundException::class);
266
267
        return $this->loadBinaryFileByUriNotFound();
268
    }
269
270
    /**
271
     * @covers \eZ\Publish\Core\IO\IOService::getFileInputStream
272
     * @depends testCreateBinaryFile
273
     */
274
    public function testGetFileInputStream(BinaryFile $binaryFile)
0 ignored issues
show
Unused Code introduced by
The parameter $binaryFile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
275
    {
276
        self::markTestSkipped('Not implemented');
277
    }
278
279
    /**
280
     * @depends testLoadBinaryFile
281
     * @covers \eZ\Publish\Core\IO\IOService::getFileContents
282
     */
283
    public function testGetFileContents(BinaryFile $binaryFile)
284
    {
285
        $expectedContents = file_get_contents(__FILE__);
286
287
        $this->binarydataHandlerMock
288
            ->expects($this->once())
289
            ->method('getContents')
290
            ->with($this->equalTo($this->getPrefixedUri($binaryFile->id)))
291
            ->will($this->returnValue($expectedContents));
292
293
        self::assertEquals(
294
            $expectedContents,
295
            $this->getIOService()->getFileContents($binaryFile)
296
        );
297
    }
298
299
    /**
300
     * @depends testCreateBinaryFile
301
     * @covers \eZ\Publish\Core\IO\IOService::exists()
302
     */
303
    public function testExists(BinaryFile $binaryFile)
304
    {
305
        $this->metadataHandlerMock
306
            ->expects($this->once())
307
            ->method('exists')
308
            ->with($this->equalTo($this->getPrefixedUri($binaryFile->id)))
309
            ->will($this->returnValue(true));
310
311
        self::assertTrue(
312
            $this->getIOService()->exists(
313
                $binaryFile->id
314
            )
315
        );
316
    }
317
318
    /**
319
     * @covers \eZ\Publish\Core\IO\IOService::exists()
320
     */
321
    public function testExistsNot()
322
    {
323
        $this->metadataHandlerMock
324
            ->expects($this->once())
325
            ->method('exists')
326
            ->with($this->equalTo($this->getPrefixedUri(__METHOD__)))
327
            ->will($this->returnValue(false));
328
329
        self::assertFalse(
330
            $this->getIOService()->exists(
331
                __METHOD__
332
            )
333
        );
334
    }
335
336
    /**
337
     * @depends testCreateBinaryFile
338
     * @covers \eZ\Publish\Core\IO\IOService::getMimeType()
339
     */
340
    public function testGetMimeType(BinaryFile $binaryFile)
341
    {
342
        $this->metadataHandlerMock
343
            ->expects($this->once())
344
            ->method('getMimeType')
345
            ->with($this->equalTo($this->getPrefixedUri($binaryFile->id)))
346
            ->will($this->returnValue($binaryFile->mimeType));
347
348
        self::assertEquals(
349
            $binaryFile->mimeType,
350
            $this->getIOService()->getMimeType(
351
                $binaryFile->id
352
            )
353
        );
354
    }
355
356
    /**
357
     * @covers \eZ\Publish\Core\IO\IOService::deleteBinaryFile
358
     * @depends testCreateBinaryFile
359
     */
360
    public function testDeleteBinaryFile(BinaryFile $binaryFile)
361
    {
362
        $this->metadataHandlerMock
363
            ->expects($this->once())
364
            ->method('delete')
365
            ->with($this->equalTo($this->getPrefixedUri($binaryFile->id)));
366
367
        $this->binarydataHandlerMock
368
            ->expects($this->once())
369
            ->method('delete')
370
            ->with($this->equalTo($this->getPrefixedUri($binaryFile->id)));
371
372
        $this->getIOService()->deleteBinaryFile($binaryFile);
373
    }
374
375
    /**
376
     * @covers \eZ\Publish\Core\IO\IOService::deleteDirectory()
377
     */
378
    public function testDeleteDirectory()
379
    {
380
        $id = 'some/directory';
381
        $spiId = $this->getPrefixedUri($id);
382
383
        $this->binarydataHandlerMock
384
            ->expects($this->once())
385
            ->method('deleteDirectory')
386
            ->with($spiId);
387
388
        $this->metadataHandlerMock
389
            ->expects($this->once())
390
            ->method('deleteDirectory')
391
            ->with($spiId);
392
393
        $this->getIOService()->deleteDirectory('some/directory');
394
    }
395
396
    /**
397
     * @covers \eZ\Publish\Core\IO\IOService::deleteBinaryFile
398
     *
399
     * @return mixed Whatever deleteBinaryFile returned
400
     */
401
    public function testDeleteBinaryFileNotFound()
402
    {
403
        $this->expectException(BinaryFileNotFoundException::class);
404
405
        $this->deleteBinaryFileNotFound();
406
    }
407
408
    public function getPrefixedUri($uri)
409
    {
410
        return self::PREFIX . '/' . $uri;
411
    }
412
413
    /**
414
     * @return \eZ\Publish\Core\IO\IOService
415
     */
416
    protected function getIOService()
417
    {
418
        return $this->IOService;
419
    }
420
421
    /**
422
     * Asserts that the given $ioCreateStruct is of the right type and that id matches the expected value.
423
     *
424
     * @param $ioCreateStruct
425
     *
426
     * @return bool
427
     */
428
    private function getSPIBinaryFileCreateStructCallback($spiId)
429
    {
430
        return function ($subject) use ($spiId) {
431
            if (!$subject instanceof \eZ\Publish\SPI\IO\BinaryFileCreateStruct) {
432
                return false;
433
            }
434
435
            return $subject->id == $spiId;
436
        };
437
    }
438
439
    /**
440
     * @return bool|\eZ\Publish\Core\IO\Values\BinaryFile
441
     *
442
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
443
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
444
     */
445
    protected function loadBinaryFileNotFound()
446
    {
447
        $id = 'id.ext';
448
        $prefixedUri = $this->getPrefixedUri($id);
449
        $this->metadataHandlerMock
450
            ->expects($this->once())
451
            ->method('load')
452
            ->with($prefixedUri)
453
            ->will($this->throwException(new BinaryFileNotFoundException($prefixedUri)));
454
455
        return $this->getIOService()->loadBinaryFile($id);
456
    }
457
458
    protected function deleteBinaryFileNotFound(): void
459
    {
460
        $binaryFile = new BinaryFile(
461
            ['id' => __METHOD__]
462
        );
463
464
        $prefixedId = $this->getPrefixedUri($binaryFile->id);
465
        $this->metadataHandlerMock
466
            ->expects($this->once())
467
            ->method('delete')
468
            ->with($this->equalTo($prefixedId))
469
            ->will($this->throwException(new BinaryFileNotFoundException($prefixedId)));
470
471
        $this->getIOService()->deleteBinaryFile($binaryFile);
472
    }
473
474
    /**
475
     * @return bool|\eZ\Publish\Core\IO\Values\BinaryFile
476
     *
477
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
478
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
479
     */
480
    protected function loadBinaryFileByUriNotFound()
481
    {
482
        $id = 'my/path.png';
483
        $spiId = $this->getPrefixedUri($id);
484
485
        $this->binarydataHandlerMock
486
            ->expects($this->once())
487
            ->method('getIdFromUri')
488
            ->with($spiId)
489
            ->will($this->returnValue($spiId));
490
491
        $this->metadataHandlerMock
492
            ->expects($this->once())
493
            ->method('load')
494
            ->with($spiId)
495
            ->will($this->throwException(new BinaryFileNotFoundException($spiId)));
496
497
        return $this->getIOService()->loadBinaryFileByUri($spiId);
498
    }
499
}
500