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

DiffTest::testDifference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
nc 1
cc 1
eloc 14
nop 0
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\Examples\Head;
6
use Horat1us\Examples\Person;
7
use Horat1us\XmlConvertibleInterface;
8
use Horat1us\XmlConvertibleObject;
9
use PHPUnit\Framework\TestCase;
10
use SebastianBergmann\PHPLOC\Log\XML;
11
12
class DiffTest extends TestCase
13
{
14
    public function testDifferenceWithEmpty()
15
    {
16
        $first = new Person('Alex', 'Jobs');
17
        $second = new Person('Alex', 'Jobs', [
18
            new XmlConvertibleObject()
19
        ]);
20
        $diff = $first->xmlDiff($second);
21
        $this->assertInstanceOf(get_class($first), $diff);
22
    }
23
24
    public function testDifferenceWithAdditional()
25
    {
26
        $first = new Person('Alex', 'Jobs', [
27
            new XmlConvertibleObject('a'),
28
            new XmlConvertibleObject('b'),
29
        ]);
30
        $second = new Person('Alex', 'Jobs', [
31
            new XmlConvertibleObject('b'),
32
        ]);
33
34
        $diff = $first->xmlDiff($second);
35
        $this->assertInstanceOf(XmlConvertibleInterface::class, $diff);
36
        $this->assertNotNull($diff->xmlChildren);
37
        $this->assertEquals(1, count($diff->xmlChildren ?? []));
38
        $this->assertEquals('a', $diff->xmlChildren[0]->getXmlElementName());
39
    }
40
41
    public function testDifferenceWithNoProperty()
42
    {
43
        $a = new XmlConvertibleObject('undefined');
44
        $b = new XmlConvertibleObject('undefined');
45
        $a->{'property'} = mt_rand();
46
        $diff = $a->xmlDiff($b);
47
        $this->assertInstanceOf(get_class($a), $diff);
48
    }
49
50
    public function testDifference()
51
    {
52
        $xml = $this->generateFirst();
53
        $compared = $this->generateSecond();
54
55
        $result = $xml->xmlDiff($compared);
56
57
        $this->assertInstanceOf(Person::class, $result);
58
59
        $this->assertNotNull($result->xmlChildren);
60
        $this->assertCount(1, $result->xmlChildren);
61
62
        $this->assertInstanceOf(Head::class, $result->xmlChildren[0]);
63
        $this->assertNotNull($result->xmlChildren[0]->xmlChildren);
64
        $this->assertCount(1, $result->xmlChildren[0]->xmlChildren);
65
66
        $this->assertInstanceOf(
67
            XmlConvertibleObject::class,
68
            $result->xmlChildren[0]->xmlChildren[0]
69
        );
70
        $this->assertNull($result->xmlChildren[0]->xmlChildren[0]->xmlChildren);
71
    }
72
73
    protected function generateFirst()
74
    {
75
        return new Person("Alex", "Letni", [
76
            new Head("small", 'cool', [
77
                new XmlConvertibleObject('Eye'),
78
                new XmlConvertibleObject('Eye', [
79
                    new Person('Adam', 'Morgan'),
80
                ]),
81
            ])
82
        ]);
83
    }
84
85
    protected function generateSecond()
86
    {
87
        return new Person("Alex", "Letni", [
88
            new Head('small', 'cool', [
89
                new XmlConvertibleObject('Eye', [
90
                    new Person('Adam', 'Morgan'),
91
                ]),
92
            ])
93
        ]);
94
    }
95
}
96