1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Util\Console\Helper\Table\Renderer; |
9
|
|
|
|
10
|
|
|
use DOMException; |
11
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
12
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class XmlRendererTest |
16
|
|
|
* |
17
|
|
|
* @covers N98\Util\Console\Helper\Table\Renderer\XmlRenderer |
18
|
|
|
* @package N98\Util\Console\Helper\Table\Renderer |
19
|
|
|
*/ |
20
|
|
|
class XmlRendererTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function creation() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$renderer = new XmlRenderer(); |
28
|
|
|
$this->assertInstanceOf(__NAMESPACE__ . '\\XmlRenderer', $renderer); |
29
|
|
|
|
30
|
|
|
$renderFactory = new RendererFactory(); |
31
|
|
|
|
32
|
|
|
$renderer = $renderFactory->create('xml'); |
33
|
|
|
$this->assertInstanceOf(__NAMESPACE__ . '\\XmlRenderer', $renderer); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
* @see tableRendering |
39
|
|
|
*/ |
40
|
|
|
public function provideTables() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
|
|
array( |
44
|
|
|
array( |
45
|
|
|
array( |
46
|
|
|
"column" => "Doors wide > open", |
47
|
|
|
), |
48
|
|
|
array( |
49
|
|
|
"column" => "null \0 bytes FTW", |
50
|
|
|
), |
51
|
|
|
), |
52
|
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
53
|
|
|
<table> |
54
|
|
|
<headers> |
55
|
|
|
<header>column</header> |
56
|
|
|
</headers> |
57
|
|
|
<row> |
58
|
|
|
<column>Doors wide > open</column> |
59
|
|
|
</row> |
60
|
|
|
<row> |
61
|
|
|
<column encoding="base64">bnVsbCAAIGJ5dGVzIEZUVw==</column> |
62
|
|
|
</row> |
63
|
|
|
</table>', |
64
|
|
|
), |
65
|
|
|
array( |
66
|
|
|
array(), |
67
|
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
68
|
|
|
<table> |
69
|
|
|
<!--intentionally left blank, the table is empty--> |
70
|
|
|
</table>', |
71
|
|
|
), |
72
|
|
|
array( |
73
|
|
|
array( |
74
|
|
|
array('Column1' => 'Value A1', 'Column2' => 'A2 is another value that there is'), |
75
|
|
|
array(1, "multi\nline\nftw"), |
76
|
|
|
array("C1 cell here!", new \SimpleXMLElement('<r>PHP Magic->toString() test</r>')), |
77
|
|
|
), |
78
|
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
79
|
|
|
<table> |
80
|
|
|
<headers> |
81
|
|
|
<header>Column1</header> |
82
|
|
|
<header>Column2</header> |
83
|
|
|
</headers> |
84
|
|
|
<row> |
85
|
|
|
<Column1>Value A1</Column1> |
86
|
|
|
<Column2>A2 is another value that there is</Column2> |
87
|
|
|
</row> |
88
|
|
|
<row> |
89
|
|
|
<Column1>1</Column1> |
90
|
|
|
<Column2>multi |
91
|
|
|
line |
92
|
|
|
ftw</Column2> |
93
|
|
|
</row> |
94
|
|
|
<row> |
95
|
|
|
<Column1>C1 cell here!</Column1> |
96
|
|
|
<Column2>PHP Magic->toString() test</Column2> |
97
|
|
|
</row> |
98
|
|
|
</table>', |
99
|
|
|
), |
100
|
|
|
array( |
101
|
|
|
array(array("\x00" => "foo")), |
102
|
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
103
|
|
|
<table> |
104
|
|
|
<headers> |
105
|
|
|
<header></header> |
106
|
|
|
</headers> |
107
|
|
|
<row> |
108
|
|
|
<_>foo</_> |
109
|
|
|
</row> |
110
|
|
|
</table>', |
111
|
|
|
), |
112
|
|
|
array( |
113
|
|
|
array( |
114
|
|
|
array("foo" => "bar"), |
115
|
|
|
array("baz", "buz" => "here"), |
116
|
|
|
), |
117
|
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
118
|
|
|
<table> |
119
|
|
|
<headers> |
120
|
|
|
<header>foo</header> |
121
|
|
|
</headers> |
122
|
|
|
<row> |
123
|
|
|
<foo>bar</foo> |
124
|
|
|
</row> |
125
|
|
|
<row> |
126
|
|
|
<foo>baz</foo> |
127
|
|
|
<buz>here</buz> |
128
|
|
|
</row> |
129
|
|
|
</table>', |
130
|
|
|
), |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @test |
136
|
|
|
* @expectedException DOMException |
137
|
|
|
* @expectedExceptionMessage Invalid name '0' |
138
|
|
|
*/ |
139
|
|
|
public function invalidName() |
140
|
|
|
{ |
141
|
|
|
$renderer = new XmlRenderer(); |
142
|
|
|
$output = new NullOutput(); |
143
|
|
|
$renderer->render($output, array(array("foo"))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
* @expectedException RuntimeException |
149
|
|
|
* @expectedExceptionMessage Encoding error, only US-ASCII and UTF-8 supported, can not process ' |
150
|
|
|
*/ |
151
|
|
|
public function invalidEncoding() |
152
|
|
|
{ |
153
|
|
|
$renderer = new XmlRenderer(); |
154
|
|
|
$output = new NullOutput(); |
155
|
|
|
$renderer->render($output, array(array("\xC1" => "foo"))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @test |
160
|
|
|
* @dataProvider provideTables |
161
|
|
|
*/ |
162
|
|
|
public function tableRendering($rows, $expected) |
163
|
|
|
{ |
164
|
|
|
$renderer = new XmlRenderer(); |
165
|
|
|
$output = new StreamOutput(fopen('php://memory', 'w', false)); |
166
|
|
|
|
167
|
|
|
$renderer->render($output, $rows); |
168
|
|
|
|
169
|
|
|
$this->assertEquals($expected . "\n", $this->getOutputBuffer($output)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.