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

AliasTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 2
cbo 5
dl 0
loc 94
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\Examples\Person;
6
use Horat1us\Services\XmlParserService;
7
use Horat1us\XmlConvertibleObject;
8
use PHPUnit\Framework\TestCase;
9
10
class AliasTest extends TestCase
11
{
12
    public function testWrongAliasClass()
13
    {
14
        $xml = '<?xml version="1.0"?>
15
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
16
';
17
        $document = new \DOMDocument();
18
        $document->loadXML($xml);
19
20
        $this->expectException(\UnexpectedValueException::class);
21
        Person::fromXml($document, [
22
            'Person' => \stdClass::class,
23
            'Some' => Person::class,
24
        ]);
25
    }
26
27
    public function testWrongAliasType()
28
    {
29
        $xml = '<?xml version="1.0"?>
30
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
31
';
32
        $document = new \DOMDocument();
33
        $document->loadXML($xml);
34
35
36
        $this->expectException(\UnexpectedValueException::class);
37
        Person::fromXml($document, [
38
            'Person' => 2,
39
            'Some' => Person::class
40
        ]);
41
    }
42
43
    public function testWrongAliasInstance()
44
    {
45
        $xml = '<?xml version="1.0"?>
46
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
47
';
48
        $document = new \DOMDocument();
49
        $document->loadXML($xml);
50
51
52
        $this->expectException(\UnexpectedValueException::class);
53
        Person::fromXml($document, [
54
            'Person' => $this,
55
            'Some' => Person::class,
56
        ]);
57
    }
58
59
    public function testCustomAlias()
60
    {
61
        $xml = '<?xml version="1.0"?>
62
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
63
';
64
        $document = new \DOMDocument();
65
        $document->loadXML($xml);
66
67
        $person = Person::fromXml($document, [
68
            'Person' => XmlConvertibleObject::class,
69
            'Custom' => Person::class,
70
        ]);
71
        $this->assertInstanceOf(XmlConvertibleObject::class, $person);
72
    }
73
74
    public function testCustomAliasInstance()
75
    {
76
        $xml = '<?xml version="1.0"?>
77
<Person name="Alexander" surname="Letnikow"><Head size="big" mind="small"/></Person>
78
';
79
        $document = new \DOMDocument();
80
        $document->loadXML($xml);
81
82
        $instance = new XmlConvertibleObject();
83
        $person = Person::fromXml($document, [
84
            'Person' => $instance,
85
            'Custom' => Person::class,
86
        ]);
87
        $this->assertInstanceOf(XmlConvertibleObject::class, $person);
88
        $this->assertNotEquals($instance, $person);
89
    }
90
91
    public function testAutoAlias()
92
    {
93
        $object = new XmlConvertibleObject('alias');
94
        $objectXml = $object->toXml();
95
96
        $service = new XmlParserService($objectXml, [
97
            $object
98
        ]);
99
        $reverse = $service->convert();
100
        $this->assertEquals($object->xmlElementName, $reverse->getXmlElementName());
101
    }
102
}
103