Completed
Push — master ( 0001a8...55fdf1 )
by Johannes
02:44
created

ImageRendererTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Lichtenwallner  (https://lichtenwallner.at)
4
 *
5
 * @see https://github.com/jolicht/markdown-cms for the canonical source repository
6
 * @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT
7
 * @copyright Copyright (c) Johannes Lichtenwallner
8
 */
9
declare(strict_types = 1);
10
namespace JolichtTest\MarkdownCms\Markdown\Renderer;
11
12
use Jolicht\MarkdownCms\Markdown\Renderer\ImageRenderer;
13
use PHPUnit\Framework\TestCase;
14
use League\CommonMark\Inline\Element\AbstractInline;
15
use League\CommonMark\Inline\Element\Text;
16
use League\CommonMark\HtmlElement;
17
use League\CommonMark\ElementRendererInterface;
18
use League\CommonMark\Inline\Element\Image;
19
20
class ImageRendererTest extends TestCase
21
{
22
    private $renderer, $urlTranslation;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
23
24
    protected function setUp()
25
    {
26
        $this->urlTranslation = [
27
            'http://local.url' => 'http://production.url'
28
        ];
29
        $this->renderer = new ImageRenderer($this->urlTranslation);
30
    }
31
32
    public function testGetUrlTranslation()
33
    {
34
        $this->assertSame($this->urlTranslation, $this->renderer->getUrlTranslation());
35
    }
36
37
    public function testRender()
38
    {
39
        $inline = $this->prophesize(Image::class);
40
        $altElement = $this->prophesize(Text::class);
41
42
        $inline
43
            ->firstChild()
44
            ->willReturn($altElement->reveal());
45
        $inline
46
            ->getUrl()
47
            ->willReturn('http://local.url/img/test.jpg');
48
49
50
        $altElement
51
            ->getContent()
52
            ->willReturn('testAltContent');
53
54
        $atts = [
55
            'src' => 'http://production.url/img/test.jpg',
56
            'layout' => 'responsive',
57
            'alt' => 'testAltContent',
58
        ];
59
        $expected = new HtmlElement('amp-img', $atts);
60
61
        $this->assertEquals($expected, $this->renderer->render($inline->reveal(), $this->prophesize(ElementRendererInterface::class)->reveal()));
62
    }
63
64
    public function testRenderAltAttributeContainsAdditionalAttributes()
65
    {
66
        $inline = $this->prophesize(Image::class);
67
        $altElement = $this->prophesize(Text::class);
68
69
        $inline
70
            ->firstChild()
71
            ->willReturn($altElement->reveal());
72
        $inline
73
            ->getUrl()
74
            ->willReturn('http://local.url/img/test.jpg');
75
76
77
        $altElement
78
            ->getContent()
79
            ->willReturn('testAltContent{"width": 30, "height": 15, "layout": "fixed"}');
80
81
        $atts = [
82
            'src' => 'http://production.url/img/test.jpg',
83
            'layout' => 'responsive',
84
            'width' => 30,
85
            'height' => 15,
86
            'layout' => 'fixed',
87
            'alt' => 'testAltContent',
88
        ];
89
        $expected = new HtmlElement('amp-img', $atts);
90
91
        $this->assertEquals($expected, $this->renderer->render($inline->reveal(), $this->prophesize(ElementRendererInterface::class)->reveal()));
92
    }
93
}