Completed
Push — 6.7 ( 72ff63...39056b )
by
unknown
62:49 queued 48:08
created

BinaryLoaderTest::testFindBadPathRoot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the BinaryLoaderTest class.
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\Bundle\EzPublishCoreBundle\Tests\Imagine;
10
11
use eZ\Bundle\EzPublishCoreBundle\Imagine\BinaryLoader;
12
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
13
use eZ\Publish\Core\IO\Exception\InvalidBinaryFileIdException;
14
use eZ\Publish\Core\IO\Values\BinaryFile;
15
use eZ\Publish\Core\IO\Values\MissingBinaryFile;
16
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
17
use Liip\ImagineBundle\Model\Binary;
18
use PHPUnit_Framework_TestCase;
19
20
class BinaryLoaderTest extends PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockObject
24
     */
25
    private $ioService;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $extensionGuesser;
31
32
    /**
33
     * @var BinaryLoader
34
     */
35
    private $binaryLoader;
36
37
    protected function setUp()
38
    {
39
        parent::setUp();
40
        $this->ioService = $this->getMock('eZ\Publish\Core\IO\IOServiceInterface');
41
        $this->extensionGuesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface');
42
        $this->binaryLoader = new BinaryLoader($this->ioService, $this->extensionGuesser);
43
    }
44
45
    /**
46
     * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
47
     */
48 View Code Duplication
    public function testFindNotFound()
49
    {
50
        $path = 'something.jpg';
51
        $this->ioService
52
            ->expects($this->once())
53
            ->method('loadBinaryFile')
54
            ->with($path)
55
            ->will($this->throwException(new NotFoundException('foo', 'bar')));
56
57
        $this->binaryLoader->find($path);
58
    }
59
60
    /**
61
     * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
62
     */
63 View Code Duplication
    public function testFindMissing()
64
    {
65
        $path = 'something.jpg';
66
        $this->ioService
67
            ->expects($this->once())
68
            ->method('loadBinaryFile')
69
            ->with($path)
70
            ->will($this->returnValue(new MissingBinaryFile()));
71
72
        $this->binaryLoader->find($path);
73
    }
74
75
    public function testFindBadPathRoot()
76
    {
77
        $path = 'var/site/storage/images/1/2/3/123-name/name.png';
78
        $this->ioService
79
            ->expects($this->once())
80
            ->method('loadBinaryFile')
81
            ->with($path)
82
            ->will($this->throwException(new InvalidBinaryFileIdException($path)));
83
84
        try {
85
            $this->binaryLoader->find($path);
86
        } catch (NotLoadableException $e) {
87
            $this->assertContains(
88
                "Suggested value: '1/2/3/123-name/name.png'",
89
                $e->getMessage()
90
            );
91
        }
92
    }
93
94
    public function testFind()
95
    {
96
        $path = 'something.jpg';
97
        $mimeType = 'foo/mime-type';
98
        $content = 'some content';
99
        $binaryFile = new BinaryFile(array('id' => $path));
100
        $this->ioService
101
            ->expects($this->once())
102
            ->method('loadBinaryFile')
103
            ->with($path)
104
            ->will($this->returnValue($binaryFile));
105
106
        $format = 'jpg';
107
        $this->extensionGuesser
108
            ->expects($this->once())
109
            ->method('guess')
110
            ->with($mimeType)
111
            ->will($this->returnValue($format));
112
113
        $this->ioService
114
            ->expects($this->once())
115
            ->method('getFileContents')
116
            ->with($binaryFile)
117
            ->will($this->returnValue($content));
118
119
        $this->ioService
120
            ->expects($this->once())
121
            ->method('getMimeType')
122
            ->with($binaryFile->id)
123
            ->will($this->returnValue($mimeType));
124
125
        $expected = new Binary($content, $mimeType, $format);
126
        $this->assertEquals($expected, $this->binaryLoader->find($path));
127
    }
128
}
129