Completed
Push — master ( c08922...87312f )
by Alexander
02:20
created

ReversibleTest::testToFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 12
nc 1
nop 0
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 = new Person();
60
61
        $person->name = 'Alexander';
62
        $person->surname = 'Letnikow';
63
64
        $childPerson = new Person();
65
        $childPerson->name = 'Some';
66
        $childPerson->surname = 'Name';
67
68
        $subChildPersonFirst = new Person();
69
        $subChildPersonFirst->name = 'Sub';
70
        $subChildPersonFirst->surname = 'First';
71
72
        $subChildPersonSecond = new Person();
73
        $subChildPersonSecond->name = 'Sub';
74
        $subChildPersonSecond->surname = 'Second';
75
76
        $childPerson->xmlChildren = [
77
            $subChildPersonFirst,
78
            $subChildPersonSecond
79
        ];
80
81
        $person->xmlChildren = [$childPerson];
82
83
        return $person;
84
    }
85
}