Passed
Branch master (532e7d)
by Alexander
03:23
created

DiffTest::testDifference()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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