1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2016 Jan Eichhorn <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApaiIO\Test\ResponseTransformer; |
19
|
|
|
|
20
|
|
|
use ApaiIO\ResponseTransformer\XmlToArray; |
21
|
|
|
use ApaiIO\ResponseTransformer\XmlToDomDocument; |
22
|
|
|
use ApaiIO\ResponseTransformer\XmlToSimpleXmlObject; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
class ResponseTransformerTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testXmlToDomDocument() |
28
|
|
|
{ |
29
|
|
|
$transformer = new XmlToDomDocument(); |
30
|
|
|
|
31
|
|
|
$xml = $this->getSampleXMLResponse(); |
32
|
|
|
|
33
|
|
|
$document = $transformer->transform($xml); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf('\DOMDocument', $document); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testXmlToSimpleXMLObject() |
39
|
|
|
{ |
40
|
|
|
$transformer = new XmlToSimpleXmlObject(); |
41
|
|
|
|
42
|
|
|
$sampleXML = $this->getSampleXMLResponse(); |
43
|
|
|
|
44
|
|
|
$simpleXML = $transformer->transform($sampleXML); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf('\SimpleXMLElement', $simpleXML); |
47
|
|
|
$this->assertEquals('Wikipedia Städteverzeichnis', $simpleXML->titel); |
48
|
|
|
$this->assertEquals('Genf', $simpleXML->eintrag[0]->stichwort); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testXmlToArray() |
52
|
|
|
{ |
53
|
|
|
$transformer = new XmlToArray(); |
54
|
|
|
|
55
|
|
|
$sampleXML = $this->getSampleXMLResponse(); |
56
|
|
|
|
57
|
|
|
$array = $transformer->transform($sampleXML); |
58
|
|
|
|
59
|
|
|
$this->assertInternalType('array', $array); |
60
|
|
|
$this->assertEquals('Wikipedia Städteverzeichnis', $array['titel']); |
61
|
|
|
$this->assertEquals('Genf', $array['eintrag']['0']['stichwort']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
private function getSampleXMLResponse() |
68
|
|
|
{ |
69
|
|
|
return <<<EOF |
70
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
71
|
|
|
<verzeichnis> |
72
|
|
|
<titel>Wikipedia Städteverzeichnis</titel> |
73
|
|
|
<eintrag> |
74
|
|
|
<stichwort>Genf</stichwort> |
75
|
|
|
<eintragstext>Genf ist der Sitz von ...</eintragstext> |
76
|
|
|
</eintrag> |
77
|
|
|
<eintrag> |
78
|
|
|
<stichwort>Köln</stichwort> |
79
|
|
|
<eintragstext>Köln ist eine Stadt, die ...</eintragstext> |
80
|
|
|
</eintrag> |
81
|
|
|
</verzeichnis> |
82
|
|
|
EOF; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|