Test Failed
Push — master ( 811c27...aa94e6 )
by Alexander
06:04
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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testXmlEqual() 0 4 1
A testNotEqualXml() 0 5 1
A diff() 0 22 2
A getFirstText() 0 14 1
A getSecondText() 0 12 1
A prepare() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: horat1us
5
 * Date: 7/10/17
6
 * Time: 1:13 PM
7
 */
8
9
namespace Horat1us\Tests;
10
11
12
use Horat1us\XmlConvertibleObject;
13
14
class RealDiffTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testXmlEqual()
17
    {
18
        $this->assertNull($this->diff($this->getFirstText(), $this->getSecondText()));
19
    }
20
21
    public function testNotEqualXml()
22
    {
23
        $diff = $this->diff($this->getFirstText(), $this->getSecondText());
0 ignored issues
show
Unused Code introduced by
$diff is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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