|
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
|
|
|
|
|
24
|
|
|
class ResponseTransformerTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
public function testXmlToDomDocument() |
|
27
|
|
|
{ |
|
28
|
|
|
$transformer = new XmlToDomDocument(); |
|
29
|
|
|
|
|
30
|
|
|
$xml = $this->getSampleXMLResponse(); |
|
31
|
|
|
|
|
32
|
|
|
$document = $transformer->transform($xml); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertInstanceOf('\DOMDocument', $document); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testXmlToSimpleXMLObject() |
|
38
|
|
|
{ |
|
39
|
|
|
$transformer = new XmlToSimpleXmlObject(); |
|
40
|
|
|
|
|
41
|
|
|
$sampleXML = $this->getSampleXMLResponse(); |
|
42
|
|
|
|
|
43
|
|
|
$simpleXML = $transformer->transform($sampleXML); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertInstanceOf('\SimpleXMLElement', $simpleXML); |
|
46
|
|
|
$this->assertEquals('Wikipedia Städteverzeichnis', $simpleXML->titel); |
|
47
|
|
|
$this->assertEquals('Genf', $simpleXML->eintrag[0]->stichwort); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testXmlToArray() |
|
51
|
|
|
{ |
|
52
|
|
|
$transformer = new XmlToArray(); |
|
53
|
|
|
|
|
54
|
|
|
$sampleXML = $this->getSampleXMLResponse(); |
|
55
|
|
|
|
|
56
|
|
|
$array = $transformer->transform($sampleXML); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertInternalType('array', $array); |
|
59
|
|
|
$this->assertEquals('Wikipedia Städteverzeichnis', $array['titel']); |
|
60
|
|
|
$this->assertEquals('Genf', $array['eintrag']['0']['stichwort']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getSampleXMLResponse() |
|
67
|
|
|
{ |
|
68
|
|
|
return <<<EOF |
|
69
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
|
70
|
|
|
<verzeichnis> |
|
71
|
|
|
<titel>Wikipedia Städteverzeichnis</titel> |
|
72
|
|
|
<eintrag> |
|
73
|
|
|
<stichwort>Genf</stichwort> |
|
74
|
|
|
<eintragstext>Genf ist der Sitz von ...</eintragstext> |
|
75
|
|
|
</eintrag> |
|
76
|
|
|
<eintrag> |
|
77
|
|
|
<stichwort>Köln</stichwort> |
|
78
|
|
|
<eintragstext>Köln ist eine Stadt, die ...</eintragstext> |
|
79
|
|
|
</eintrag> |
|
80
|
|
|
</verzeichnis> |
|
81
|
|
|
EOF; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|