-
Notifications
You must be signed in to change notification settings - Fork 58
/
XMLWriterTest.php
190 lines (165 loc) · 5.98 KB
/
XMLWriterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/Common/contributors.
*
* @see https://github.com/PHPOffice/Common
*
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common\Tests;
use PhpOffice\Common\XMLWriter;
/**
* Test class for XMLWriter
*
* @coversDefaultClass \PhpOffice\Common\XMLWriter
*/
class XMLWriterTest extends \PHPUnit\Framework\TestCase
{
public function testConstruct(): void
{
// Memory
$object = new XMLWriter();
$object->startElement('element');
$object->text('AAA');
$object->endElement();
$this->assertEquals('<element>AAA</element>' . chr(10), $object->getData());
// Disk
$object = new XMLWriter(XMLWriter::STORAGE_DISK);
$object->startElement('element');
$object->text('BBB');
$object->endElement();
$this->assertEquals('<element>BBB</element>' . chr(10), $object->getData());
}
public function testConstructCompatibility(): void
{
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, false);
$object->startElement('element');
$object->startElement('sub');
$object->text('CCC');
$object->endElement();
$object->endElement();
$this->assertEquals(
'<element>' . PHP_EOL . ' <sub>CCC</sub>' . PHP_EOL . '</element>' . PHP_EOL,
$object->getData()
);
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, true);
$object->startElement('element');
$object->startElement('sub');
$object->text('CCC');
$object->endElement();
$object->endElement();
$this->assertEquals(
'<element><sub>CCC</sub></element>',
$object->getData()
);
}
public function testWriteAttribute(): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttribute('name', 'value');
$xmlWriter->endElement();
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}
public function testWriteAttributeIf(): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttributeIf(true, 'name', 'value');
$xmlWriter->endElement();
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttributeIf(false, 'name', 'value');
$xmlWriter->endElement();
$this->assertSame('<element/>' . chr(10), $xmlWriter->getData());
}
public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void
{
$value = 1.2;
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttribute('name', $value);
$xmlWriter->endElement();
// https://www.php.net/manual/en/language.types.string.php#language.types.string.casting
// As of PHP 8.0.0, the decimal point character is always ..
// Prior to PHP 8.0.0, the decimal point character is defined in the script's locale (category LC_NUMERIC).
setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de');
if (PHP_VERSION_ID > 80000) {
$this->assertSame('1.2', (string) $value);
} else {
$this->assertSame('1,2', (string) $value);
}
$this->assertSame('<element name="1.2"/>' . chr(10), $xmlWriter->getData());
}
public function testWriteElementBlock(): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', 'name');
$this->assertSame('<element name=""/>' . chr(10), $xmlWriter->getData());
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', 'name', 'value');
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', ['name' => 'value']);
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', ['name' => 'value'], 'value2');
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}
/**
* @dataProvider dataProviderWriteElementIf
*/
public function testWriteElementIf(bool $condition, ?string $attribute, ?string $value, string $expected): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementIf($condition, 'element', $attribute, $value);
$this->assertSame($expected, $xmlWriter->getData());
}
/**
* @return array<array<bool|string|null>>
*/
public function dataProviderWriteElementIf(): array
{
return [
[
false,
null,
null,
'',
],
[
true,
null,
null,
'<element/>' . chr(10),
],
[
true,
'attribute',
null,
'<element attribute=""/>' . chr(10),
],
[
true,
null,
'value',
'<element>value</element>' . chr(10),
],
[
true,
'attribute',
'value',
'<element attribute="value"/>' . chr(10),
],
];
}
}