Completed
Push — master ( 5d53c5...860d57 )
by Alex
09:53 queued 01:42
created

HtmlTemplateUnitTest::testPathsManipulations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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