Passed
Push — master ( 4f9e3a...139be2 )
by Alexander
23:24
created

IntersectTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 87
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\Examples\Head;
6
use Horat1us\Examples\Person;
7
use Horat1us\XmlConvertibleObject;
8
use PHPUnit\Framework\TestCase;
9
10
class IntersectTest extends TestCase
11
{
12
    public function testIntersection()
13
    {
14
        $xml = new Person("Alex", "Letni", [
15
            new Head("small", null, [
16
                new XmlConvertibleObject('Eye'),
17
                new XmlConvertibleObject('Eye', [
18
                    new Person('Adam', 'Morgan'),
19
                ]),
20
            ])
21
        ]);
22
23
        $compared = new Person("Alex", "Letni", [
24
            new Head('small', null, [
25
                new XmlConvertibleObject('Eye', [
26
                    new Person('Adam', 'Morgan'),
27
                ]),
28
            ])
29
        ]);
30
31
32
        $result = $xml->xmlIntersect($compared);
33
        $this->assertInstanceOf(Person::class, $result);
34
        /** @var Person $result */
35
36
        $this->assertEquals($result->name, $compared->name);
37
        $this->assertEquals($result->name, $xml->name);
38
        $this->assertNotNull($result->xmlChildren);
39
        $this->assertCount(1, $result->xmlChildren);
40
        $this->assertInstanceOf(Head::class, $result->xmlChildren[0]);
41
        $this->assertNotNull($result->xmlChildren[0]->xmlChildren);
42
        $this->assertCount(1, $result->xmlChildren[0]->xmlChildren);
43
        // Eye
44
        $this->assertInstanceOf(XmlConvertibleObject::class, $result->xmlChildren[0]->xmlChildren[0]);
45
        $this->assertNotNull(
46
            $result->xmlChildren[0]->xmlChildren[0]->xmlChildren
47
        );
48
        $this->assertCount(
49
            1,
50
            // Sub-person
51
            $result->xmlChildren[0]->xmlChildren[0]->xmlChildren
52
        );
53
        $this->assertInstanceOf(
54
            Person::class,
55
            $subPerson = $result->xmlChildren[0]->xmlChildren[0]->xmlChildren[0]
56
        );
57
        $this->assertEquals(
58
            'Adam',
59
            $subPerson->name
60
        );
61
        $this->assertEquals(
62
            'Morgan',
63
            $subPerson->surname
64
        );
65
    }
66
67
    public function testEmptyIntersect()
68
    {
69
        $first = new Person('Alex', "Lowe", [
70
            new XmlConvertibleObject('inner')
71
        ]);
72
        $second = clone $first;
73
74
        $result = $first->xmlIntersect($second);
75
        $this->assertInstanceOf(Person::class, $result);
76
        $this->assertCount(1, $result->getXmlChildren());
77
        $this->assertInstanceOf(XmlConvertibleObject::class, $result->getXmlChildren()[0]);
78
        $this->assertEquals('inner', $result->getXmlChildren()[0]->getXmlElementName());
79
    }
80
81
    public function testDifferentElements()
82
    {
83
        $xml = new Person();
84
        $compared = new Head();
85
        $this->assertNull($xml->xmlIntersect($compared));
86
    }
87
88
    public function testOne()
89
    {
90
        $first = new Person('Alex', "First");
91
        $second = clone $first;
92
93
        $result = $first->xmlIntersect($first);
94
        $this->assertInstanceOf(get_class($first), $result);
95
    }
96
}
97