Passed
Push — master ( efa86f...55c9f6 )
by Petr
10:18
created

ElementTest::testChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 27
rs 9.568
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_templates\AHtmlElement;
8
9
10
/**
11
 * Class ElementTest
12
 * @package BasicTests
13
 * How to check abstracts? Extend them.
14
 */
15
class ElementTest extends CommonTestClass
16
{
17
    public function testSimple()
18
    {
19
        $data = new Element('exe');
20
        $this->assertEquals('exe', $data->getAlias());
21
    }
22
23
    public function testAttributes()
24
    {
25
        $data = new Element('exe');
26
        $this->assertEmpty($data->dummy());
27
        $data->dummy('resggs');
28
        $this->assertEquals('resggs', $data->dummy());
29
        $data->removeAttribute('dummy');
30
        $this->assertEmpty($data->dummy());
31
        $data->foo('fkhlg', 'fpklasg');
32
        $this->assertEmpty($data->foo());
33
    }
34
35
    public function testChildren()
36
    {
37
        $data = new Element('exe');
38
        $this->assertEmpty(iterator_to_array($data->getChildren()));
39
        $data->setChildren([
40
            0 => new SomeChild(),
41
            'dome' => new ElseChild(),
42
            'anno' => 'whyy',
43
        ]);
44
        $this->assertEmpty($data->dummy);
45
        $data->dummy = 'resggs';
46
        $this->assertInstanceOf('\kalanis\kw_templates\AHtmlElement', $data->dummy);
47
        $this->assertInstanceOf('\kalanis\kw_templates\AHtmlElement', $data->dome);
48
        $this->assertInstanceOf('\kalanis\kw_templates\AHtmlElement', $data->__get(0));
49
        $data->__set('or', new NextChild());
50
        $data->removeChild('dome');
51
        $this->assertTrue($data->__empty('dome'));
52
        $this->assertEmpty($data->dome);
53
54
        $this->assertFalse($data->offsetExists('fahd'));
55
        $data->offsetSet('fahd', new ElseChild());
56
        $this->assertTrue($data->offsetExists('fahd'));
57
        $this->assertInstanceOf('\kalanis\kw_templates\AHtmlElement', $data->offsetGet('fahd'));
58
        $data->offsetUnset('fahd');
59
        $this->assertFalse($data->offsetExists('fahd'));
60
61
        $this->assertNotEmpty(iterator_to_array($data->getIterator()));
62
    }
63
64
    public function testInheritance()
65
    {
66
        $original = new Element('exe');
67
        $original->setAttributes([
68
            'cde' => 'zfx',
69
            'vfr' => 'ohv',
70
        ]);
71
        $sender = new SomeChild();
72
        $result = $original->inherit($sender);
73
74
        $this->assertEquals('ohv', $result->getAttribute('vfr'));
75
        $this->assertEquals('zfx', $result->getAttribute('cde'));
76
        $this->assertNotEquals($result, $original);
77
    }
78
79
    public function testMerge()
80
    {
81
        $data = new Element('exe');
82
        $data->setAttributes([
83
            'cde' => 'zfx',
84
            'vfr' => 'ohv',
85
        ]);
86
        $result = new SomeChild();
87
        $result->merge($data);
88
89
        $this->assertEquals('ohv', $result->getAttribute('vfr'));
90
        $this->assertEquals('zfx', $result->getAttribute('cde'));
91
    }
92
93
    public function testRender()
94
    {
95
        $data = new Element('exe');
96
        $data->setAttributes([
97
            'cde' => 'zfx',
98
            'vfr' => 'ohv',
99
        ]);
100
        $data1 = new SomeChild();
101
        $data1->setAttributes([
102
            'vfr' => 'ohv',
103
        ]);
104
        $data2 = new ElseChild();
105
        $data2->setAttributes([
106
            'cde' => 'zfx',
107
        ]);
108
109
        $data3 = new ElseChild();
110
        $data3->setAttributes([
111
            'fht' => 'kgs',
112
        ]);
113
114
        $data->addChild($data1);
115
        $data->addChild($data2, 'doo');
116
117
        $this->assertEquals('-- cde="zfx" vfr="ohv"-- ::poiuztrewq  vfr="ohv" ' . "\n" . '::lkjhgfdsa  cde="zfx" ', $data->render());
118
        $data->addChild($data3, 'doo', true);
119
120
        $this->assertEquals('-- cde="zfx" vfr="ohv"-- ::poiuztrewq  vfr="ohv" ' . "\n" . '::lkjhgfdsa  fht="kgs" ', $data->render());
121
    }
122
}
123
124
125
class Element extends AHtmlElement
126
{
127
    protected string $template = '--%s-- %s';
128
129
    public function __construct(string $alias)
130
    {
131
        $this->alias = $alias;
132
    }
133
}
134
135
136
class SomeChild extends AHtmlElement
137
{
138
    protected string $template = '::poiuztrewq %s %s';
139
}
140
141
142
class ElseChild extends AHtmlElement
143
{
144
    protected string $template = '::lkjhgfdsa %s %s';
145
}
146
147
148
class NextChild extends AHtmlElement
149
{
150
    protected string $template = 'mnbvcxy %s %s';
151
}
152