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