Completed
Push — master ( 7de777...9be641 )
by
unknown
11:28
created

ImageGeneratorTest::testGenerate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Tests\Unit\Generator;
4
5
use League\Flysystem\Filesystem;
6
use League\Glide\Api\ApiInterface;
7
use League\Glide\Filesystem\FilesystemException;
8
use League\Glide\Server;
9
use MediaMonks\SonataMediaBundle\Generator\FilenameGeneratorInterface;
10
use MediaMonks\SonataMediaBundle\Generator\ImageGenerator;
11
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
12
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
13
use Mockery as m;
14
use org\bovigo\vfs\vfsStream;
15
16
class ImageGeneratorTest extends \PHPUnit_Framework_TestCase
17
{
18
    public function testGenerate()
19
    {
20
        $filesystem = m::mock(Filesystem::class);
21
        $filesystem->shouldReceive('has')->once()->andReturn(true);
22
        $filesystem->shouldReceive('read')->once()->andReturn('foo');
23
        $filesystem->shouldReceive('write')->withArgs(['image_handled.jpg', 'bar'])->once()->andReturn(true);
24
25
        $api = m::mock(ApiInterface::class);
26
        $api->shouldReceive('run')->once()->andReturn('bar');
27
28
        $server = m::mock(Server::class);
29
        $server->shouldReceive('getSource')->twice()->andReturn($filesystem);
30
        $server->shouldReceive('getCache')->once()->andReturn($filesystem);
31
        $server->shouldReceive('getApi')->once()->andReturn($api);
32
33
        $filenameGenerator = m::mock(FilenameGeneratorInterface::class);
34
        $filenameGenerator->shouldReceive('generate')->once()->andReturn('image_handled.jpg');
35
36
        $media = m::mock(MediaInterface::class);
37
        $media->shouldReceive('getImage')->once()->andReturn('image.jpg');
38
        $media->shouldReceive('getFocalPoint')->once()->andReturn('50-50');
39
40
        $parameters = new ImageParameterBag(400, 300);
41
        $imageGenerator = new ImageGenerator($server, $filenameGenerator);
42
        $filename = $imageGenerator->generate($media, $parameters);
43
44
        $this->assertEquals('image_handled.jpg', $filename);
45
    }
46
47
    public function testGetters()
48
    {
49
        $server = m::mock(Server::class);
50
        $filenameGenerator = m::mock(FilenameGeneratorInterface::class);
51
        $imageGenerator = new ImageGenerator(
52
            $server,
53
            $filenameGenerator,
54
            ['foo' => 'bar'],
55
            'fallback',
56
            'tmpPath',
57
            'tmpPrefix'
58
        );
59
        $this->assertEquals($server, $imageGenerator->getServer());
60
        $this->assertEquals($filenameGenerator, $imageGenerator->getFilenameGenerator());
61
        $this->assertEquals(['foo' => 'bar'], $imageGenerator->getDefaultImageParameters());
62
        $this->assertEquals('fallback', $imageGenerator->getFallbackImage());
63
        $this->assertEquals('tmpPath', $imageGenerator->getTmpPath());
64
        $this->assertEquals('tmpPrefix', $imageGenerator->getTmpPrefix());
65
    }
66
67
    public function testUnableToWriteTemporaryFile()
68
    {
69
        vfsStream::setup('/tmp');
70
        vfsStream::setQuota(1);
71
72
        $this->setExpectedException(FilesystemException::class);
73
74
        $filesystem = m::mock(Filesystem::class);
75
        $filesystem->shouldReceive('has')->once()->andReturn(false);
76
        $filesystem->shouldReceive('read')->once()->andReturn('foo');
77
        $filesystem->shouldReceive('write')->withArgs(['image_handled.jpg', 'bar'])->once()->andReturn(true);
78
79
        $api = m::mock(ApiInterface::class);
80
        $api->shouldReceive('run')->once()->andReturn('bar');
81
82
        $server = m::mock(Server::class);
83
        $server->shouldReceive('getSource')->twice()->andReturn($filesystem);
84
        $server->shouldReceive('getCache')->once()->andReturn($filesystem);
85
        $server->shouldReceive('getApi')->once()->andReturn($api);
86
87
        $filenameGenerator = m::mock(FilenameGeneratorInterface::class);
88
        $filenameGenerator->shouldReceive('generate')->once()->andReturn('image_handled.jpg');
89
90
        $media = m::mock(MediaInterface::class);
91
        $media->shouldReceive('getImage')->once()->andReturn('image.jpg');
92
        $media->shouldReceive('getFocalPoint')->once()->andReturn('50-50');
93
94
        $parameters = new ImageParameterBag(400, 300);
95
        $imageGenerator = new ImageGenerator($server, $filenameGenerator, [], null, '/tmp');
96
        $filename = $imageGenerator->generate($media, $parameters);
0 ignored issues
show
Unused Code introduced by
$filename is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
    }
98
99
    public function testUnableToWriteGeneratedImage()
100
    {
101
        $this->setExpectedException(FilesystemException::class);
102
103
        $source = m::mock(Filesystem::class);
104
        $source->shouldReceive('has')->once()->andReturn(true);
105
        $source->shouldReceive('read')->once()->andReturn('foo');
106
        $source->shouldReceive('write')->withArgs(['image_handled.jpg', 'bar'])->once()->andReturn(false);
107
108
        $cache = m::mock(Filesystem::class);
109
        $cache->shouldReceive('has')->once()->andReturn(false);
110
        $cache->shouldReceive('put')->once()->andReturn(false)->andThrow(\Exception::class, 'Unable to write');
111
112
        $api = m::mock(ApiInterface::class);
113
        $api->shouldReceive('run')->once()->andReturn('bar');
114
115
        $server = m::mock(Server::class);
116
        $server->shouldReceive('getSource')->twice()->andReturn($source);
117
        $server->shouldReceive('getCache')->once()->andReturn($cache);
118
        $server->shouldReceive('getApi')->once()->andReturn($api);
119
120
        $filenameGenerator = m::mock(FilenameGeneratorInterface::class);
121
        $filenameGenerator->shouldReceive('generate')->once()->andReturn('image_handled.jpg');
122
123
        $media = m::mock(MediaInterface::class);
124
        $media->shouldReceive('getImage')->once()->andReturn('image.jpg');
125
        $media->shouldReceive('getFocalPoint')->once()->andReturn('50-50');
126
127
        $parameters = new ImageParameterBag(400, 300);
128
        $imageGenerator = new ImageGenerator($server, $filenameGenerator);
129
        $filename = $imageGenerator->generate($media, $parameters);
130
131
        $this->assertEquals('image_handled.jpg', $filename);
132
    }
133
}
134