|
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 = $this->generateFirst(); |
|
58
|
|
|
$compared = $this->generateSecond(); |
|
59
|
|
|
|
|
60
|
|
|
$result = $xml->xmlDiff($compared); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertInstanceOf(Person::class, $result); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertNotNull($result->xmlChildren); |
|
|
|
|
|
|
65
|
|
|
$this->assertCount(1, $result->xmlChildren); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$this->assertInstanceOf(Head::class, $result->xmlChildren[0]); |
|
|
|
|
|
|
68
|
|
|
$this->assertNotNull($result->xmlChildren[0]->xmlChildren); |
|
|
|
|
|
|
69
|
|
|
$this->assertCount(1, $result->xmlChildren[0]->xmlChildren); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->assertInstanceOf( |
|
72
|
|
|
XmlConvertibleObject::class, |
|
73
|
|
|
$result->xmlChildren[0]->xmlChildren[0] |
|
|
|
|
|
|
74
|
|
|
); |
|
75
|
|
|
$this->assertNull($result->xmlChildren[0]->xmlChildren[0]->xmlChildren); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function generateFirst() |
|
79
|
|
|
{ |
|
80
|
|
|
return new Person("Alex", "Letni", [ |
|
81
|
|
|
new Head("small", 'cool', [ |
|
82
|
|
|
new XmlConvertibleObject('Eye'), |
|
83
|
|
|
new XmlConvertibleObject('Eye', [ |
|
84
|
|
|
new Person('Adam', 'Morgan'), |
|
85
|
|
|
]), |
|
86
|
|
|
]) |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function generateSecond() |
|
91
|
|
|
{ |
|
92
|
|
|
return new Person("Alex", "Letni", [ |
|
93
|
|
|
new Head('small', 'cool', [ |
|
94
|
|
|
new XmlConvertibleObject('Eye', [ |
|
95
|
|
|
new Person('Adam', 'Morgan'), |
|
96
|
|
|
]), |
|
97
|
|
|
]) |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: