SimpleResizerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testResize() 0 24 1
A testGetBox() 0 18 1
A getBoxSettings() 0 16 1
A testResizeWithNoWidthAndHeight() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\Resizer;
15
16
use Gaufrette\Adapter\InMemory;
17
use Gaufrette\File;
18
use Gaufrette\Filesystem;
19
use Imagine\Image\Box;
20
use Imagine\Image\ImageInterface;
21
use Imagine\Image\ImagineInterface;
22
use PHPUnit\Framework\TestCase;
23
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
24
use Sonata\MediaBundle\Model\MediaInterface;
25
use Sonata\MediaBundle\Resizer\SimpleResizer;
26
27
class SimpleResizerTest extends TestCase
28
{
29
    public function testResizeWithNoWidthAndHeight(): void
30
    {
31
        $this->expectException(\RuntimeException::class);
32
33
        $adapter = $this->createMock(ImagineInterface::class);
34
        $media = $this->createMock(MediaInterface::class);
35
        $metadata = $this->createMock(MetadataBuilderInterface::class);
36
        $file = $this->createMock(File::class);
37
38
        $resizer = new SimpleResizer($adapter, 'foo', $metadata);
39
        $resizer->resize($media, $file, $file, 'bar', []);
40
    }
41
42
    public function testResize(): void
43
    {
44
        $image = $this->createMock(ImageInterface::class);
45
        $image->expects($this->once())->method('thumbnail')->willReturn($image);
46
        $image->expects($this->once())->method('get')->willReturn(file_get_contents(__DIR__.'/../fixtures/logo.png'));
47
48
        $adapter = $this->createMock(ImagineInterface::class);
49
        $adapter->method('load')->willReturn($image);
50
51
        $media = $this->createMock(MediaInterface::class);
52
        $media->expects($this->exactly(2))->method('getBox')->willReturn(new Box(535, 132));
53
54
        $filesystem = new Filesystem(new InMemory());
55
        $in = $filesystem->get('in', true);
56
        $in->setContent(file_get_contents(__DIR__.'/../fixtures/logo.png'));
57
58
        $out = $filesystem->get('out', true);
59
60
        $metadata = $this->createMock(MetadataBuilderInterface::class);
61
        $metadata->expects($this->once())->method('get')->willReturn([]);
62
63
        $resizer = new SimpleResizer($adapter, 'outbound', $metadata);
64
        $resizer->resize($media, $in, $out, 'bar', ['height' => null, 'width' => 90, 'quality' => 100]);
65
    }
66
67
    /**
68
     * @dataProvider getBoxSettings
69
     */
70
    public function testGetBox(string $mode, array $settings, Box $mediaSize, Box $result): void
71
    {
72
        $adapter = $this->createMock(ImagineInterface::class);
73
74
        $media = $this->createMock(MediaInterface::class);
75
        $media->expects($this->exactly(2))->method('getBox')->willReturn($mediaSize);
76
77
        $metadata = $this->createMock(MetadataBuilderInterface::class);
78
79
        $resizer = new SimpleResizer($adapter, $mode, $metadata);
80
81
        $box = $resizer->getBox($media, $settings);
82
83
        $this->assertInstanceOf(Box::class, $box);
84
85
        $this->assertSame($result->getWidth(), $box->getWidth());
86
        $this->assertSame($result->getHeight(), $box->getHeight());
87
    }
88
89
    public static function getBoxSettings(): array
90
    {
91
        return [
92
            ['inset', ['width' => 90, 'height' => 90], new Box(100, 120), new Box(75, 90)],
93
            ['inset', ['width' => 90, 'height' => 90], new Box(50, 50), new Box(90, 90)],
94
            ['inset', ['width' => 90, 'height' => null], new Box(50, 50), new Box(90, 90)],
95
            ['inset', ['width' => 90, 'height' => null], new Box(567, 200), new Box(90, 32)],
96
            ['inset', ['width' => 100, 'height' => 100], new Box(567, 200), new Box(100, 35)],
97
98
            ['outbound', ['width' => 90, 'height' => 90], new Box(100, 120), new Box(90, 90)],
99
            ['outbound', ['width' => 90, 'height' => 90], new Box(120, 100), new Box(90, 90)],
100
            ['outbound', ['width' => 90, 'height' => 90], new Box(50, 50), new Box(90, 90)],
101
            ['outbound', ['width' => 90, 'height' => null], new Box(50, 50), new Box(90, 90)],
102
            ['outbound', ['width' => 90, 'height' => null], new Box(567, 50), new Box(90, 8)],
103
        ];
104
    }
105
}
106