Passed
Push — master ( 8f302d...636da2 )
by Alex
21:26
created

HtmlTemplateUnitTest::testBlockExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\HtmlTemplate\Tests;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
use Mezon\HtmlTemplate\TemplateResources;
6
7
class HtmlTemplateUnitTest extends \PHPUnit\Framework\TestCase
8
{
9
10
    const PATH_TO_TEST_DATA = __DIR__ . '/TestData/';
11
12
    /**
13
     * Data provider for constructor tests
14
     *
15
     * @return array
16
     */
17
    public function constructorDataProvider(): array
18
    {
19
        return [
20
            [
21
                HtmlTemplateUnitTest::PATH_TO_TEST_DATA,
22
                'index'
23
            ],
24
            [
25
                HtmlTemplateUnitTest::PATH_TO_TEST_DATA . 'Res/',
26
                'index2'
27
            ],
28
            [
29
                [
30
                    HtmlTemplateUnitTest::PATH_TO_TEST_DATA,
31
                    HtmlTemplateUnitTest::PATH_TO_TEST_DATA . 'Res/'
32
                ],
33
                'index2'
34
            ]
35
        ];
36
    }
37
38
    /**
39
     * Testing construction with default path
40
     *
41
     * @param string|array $path
42
     *            paths to content
43
     * @param string $template
44
     *            template's name
45
     * @dataProvider constructorDataProvider
46
     */
47
    public function testConstructor($path, string $template)
48
    {
49
        // setup and test body
50
        $resources = new TemplateResources();
51
        $resources->addCssFile('./some.css');
52
        $resources->addJsFile('./some.js');
53
54
        $template = new HtmlTemplate($path, $template, [
55
            'main'
56
        ]);
57
        $template->setResources($resources);
58
59
        $content = $template->compile();
60
61
        // assertions
62
        $this->assertStringContainsString('<body>', $content, 'Layout was not setup');
63
        $this->assertStringContainsString('<section>', $content, 'Block was not setup');
64
    }
65
66
    /**
67
     * Data provider for constructor tests
68
     *
69
     * @return array
70
     */
71
    public function invalidConstructorDataProvider(): array
72
    {
73
        return [
74
            [
75
                __DIR__,
76
                'index3'
77
            ],
78
            [
79
                false,
80
                'index4'
81
            ]
82
        ];
83
    }
84
85
    /**
86
     * Testing invalid construction
87
     *
88
     * @param string|array $path
89
     *            paths to content
90
     * @param string $template
91
     *            template's name
92
     * @dataProvider invalidConstructorDataProvider
93
     */
94
    public function testInvalidConstructor($path, string $template)
95
    {
96
        $this->expectException(\Exception::class);
97
98
        // setup and test body
99
        $template = new HtmlTemplate($path, $template, [
100
            'main'
101
        ]);
102
103
        // debug if the exception was not thrown
104
        var_dump($template);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($template) looks like debug code. Are you sure you do not want to remove it?
Loading history...
105
    }
106
107
    /**
108
     * Testing that all unused place holders will be removed
109
     */
110
    public function testCompile()
111
    {
112
        // setup
113
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, 'index', [
114
            'main'
115
        ]);
116
        $_SERVER['HTTP_HOST'] = 'host';
117
118
        // test body
119
        $result = $template->compile();
120
121
        // assertions
122
        $this->assertStringNotContainsStringIgnoringCase('{title}', $result);
123
    }
124
125
    /**
126
     * Testing unexisting block
127
     */
128
    public function testGetUnexistingBlock()
129
    {
130
        // setup and test body
131
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, 'index', [
132
            'main'
133
        ]);
134
135
        $this->expectException(\Exception::class);
136
137
        // test body
138
        $template->getBlock('unexisting');
139
    }
140
141
    /**
142
     * Test existing var fetch
143
     */
144
    public function testGetExistingVar(): void
145
    {
146
        // setup
147
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
148
        $template->setPageVar('existing-var', 'existing value');
149
150
        // test body and assertions
151
        $this->assertEquals('existing value', $template->getPageVar('existing-var'));
152
    }
153
154
    /**
155
     * Test unexisting var fetch
156
     */
157
    public function testGetUnExistingVar(): void
158
    {
159
        // setup
160
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
161
162
        // assertions
163
        $this->expectException(\Exception::class);
164
165
        // test body
166
        $template->getPageVar('unexisting-var');
167
    }
168
169
    /**
170
     * Testing setPageVars method
171
     */
172
    public function testSetPageVars(): void
173
    {
174
        // setup
175
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
176
177
        // test body
178
        $template->setPageVars([
179
            'title' => 'stitle',
180
            'resources' => 'sresources',
181
            'main' => 'smain'
182
        ]);
183
184
        // assertions
185
        $this->assertEquals('stitle', $template->getPageVar('title'));
186
        $this->assertEquals('sresources', $template->getPageVar('resources'));
187
        $this->assertEquals('smain', $template->getPageVar('main'));
188
    }
189
190
    /**
191
     * Testing method setPageVarFromFile
192
     */
193
    public function testSetPageVarFromFile(): void
194
    {
195
        // setup
196
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
197
198
        // test body
199
        $template->setPageVarFromFile('title', HtmlTemplateUnitTest::PATH_TO_TEST_DATA . '/Res/var.txt');
200
201
        // assertions
202
        $this->assertEquals('some var from file', $template->getPageVar('title'));
203
    }
204
205
    /**
206
     * Testing methods addPaths, setPaths, getPaths
207
     */
208
    public function testPathsManipulations(): void
209
    {
210
        // setup
211
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
212
213
        // assertions
214
        $this->assertContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths());
215
216
        // test body
217
        $template->addPaths([
218
            'some-path'
219
        ]);
220
221
        // assertions
222
        $this->assertContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths());
223
        $this->assertContains('some-path', $template->getPaths());
224
225
        // test body
226
        $template->setPaths([
227
            'some-path'
228
        ]);
229
230
        // asssertions
231
        $this->assertNotContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths());
232
        $this->assertContains('some-path', $template->getPaths());
233
    }
234
235
    /**
236
     * Testing method
237
     */
238
    public function testBlockExists(): void
239
    {
240
        // setup
241
        $template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA);
242
243
        // test body and assertions
244
        $this->assertTrue($template->blockExists('block1'));
245
        $this->assertTrue($template->blockExists('block2'));
246
        $this->assertFalse($template->blockExists('block3'));
247
    }
248
}
249