HtmlElementTest::getMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Sake
4
 *
5
 * @link      http://github.com/sandrokeil/HtmlElement for the canonical source repository
6
 * @copyright Copyright (c) 2014-2017 Sandro Keil
7
 * @license   http://github.com/sandrokeil/HtmlElement/blob/master/LICENSE.txt New BSD License
8
 */
9
10
namespace SakeTest\HtmlElement\View\Helper;
11
12
use Sake\HtmlElement\View\Helper\HtmlElement;
13
use PHPUnit\Framework\TestCase;
14
use Zend\Escaper\Escaper;
15
16
/**
17
 * Class HtmlElementTest
18
 *
19
 * Tests integrity of \SakeTest\HtmlElement\View\Helper\HtmlElement
20
 */
21
class HtmlElementTest extends TestCase
22
{
23
    /**
24
     * PHP errors
25
     *
26
     * @var array
27
     */
28
    protected $errors = array();
29
30
    /**
31
     * Test error handler to get php errors
32
     *
33
     * @param int $errno
34
     * @param string $errstr
35
     * @param string $errfile
36
     * @param string $errline
37
     * @param string $errcontext
38
     */
39
    public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
40
    {
41
        $this->errors[] = compact("errno", "errstr", "errfile", "errline", "errcontext");
42
    }
43
44
    /**
45
     * Checks if php error was occured
46
     *
47
     * @param int $errno
48
     * @param string $errstr
49
     * @return bool True if exists, otherwise false
50
     */
51
    public function hasError($errno, $errstr)
52
    {
53
        foreach ($this->errors as $error) {
54
            if (false !== strpos($error["errstr"], $errstr)
55
                && $error["errno"] === $errno) {
56
                return false;
57
            }
58
        }
59
        return true;
60
    }
61
62
    /**
63
     * Tests setTag
64
     *
65
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setTag
66
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getTag
67
     * @group view
68
     */
69
    public function testSetTag()
70
    {
71
        $cut = new HtmlElement();
72
        $tag = 'div';
73
74
        $cut->setTag($tag);
75
76
        $this->assertEquals($tag, $cut->getTag());
77
    }
78
79
    /**
80
     * Tests setId
81
     *
82
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setId
83
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getId
84
     * @group view
85
     */
86
    public function testSetId()
87
    {
88
        $cut = new HtmlElement();
89
        $id = 'unique';
90
91
        $cut->setId($id);
92
93
        $this->assertEquals($id, $cut->getId());
94
    }
95
96
    /**
97
     * Tests setClass
98
     *
99
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setClass
100
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getClass
101
     * @group view
102
     */
103
    public function testSetClass()
104
    {
105
        $cut = new HtmlElement();
106
        $class = 'box shadow';
107
108
        $cut->setClass($class);
109
110
        $this->assertEquals($class, $cut->getClass());
111
    }
112
113
    /**
114
     * Tests appendClass
115
     *
116
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::appendClass
117
     * @depends testSetClass
118
     * @group view
119
     */
120
    public function testAppendClass()
121
    {
122
        $cut = new HtmlElement();
123
        $class = 'box';
124
        $append = 'shadow';
125
126
        $cut->appendClass($append);
127
        $this->assertEquals($append, $cut->getClass());
128
129
        $cut->setClass($class);
130
        $cut->appendClass($append);
131
132
        $this->assertEquals($class . ' ' . $append, $cut->getClass());
133
    }
134
135
    /**
136
     * Tests setText
137
     *
138
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setText
139
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getText
140
     * @group view
141
     */
142
    public function testSetText()
143
    {
144
        $cut = new HtmlElement();
145
        $test = 'my text';
146
147
        $cut->setText($test);
148
149
        $this->assertEquals($test, $cut->getText());
150
    }
151
152
    /**
153
     * Tests appendText
154
     *
155
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::appendText
156
     * @depends testSetText
157
     * @group view
158
     */
159
    public function testAppendText()
160
    {
161
        $cut = new HtmlElement();
162
        $text = 'my text';
163
        $append = 'another text';
164
165
        $cut->appendText($append);
166
        $this->assertEquals($append, $cut->getText());
167
168
        $cut->setText($text);
169
        $cut->appendText($append);
170
171
        $this->assertEquals($text . $append, $cut->getText());
172
    }
173
174
    /**
175
     * Tests setAttributes
176
     *
177
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setAttributes
178
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getAttributes
179
     * @group view
180
     */
181
    public function testSetAttributes()
182
    {
183
        $cut = new HtmlElement();
184
185
        $id = 'my id';
186
        $class = 'box';
187
188
        $cut->setId($id);
189
        $cut->setClass($class);
190
191
        $this->assertEquals(array('id' => $id, 'class' => $class), $cut->getAttributes());
192
193
        $attributes = array(
194
            'id' => 'unique',
195
            'class' => 'box shadow',
196
        );
197
        $cut->setAttributes($attributes);
198
199
        $this->assertEquals($attributes, $cut->getAttributes());
200
    }
201
202
    /**
203
     * Tests if html rendering can be enabled / disabled
204
     *
205
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::enableHtml
206
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::useHtml
207
     * @group view
208
     */
209
    public function testToggleHtmlRendering()
210
    {
211
        $cut = new HtmlElement();
212
213
        $this->assertEquals(false, $cut->useHtml(), 'Html rendering is not disabled at default');
214
215
        $cut->enableHtml(true);
216
        $this->assertEquals(true, $cut->useHtml(), 'Html rendering can not be enabled');
217
218
        $cut->enableHtml(false);
219
        $this->assertEquals(false, $cut->useHtml(), 'Html rendering can not be disabled');
220
    }
221
222
    /**
223
     * Tests setEscapeText
224
     *
225
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setEscapeText
226
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::isEscapeText
227
     * @group view
228
     */
229
    public function testSetEscapeText()
230
    {
231
        $cut = new HtmlElement();
232
        $test = false;
233
234
        $this->assertEquals(true, $cut->isEscapeText());
235
236
        $cut->setEscapeText($test);
237
238
        $this->assertEquals($test, $cut->isEscapeText());
239
    }
240
241
    /**
242
     * Tests setEscapeHtmlAttributes
243
     *
244
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setEscapeHtmlAttribute
245
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::isEscapeHtmlAttribute
246
     * @group view
247
     */
248
    public function testSetEscapeHtmlAttributes()
249
    {
250
        $cut = new HtmlElement();
251
        $test = false;
252
253
        $this->assertEquals(true, $cut->isEscapeHtmlAttribute());
254
255
        $cut->setEscapeHtmlAttribute($test);
256
257
        $this->assertEquals($test, $cut->isEscapeHtmlAttribute());
258
    }
259
260
261
    /**
262
     * Tests setEscaper
263
     *
264
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::setEscaper
265
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::getEscaper
266
     * @group view
267
     */
268
    public function testSetEscaper()
269
    {
270
        $class = new \ReflectionClass('\Sake\HtmlElement\View\Helper\HtmlElement');
271
        $method = $class->getMethod('getEscaper');
272
        $method->setAccessible(true);
273
274
        $cut = new HtmlElement();
275
        $cut->setEscaper(new Escaper());
276
277
        $this->assertInstanceOf('\Zend\Escaper\Escaper', $method->invoke($cut));
278
    }
279
280
    /**
281
     * Tests if call of view helper return new html element object with specified objects
282
     *
283
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::__invoke
284
     * @depends testSetText
285
     * @depends testSetClass
286
     * @depends testToggleHtmlRendering
287
     * @group view
288
     */
289
    public function testInvokeShouldReturnObjectWithSpecifiedOptions()
290
    {
291
        $cut = $this->getStubWithEscaper();
292
293
        $text = 'my text';
294
        $tag = 'div';
295
        $id = 'unique';
296
        $class = 'box shadow';
297
        $attributes = array('id' => $id, 'class' => $class);
298
299
        /* @var $element HtmlElement */
300
        $element = $cut($tag, $text, $attributes, true);
301
302
        $this->assertInstanceOf('\Sake\HtmlElement\View\Helper\HtmlElement', $element, "Invocation doesn't work");
303
304
        $this->assertEquals(true, $element->useHtml(), 'Html rendering was not enabled');
305
        $this->assertEquals($attributes, $element->getAttributes(), 'Html attributes were not set');
306
        $this->assertEquals($id, $element->getId(), 'Id attribute was not set');
307
        $this->assertEquals($class, $element->getClass(), 'Css class was not set');
308
    }
309
310
    /**
311
     * Tests if html element rendered successfully
312
     *
313
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::render
314
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::buildAttributes
315
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::__toString
316
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::toString
317
     * @depends testInvokeShouldReturnObjectWithSpecifiedOptions
318
     * @group view
319
     */
320
    public function testToStringShouldTriggerErrorIfExceptionOccurs()
321
    {
322
        $this->errors = array();
323
        set_error_handler(array($this, "errorHandler"));
324
325
        $stub = $this->getStubWithEscaper(array('render'));
326
327
        $stub->expects($this->any())
328
            ->method('render')
329
            ->will(
330
                $this->throwException(
331
                    new \Exception(
332
                        'Exception occurred',
333
                        0,
334
                        new \Exception('Previous Exception occurred')
335
                    )
336
                )
337
            );
338
339
        $text = 'my text';
340
        $tag = 'div';
341
        $id = 'unique';
342
        $class = 'box shadow';
343
        $attributes = array(array('id' => $id, 'class' => $class));
344
345
        /* @var $element HtmlElement */
346
        $element = $stub($tag, $text, $attributes);
347
        $html = (string) $element;
348
349
        $this->assertEquals('', $html);
350
351
        $this->assertFalse(
352
            $this->hasError(E_USER_WARNING, 'Exception occurred'),
353
            sprintf('Error "%s" with message "%s" was not found', E_USER_WARNING, 'Exception occurred')
354
        );
355
356
        $this->assertFalse(
357
            $this->hasError(E_USER_WARNING, 'Previous Exception occurred'),
358
            sprintf('Error "%s" with message "%s" was not found', E_USER_WARNING, 'Previous Exception occurred')
359
        );
360
    }
361
362
    /**
363
     * Tests if html element rendered successfully
364
     *
365
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::render
366
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::buildAttributes
367
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::__toString
368
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::toString
369
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::escapeHtmlAttribute
370
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::escapeText
371
     * @depends testInvokeShouldReturnObjectWithSpecifiedOptions
372
     * @group view
373
     */
374
    public function testRender()
375
    {
376
        $stub = $this->getStubWithEscaper();
377
378
        $text = 'my text';
379
        $tag = 'div';
380
        $id = 'unique';
381
        $class = 'box shadow';
382
        $attributes = array('id' => $id, 'class' => $class);
383
384
        $expectedHtml = '<div id="unique" class="box shadow">my text</div>';
385
386
        /* @var $element HtmlElement */
387
        $element = $stub($tag, $text, $attributes);
388
389
        $this->assertEquals($expectedHtml, $element->render(), 'Html element rendering failed');
390
        $this->assertEquals($expectedHtml, $element->__toString(), 'Html element rendering failed');
391
        $this->assertEquals($expectedHtml, $element->toString(), 'Html element rendering failed');
392
393
        $element->setEscapeText(false);
394
        $element->setEscapeHtmlAttribute(false);
395
        $this->assertEquals($expectedHtml, $element->render(), 'Html element rendering failed');
396
397
        $element->enableHtml(true);
398
        $this->assertEquals($expectedHtml, $element->render(), 'Html element rendering failed');
399
    }
400
401
    /**
402
     * Tests if html element rendered self closing tag successfully
403
     *
404
     * @depends testInvokeShouldReturnObjectWithSpecifiedOptions
405
     * @dataProvider dataProviderForTestRenderWithSelfClosingTag
406
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::render
407
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::buildAttributes
408
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::__toString
409
     * @covers \Sake\HtmlElement\View\Helper\HtmlElement::toString
410
     * @group view
411
     */
412
    public function testRenderWithSelfClosingTag($tag)
413
    {
414
        $stub = $this->getStubWithEscaper();
415
416
        // text should not be rendered
417
        $text = 'my text';
418
        $id = 'unique';
419
        $class = 'full';
420
        $attributes = array('id' => $id, 'class' => $class);
421
422
        $expectedHtml = '<' . $tag . ' id="unique" class="full" />';
423
424
        /* @var $element HtmlElement */
425
        $element = $stub($tag, $text, $attributes);
426
427
        $this->assertEquals($expectedHtml, $element->render(), 'Html element rendering failed');
428
        $this->assertEquals($expectedHtml, $element->__toString(), 'Html element rendering failed');
429
        $this->assertEquals($expectedHtml, $element->toString(), 'Html element rendering failed');
430
    }
431
432
    /**
433
     * Returns stub with mocked view plugin manager
434
     *
435
     * @return \PHPUnit_Framework_MockObject_MockObject
436
     */
437
    protected function getStubWithEscaper(array $methods = array())
438
    {
439
        $stub = $this->getMock(
440
            'Sake\HtmlElement\View\Helper\HtmlElement',
441
            array_merge(array('getView', 'getEscaper'), $methods)
442
        );
443
444
        $stubEscaper = $this->getMock('Zend\Escaper\Escaper', array('escapeHtml', 'escapeHtmlAttr'));
445
446
        $stubEscaper->expects($this->any())
447
            ->method('escapeHtml')
448
            ->will($this->returnArgument(0));
449
450
        $stubEscaper->expects($this->any())
451
            ->method('escapeHtmlAttr')
452
            ->will($this->returnArgument(0));
453
454
        $stub->expects($this->any())
455
            ->method('getEscaper')
456
            ->will($this->returnValue($stubEscaper));
457
458
        return $stub;
459
    }
460
461
    /**
462
     * data provider for the test method testRenderWithSelfClosingTag()
463
     *
464
     * @return array
465
     */
466
    public function dataProviderForTestRenderWithSelfClosingTag()
467
    {
468
        return array(
469
            array(
470
                'tag' => 'area',
471
            ),
472
            array(
473
                'tag' => 'base',
474
            ),
475
            array(
476
                'tag' => 'br',
477
            ),
478
            array(
479
                'tag' => 'col',
480
            ),
481
            array(
482
                'tag' => 'command',
483
            ),
484
            array(
485
                'tag' => 'embed',
486
            ),
487
            array(
488
                'tag' => 'hr',
489
            ),
490
            array(
491
                'tag' => 'img',
492
            ),
493
            array(
494
                'tag' => 'input',
495
            ),
496
            array(
497
                'tag' => 'keygen',
498
            ),
499
            array(
500
                'tag' => 'link',
501
            ),
502
            array(
503
                'tag' => 'meta',
504
            ),
505
            array(
506
                'tag' => 'param',
507
            ),
508
            array(
509
                'tag' => 'source',
510
            ),
511
            array(
512
                'tag' => 'track',
513
            ),
514
            array(
515
                'tag' => 'wbr',
516
            ),
517
        );
518
    }
519
520
    private function getMock($class, $methods)
521
    {
522
        return $this->getMockBuilder($class)
523
            ->setMethods($methods)
524
            ->getMock();
525
    }
526
}
527