Passed
Push — master ( 11e063...6d34f9 )
by Alexander
02:12
created

ReversibleTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testFromFirst() 0 17 1
A testToFirst() 0 22 1
A generateTestPerson() 0 19 1
A generateRandomPerson() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: horat1us
5
 * Date: 5/2/17
6
 * Time: 4:12 PM
7
 */
8
9
namespace Horat1us\Tests;
10
11
12
use Horat1us\Examples\Person;
13
14
class ReversibleTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testFromFirst()
17
    {
18
        $xml = '<?xml version="1.0"?>
19
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
20
';
21
22
        $document = new \DOMDocument();
23
        $document->loadXML($xml);
24
25
        $person = Person::fromXml($document);
26
27
        $document = new \DOMDocument();
28
        $node = $person->toXml($document);
29
        $document->appendChild($node);
30
        $result = $document->saveXML();
31
        $this->assertEquals($xml, $result);
32
    }
33
34
    public function testToFirst()
35
    {
36
        $person = $this->generateTestPerson();
37
38
        $documentGenerated = new \DOMDocument();
39
        $documentReversed = new \DOMDocument();
40
41
42
        $xml = $person->toXml($documentGenerated);
43
        $documentGenerated->appendChild($xml);
44
45
        $saved = $documentGenerated->saveXML();
46
47
        $documentParsed = new \DOMDocument();
48
        $documentParsed->loadXML($saved);
49
50
        $result = Person::fromXml($documentParsed)->toXml($documentReversed);
51
        $documentReversed->appendChild($result);
52
53
        $this->assertEquals($saved, $documentReversed->saveXML());
54
55
    }
56
57
    protected function generateTestPerson()
58
    {
59
        $person = $this->generateRandomPerson();
60
61
        $childPerson = $this->generateRandomPerson();
62
63
        $subChildPersonFirst = $this->generateRandomPerson();
64
65
        $subChildPersonSecond = $this->generateRandomPerson();
66
67
        $childPerson->xmlChildren = [
68
            $subChildPersonFirst,
69
            $subChildPersonSecond
70
        ];
71
72
        $person->xmlChildren = [$childPerson];
73
74
        return $person;
75
    }
76
77
    protected function generateRandomPerson()
78
    {
79
        return new Person(time() . mt_rand(), time() . mt_rand());
80
    }
81
}