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

RealDiffTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 69
rs 10
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\XmlConvertibleObject;
6
use PHPUnit\Framework\TestCase;
7
8
class RealDiffTest extends TestCase
9
{
10
    public function testXmlEqual()
11
    {
12
        $this->assertNull($this->diff($this->getFirstText(), $this->getFirstText()));
13
    }
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