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

ReversibleTest::testToFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 3
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\Examples\Person;
6
use PHPUnit\Framework\TestCase;
7
8
class ReversibleTest extends TestCase
9
{
10
    public function testFromFirst()
11
    {
12
        $xml = '<?xml version="1.0"?>
13
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
14
';
15
16
        $document = new \DOMDocument();
17
        $document->loadXML($xml);
18
19
        $person = Person::fromXml($document);
20
21
        $document = new \DOMDocument();
22
        $node = $person->toXml($document);
23
        $document->appendChild($node);
24
        $result = $document->saveXML();
25
        $this->assertEquals($xml, $result);
26
    }
27
28
    public function testToFirst()
29
    {
30
        $person = $this->generateTestPerson();
31
32
        $documentGenerated = new \DOMDocument();
33
        $documentReversed = new \DOMDocument();
34
35
36
        $xml = $person->toXml($documentGenerated);
37
        $documentGenerated->appendChild($xml);
38
39
        $saved = $documentGenerated->saveXML();
40
41
        $documentParsed = new \DOMDocument();
42
        $documentParsed->loadXML($saved);
43
44
        $result = Person::fromXml($documentParsed)->toXml($documentReversed);
45
        $documentReversed->appendChild($result);
46
47
        $this->assertEquals($saved, $documentReversed->saveXML());
48
    }
49
50
    protected function generateTestPerson()
51
    {
52
        $person = $this->generateRandomPerson();
53
54
        $childPerson = $this->generateRandomPerson();
55
56
        $subChildPersonFirst = $this->generateRandomPerson();
57
58
        $subChildPersonSecond = $this->generateRandomPerson();
59
60
        $childPerson->xmlChildren = [
61
            $subChildPersonFirst,
62
            $subChildPersonSecond
63
        ];
64
65
        $person->xmlChildren = [$childPerson];
66
67
        return $person;
68
    }
69
70
    protected function generateRandomPerson()
71
    {
72
        return new Person(time() . mt_rand(), time() . mt_rand());
73
    }
74
}
75