DefaultEditorTest::testRenderEditor()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 8
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Extension;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Editor\DefaultEditor;
15
use Ivoaz\Bundle\ContentEditableBundle\Model\Content;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
class DefaultEditorTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var \Twig_Environment|\PHPUnit_Framework_MockObject_MockObject
23
     */
24
    private $twig;
25
26
    /**
27
     * @var DefaultEditor
28
     */
29
    private $editor;
30
31
    public function setUp()
32
    {
33
        $this->twig = $this->getMock(\Twig_Environment::class, ['render'], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
        $container = $this->getMock(ContainerInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
        $container->method('get')
36
            ->with('twig')
37
            ->willReturn($this->twig);
38
        $this->editor = new DefaultEditor($container);
39
    }
40
41
    /**
42
     * @dataProvider getRenderContentTestData
43
     *
44
     * @param string  $expectedText
45
     * @param Content $content
46
     * @param array   $options
47
     * @param string  $message
48
     */
49
    public function testRenderContent($expectedText, Content $content, array $options = [], $message = '')
50
    {
51
        $text = $this->editor->renderContent($content, $options);
52
53
        $this->assertSame($expectedText, $text, $message);
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getRenderContentTestData()
60
    {
61
        $content = new Content();
62
        $content->setId(1)
63
            ->setText('Example text')
64
            ->setLocale('en');
65
66
        return [
67
            [
68
                'Example text',
69
                $content,
70
                ['separately' => true],
71
                'The text should not be modified when editable separately.',
72
            ],
73
            [
74
                '<span class="ivoaz-content-editable" data-ivoaz-content-editable-id="1">Example text</span>',
75
                clone $content,
76
                [],
77
                'The text was not wrapped correctly.',
78
            ],
79
        ];
80
    }
81
82
83
    /**
84
     * @dataProvider getRenderEditorTestData
85
     *
86
     * @param array  $contents
87
     * @param array  $separateContents
88
     * @param string $message
89
     */
90
    public function testRenderEditor($contents, $separateContents, $message = '')
91
    {
92
        $this->twig->method('render')
93
            ->will(
94
                $this->returnValueMap(
95
                    [
96
                        ['@IvoazContentEditable/editor.html.twig', [], '<p>editor</p>'],
97
                        [
98
                            '@IvoazContentEditable/separately_editable_contents.html.twig',
99
                            ['contents' => $separateContents],
100
                            '<p>separate contents</p>',
101
                        ],
102
                    ]
103
                )
104
            );
105
106
        foreach ($contents as $content) {
107
            $this->editor->renderContent($content, []);
108
        }
109
110
        foreach ($separateContents as $content) {
111
            $this->editor->renderContent($content, ['separately' => true]);
112
        }
113
114
        $html = $this->editor->renderEditor(new Response());
115
116
        $expectedHtml = empty($separateContents)
117
            ? '<p>editor</p>'
118
            : '<p>separate contents</p><p>editor</p>';
119
120
        $this->assertSame($expectedHtml, $html, $message);
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getRenderEditorTestData()
127
    {
128
        return [
129
            [
130
                [new Content(), new Content()],
131
                [(new Content())->setId(1), (new Content())->setId(2)],
132
                'Editor should be injected with separately editable contents.',
133
            ],
134
            [
135
                [new Content(), new Content()],
136
                [],
137
                'Editor should be injected without separately editable contents.',
138
            ],
139
        ];
140
    }
141
}
142