1 | <?php |
||
14 | |||
15 | public function testNotEqualXml() |
||
16 | { |
||
17 | $diff = $this->diff($this->getFirstText(), $this->getSecondText()); |
||
18 | $this->assertNotNull($diff); |
||
19 | } |
||
20 | |||
21 | protected function diff(string $text1, string $text2) |
||
22 | { |
||
23 | |||
24 | $document = new \DOMDocument(); |
||
25 | $document->loadXML($text1); |
||
26 | |||
27 | $document2 = new \DOMDocument(); |
||
28 | $document2->loadXML($text2); |
||
29 | |||
30 | |||
31 | $xml1 = XmlConvertibleObject::fromXml($document); |
||
32 | $xml2 = XmlConvertibleObject::fromXml($document2); |
||
33 | |||
34 | $diff = $xml1->xmlDiff($xml2); |
||
35 | if (!$diff) { |
||
36 | return null; |
||
37 | } |
||
38 | |||
39 | $result = new \DOMDocument(); |
||
40 | $diffElement = $diff->toXml($result); |
||
41 | $result->appendChild($diffElement); |
||
42 | return $result->saveXML(); |
||
43 | } |
||
44 | |||
45 | protected function getFirstText() |
||
46 | { |
||
47 | $text = '<comp> |
||
48 | <deal id="1"> |
||
49 | <period id="1"></period> |
||
50 | <period id="2"></period> |
||
51 | </deal> |
||
52 | <deal id="2"> |
||
53 | <period id="1"></period> |
||
54 | <period id="2"></period> |
||
55 | </deal> |
||
56 | </comp>'; |
||
57 | return $this->prepare($text); |
||
58 | } |
||
59 | |||
60 | protected function getSecondText() |
||
61 | { |
||
62 | $text = '<comp> |
||
63 | <deal id="1"> |
||
64 | <period id="2"></period> |
||
65 | </deal> |
||
66 | <deal id="2"> |
||
67 | <period id="1"></period> |
||
68 | </deal> |
||
69 | </comp>'; |
||
70 | return $this->prepare($text); |
||
71 | } |
||
72 | |||
73 | protected function prepare($text) |
||
74 | { |
||
75 | return preg_replace('/>[\n\s]+</', '><', $text); |
||
76 | } |
||
77 | } |
||
78 |