TemplateTest::renderingSuccessProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 92
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 64
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 92
rs 8.7853

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Template;
6
7
use AbterPhp\Framework\Html\Helper\Attributes;
8
use PHPUnit\Framework\TestCase;
9
10
class TemplateTest extends TestCase
11
{
12
    /** @var Template - System Under Test */
13
    protected Template $sut;
14
15
    public function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $this->sut = new Template();
20
    }
21
22
    /**
23
     * @return array
24
     */
25
    public function parsingSuccessProvider(): array
26
    {
27
        $attributes = Attributes::fromArray(['f' => 'foo', 'b' => 'bar']);
28
29
        return [
30
            'empty'                     => ['', []],
31
            'fail'                      => ['', []],
32
            'templates-only-1'          => [
33
                '{{block/content-one-1}}',
34
                [
35
                    'block' =>
36
                        [
37
                            'content-one-1' => [
38
                                new ParsedTemplate('block', 'content-one-1', null, ['{{block/content-one-1}}']),
39
                            ],
40
                        ],
41
                ],
42
            ],
43
            'templates-only-2'          => [
44
                '{{block/content-one-1}} {{  block/content-2-two   }}',
45
                [
46
                    'block' => [
47
                        'content-2-two' => [
48
                            new ParsedTemplate('block', 'content-2-two', null, ['{{  block/content-2-two   }}']),
49
                        ],
50
                        'content-one-1' => [
51
                            new ParsedTemplate('block', 'content-one-1', null, ['{{block/content-one-1}}']),
52
                        ],
53
                    ],
54
                ],
55
            ],
56
            'templates-only-3'          => [
57
                '{{block/layout-one-1}} {{  block/layout-2-two   }} {{block/layout-one-1 }}',
58
                [
59
                    'block' => [
60
                        'layout-2-two' => [
61
                            new ParsedTemplate('block', 'layout-2-two', null, ['{{  block/layout-2-two   }}']),
62
                        ],
63
                        'layout-one-1' => [
64
                            new ParsedTemplate(
65
                                'block',
66
                                'layout-one-1',
67
                                null,
68
                                ['{{block/layout-one-1}}', '{{block/layout-one-1 }}']
69
                            ),
70
                        ],
71
                    ],
72
                ],
73
            ],
74
            'template-with-attribute'   => [
75
                '{{block/layout-one-1 f="foo" b="bar"}}',
76
                [
77
                    'block' => [
78
                        'layout-one-1' => [
79
                            new ParsedTemplate(
80
                                'block',
81
                                'layout-one-1',
82
                                $attributes,
83
                                ['{{block/layout-one-1 f="foo" b="bar"}}']
84
                            ),
85
                        ],
86
                    ],
87
                ],
88
            ],
89
            'templates-with-attributes' => [
90
                '{{block/layout-one-1 f="foo" b="bar"}} {{block/layout-one-1 b="bar" f="foo"}}', // phpcs:ignore
91
                [
92
                    'block' => [
93
                        'layout-one-1' => [
94
                            new ParsedTemplate(
95
                                'block',
96
                                'layout-one-1',
97
                                $attributes,
98
                                [
99
                                    '{{block/layout-one-1 f="foo" b="bar"}}',
100
                                    '{{block/layout-one-1 b="bar" f="foo"}}',
101
                                ]
102
                            ),
103
                        ],
104
                    ],
105
                ],
106
            ],
107
        ];
108
    }
109
110
    /**
111
     * @dataProvider parsingSuccessProvider
112
     *
113
     * @param string $rawContent
114
     * @param array  $expectedTemplates
115
     */
116
    public function testParse(string $rawContent, array $expectedTemplates): void
117
    {
118
        $this->sut->setRawContent($rawContent);
119
120
        $actualTemplates = $this->sut->parse();
121
122
        $this->assertEquals($expectedTemplates, $actualTemplates);
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function renderingSuccessProvider(): array
129
    {
130
        return [
131
            'content-only-1'                        => [
132
                'abc',
133
                [],
134
                [],
135
                'abc',
136
            ],
137
            'content-with-vars-only-1'              => [
138
                '0  {{var/variable-one-1}} 1',
139
                ['variable-one-1' => 'abc'],
140
                [],
141
                '0  abc 1',
142
            ],
143
            'content-with-vars-only-2'              => [
144
                '0  {{var/variable-one-1}} 1{{var/variable-one-2}}2',
145
                ['variable-one-1' => 'abc'],
146
                [],
147
                '0  abc 12',
148
            ],
149
            'content-with-vars-only-3'              => [
150
                '0  {{var/variable-one-1}} 1{{var/variable-one-2}}2',
151
                ['variable-one-1' => 'abc', 'variable-one-2' => 'bcd'],
152
                [],
153
                '0  abc 1bcd2',
154
            ],
155
            'content-with-repeated-vars'            => [
156
                '0  {{var/variable-one-1}} {{var/variable-one-1}} 1',
157
                ['variable-one-1' => 'abc'],
158
                [],
159
                '0  abc abc 1',
160
            ],
161
            'content-with-modified-repeated-vars'   => [
162
                '0  {{var/variable-one-1}} {{ var/variable-one-1 }} 1',
163
                ['variable-one-1' => 'abc'],
164
                [],
165
                '0  abc abc 1',
166
            ],
167
            'content-with-blocks-only-1'            => [
168
                '0  {{block/one-1}} 1',
169
                [],
170
                ['block' => ['one-1' => 'abc']],
171
                '0  abc 1',
172
            ],
173
            'content-with-blocks-only-2'            => [
174
                '0  {{block/one-1}} 1{{block/two-2-two}}2',
175
                [],
176
                ['block' => ['one-1' => 'abc']],
177
                '0  abc 12',
178
            ],
179
            'content-with-blocks-only-3'            => [
180
                '0  {{block/one-1}} 1{{block/two-2-two}}2',
181
                [],
182
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']],
183
                '0  abc 1bcd2',
184
            ],
185
            'content-with-repeated-blocks'          => [
186
                '0  {{block/one-1}} {{block/one-1}} 1{{block/two-2-two}}2',
187
                [],
188
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']],
189
                '0  abc abc 1bcd2',
190
            ],
191
            'content-with-modified-repeated-blocks' => [
192
                '0  {{block/one-1}} {{ block/one-1 }} 1{{block/two-2-two}}2',
193
                [],
194
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']],
195
                '0  abc abc 1bcd2',
196
            ],
197
            'complex-1'                             => [
198
                '0  {{block/one-1}} {{ block/one-1 }}  {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore
199
                ['3-threeThree' => 'cde'],
200
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd'], 'gallery' => ['event-1' => 'fgh']],
201
                '0  abc abc  cde 1bcd2fgh bcd',
202
            ],
203
            'complex-without-subtemplate-value'     => [
204
                '0  {{block/one-1}} {{ block/one-1 }}  {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore
205
                ['3-threeThree' => 'cde'],
206
                ['block' => ['one-1' => 'abc'], 'gallery' => ['event-1' => 'fgh']],
207
                '0  abc abc  cde 12fgh ',
208
            ],
209
            'complex-without-subtemplate-type'      => [
210
                '0  {{block/one-1}} {{ block/one-1 }}  {{var/3-threeThree}} 1{{block/two-2-two}}2{{gallery/event-1}} {{ block/two-2-two }}', // phpcs:ignore
211
                ['3-threeThree' => 'cde'],
212
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']],
213
                '0  abc abc  cde 1bcd2 bcd',
214
            ],
215
            'brutal'                                => [
216
                "0 {{nope/nay}}  {{block/one-1}} {{  block/one-1 }}  {{var/3-threeThree}} 1{{block/two-2-two foo=\"This foo!\" bar=\"That bar!\"}}2{{gallery/event-1}}\n{{ block/two-2-two }}", // phpcs:ignore
217
                ['3-threeThree' => 'cde'],
218
                ['block' => ['one-1' => 'abc', 'two-2-two' => 'bcd']],
219
                "0 {{nope/nay}}  abc abc  cde 1bcd2\nbcd",
220
            ],
221
        ];
222
    }
223
224
    /**
225
     * @dataProvider renderingSuccessProvider
226
     *
227
     * @param string $rawContent
228
     * @param array  $vars
229
     * @param array  $templateData
230
     * @param string $expectedResult
231
     */
232
    public function testRender(string $rawContent, array $vars, array $templateData, string $expectedResult): void
233
    {
234
        $this->sut->setRawContent($rawContent)->setVars($vars)->setTypes(['block', 'gallery']);
235
236
        $this->sut->parse();
237
238
        $actualResult = $this->sut->render($templateData);
239
240
        $this->assertSame($expectedResult, $actualResult);
241
    }
242
}
243