Completed
Push — master ( 513898...595f7b )
by Łukasz
29:17
created

GenericProviderTest::testGetPlaceholder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 3
dl 0
loc 47
rs 9.0303
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\Bundle\EzPublishCoreBundle\Tests\Imagine\PlaceholderProvider;
10
11
use eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\GenericProvider;
12
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
13
use Imagine\Draw\DrawerInterface;
14
use Imagine\Image\AbstractFont;
15
use Imagine\Image\BoxInterface;
16
use Imagine\Image\ImageInterface;
17
use Imagine\Image\ImagineInterface;
18
use Imagine\Image\Palette\Color\ColorInterface;
19
use PHPUnit\Framework\TestCase;
20
21
class GenericProviderTest extends TestCase
22
{
23
    /**
24
     * @dataProvider getPlaceholderDataProvider
25
     */
26
    public function testGetPlaceholder(ImageValue $value, array $options = [], $expectedText)
27
    {
28
        $font = $this->createMock(AbstractFont::class);
29
30
        $imagine = $this->createMock(ImagineInterface::class);
31
        $imagine
32
            ->expects($this->atLeastOnce())
33
            ->method('font')
34
            ->willReturnCallback(function ($fontpath, $fontsize, ColorInterface $foreground) use ($options, $font) {
35
                $this->assertEquals($options['fontpath'], $fontpath);
36
                $this->assertEquals($options['fontsize'], $fontsize);
37
                $this->assertColorEquals($options['foreground'], $foreground);
38
39
                return $font;
40
            });
41
42
        $font
43
            ->expects($this->any())
44
            ->method('box')
45
            ->willReturn($this->createMock(BoxInterface::class));
46
47
        $image = $this->createMock(ImageInterface::class);
48
49
        $imagine
50
            ->expects($this->atLeastOnce())
51
            ->method('create')
52
            ->willReturnCallback(function (BoxInterface $size, ColorInterface $background) use ($value, $options, $image) {
53
                $this->assertSizeEquals([$value->width, $value->height], $size);
54
                $this->assertColorEquals($options['background'], $background);
55
56
                return $image;
57
            });
58
59
        $drawer = $this->createMock(DrawerInterface::class);
60
        $image
61
            ->expects($this->any())
62
            ->method('draw')
63
            ->willReturn($drawer);
64
65
        $drawer
66
            ->expects($this->atLeastOnce())
67
            ->method('text')
68
            ->with($expectedText, $font);
69
70
        $provider = new GenericProvider($imagine);
71
        $provider->getPlaceholder($value, $options);
72
    }
73
74
    public function getPlaceholderDataProvider()
75
    {
76
        return [
77
            [
78
                new ImageValue([
79
                    'id' => 'photo.jpg',
80
                    'width' => 640,
81
                    'height' => 480,
82
                ]),
83
                [
84
                    'background' => '#00FF00',
85
                    'foreground' => '#FF0000',
86
                    'fontsize' => 72,
87
                    'text' => "IMAGE PLACEHOLDER %width%x%height%\n(%id%)",
88
                    'fontpath' => '/path/to/font.ttf',
89
                ],
90
                "IMAGE PLACEHOLDER 640x480\n(photo.jpg)",
91
            ],
92
        ];
93
    }
94
95
    private function assertSizeEquals(array $expected, BoxInterface $actual)
96
    {
97
        $this->assertEquals($expected[0], $actual->getWidth());
98
        $this->assertEquals($expected[1], $actual->getHeight());
99
    }
100
101
    private function assertColorEquals($expected, ColorInterface $actual)
102
    {
103
        $this->assertEquals(strtolower($expected), strtolower((string)$actual));
104
    }
105
}
106