3
* This file is part of PHPUnit.
5
* (c) Sebastian Bergmann <sebastian@phpunit.de>
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
15
* @author Mike Naberezny <mike@maintainable.com>
16
* @author Derek DeVries <derek@maintainable.com>
17
* @author Sebastian Bergmann <sebastian@phpunit.de>
18
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
19
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
20
* @link http://www.phpunit.de/
21
* @since Class available since Release 3.3.0
22
* @covers PHPUnit_Util_XML
24
class Util_XMLTest extends PHPUnit_Framework_TestCase
26
public function testAssertValidKeysValidKeys()
28
$options = array('testA' => 1, 'testB' => 2, 'testC' => 3);
29
$valid = array('testA', 'testB', 'testC');
30
$expected = array('testA' => 1, 'testB' => 2, 'testC' => 3);
31
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
33
$this->assertEquals($expected, $validated);
36
public function testAssertValidKeysValidKeysEmpty()
38
$options = array('testA' => 1, 'testB' => 2);
39
$valid = array('testA', 'testB', 'testC');
40
$expected = array('testA' => 1, 'testB' => 2, 'testC' => null);
41
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
43
$this->assertEquals($expected, $validated);
46
public function testAssertValidKeysDefaultValuesA()
48
$options = array('testA' => 1, 'testB' => 2);
49
$valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
50
$expected = array('testA' => 1, 'testB' => 2, 'testC' => 25);
51
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
53
$this->assertEquals($expected, $validated);
56
public function testAssertValidKeysDefaultValuesB()
59
$valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
60
$expected = array('testA' => 23, 'testB' => 24, 'testC' => 25);
61
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
63
$this->assertEquals($expected, $validated);
66
public function testAssertValidKeysInvalidKey()
68
$options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
69
$valid = array('testA', 'testB', 'testC');
72
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
74
} catch (PHPUnit_Framework_Exception $e) {
75
$this->assertEquals('Unknown key(s): testD', $e->getMessage());
79
public function testAssertValidKeysInvalidKeys()
81
$options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
82
$valid = array('testA', 'testB', 'testC');
85
$validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
87
} catch (PHPUnit_Framework_Exception $e) {
88
$this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
92
public function testConvertAssertSelect()
94
$selector = 'div#folder.open a[href="http://www.xerox.com"][title="xerox"].selected.big > span + h1';
95
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
96
$tag = array('tag' => 'div',
99
'descendant' => array('tag' => 'a',
100
'class' => 'selected big',
101
'attributes' => array('href' => 'http://www.xerox.com',
103
'child' => array('tag' => 'span',
104
'adjacent-sibling' => array('tag' => 'h1'))));
105
$this->assertEquals($tag, $converted);
108
public function testConvertAssertSelectElt()
111
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
112
$tag = array('tag' => 'div');
114
$this->assertEquals($tag, $converted);
117
public function testConvertAssertClass()
120
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
121
$tag = array('class' => 'foo');
123
$this->assertEquals($tag, $converted);
126
public function testConvertAssertId()
129
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
130
$tag = array('id' => 'foo');
132
$this->assertEquals($tag, $converted);
135
public function testConvertAssertAttribute()
137
$selector = '[foo="bar"]';
138
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
139
$tag = array('attributes' => array('foo' => 'bar'));
141
$this->assertEquals($tag, $converted);
144
public function testConvertAssertAttributeSpaces()
146
$selector = '[foo="bar baz"] div[value="foo bar"]';
147
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
148
$tag = array('attributes' => array('foo' => 'bar baz'),
149
'descendant' => array('tag' => 'div',
150
'attributes' => array('value' => 'foo bar')));
151
$this->assertEquals($tag, $converted);
154
public function testConvertAssertAttributeMultipleSpaces()
156
$selector = '[foo="bar baz"] div[value="foo bar baz"]';
157
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
158
$tag = array('attributes' => array('foo' => 'bar baz'),
159
'descendant' => array('tag' => 'div',
160
'attributes' => array('value' => 'foo bar baz')));
161
$this->assertEquals($tag, $converted);
164
public function testConvertAssertSelectEltClass()
166
$selector = 'div.foo';
167
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
168
$tag = array('tag' => 'div', 'class' => 'foo');
170
$this->assertEquals($tag, $converted);
173
public function testConvertAssertSelectEltId()
175
$selector = 'div#foo';
176
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
177
$tag = array('tag' => 'div', 'id' => 'foo');
179
$this->assertEquals($tag, $converted);
182
public function testConvertAssertSelectEltAttrEqual()
184
$selector = 'div[foo="bar"]';
185
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
186
$tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
188
$this->assertEquals($tag, $converted);
191
public function testConvertAssertSelectEltMultiAttrEqual()
193
$selector = 'div[foo="bar"][baz="fob"]';
194
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
195
$tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar', 'baz' => 'fob'));
197
$this->assertEquals($tag, $converted);
200
public function testConvertAssertSelectEltAttrHasOne()
202
$selector = 'div[foo~="bar"]';
203
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
204
$tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
206
$this->assertEquals($tag, $converted);
209
public function testConvertAssertSelectEltAttrContains()
211
$selector = 'div[foo*="bar"]';
212
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
213
$tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
215
$this->assertEquals($tag, $converted);
218
public function testConvertAssertSelectEltChild()
220
$selector = 'div > a';
221
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
222
$tag = array('tag' => 'div', 'child' => array('tag' => 'a'));
224
$this->assertEquals($tag, $converted);
227
public function testConvertAssertSelectEltAdjacentSibling()
229
$selector = 'div + a';
230
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
231
$tag = array('tag' => 'div', 'adjacent-sibling' => array('tag' => 'a'));
233
$this->assertEquals($tag, $converted);
236
public function testConvertAssertSelectEltDescendant()
239
$converted = PHPUnit_Util_XML::convertSelectToTag($selector);
240
$tag = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
242
$this->assertEquals($tag, $converted);
245
public function testConvertAssertSelectContent()
248
$content = 'div contents';
249
$converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
250
$tag = array('id' => 'foo', 'content' => 'div contents');
252
$this->assertEquals($tag, $converted);
255
public function testConvertAssertSelectTrue()
259
$converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
260
$tag = array('id' => 'foo');
262
$this->assertEquals($tag, $converted);
265
public function testConvertAssertSelectFalse()
269
$converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
270
$tag = array('id' => 'foo');
272
$this->assertEquals($tag, $converted);
275
public function testConvertAssertNumber()
279
$converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
280
$tag = array('class' => 'foo');
282
$this->assertEquals($tag, $converted);
285
public function testConvertAssertRange()
288
$content = array('greater_than' => 5, 'less_than' => 10);
289
$converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
290
$tag = array('id' => 'foo');
292
$this->assertEquals($tag, $converted);
296
* @dataProvider charProvider
298
public function testPrepareString($char)
302
$escapedString = PHPUnit_Util_XML::prepareString($char);
303
$xml = "<?xml version='1.0' encoding='UTF-8' ?><tag>$escapedString</tag>";
304
$dom = new DomDocument('1.0', 'UTF-8');
308
} catch (Exception $e) {
311
$this->assertNull($e, sprintf(
312
'PHPUnit_Util_XML::prepareString("\x%02x") should not crash DomDocument',
317
public function charProvider()
321
for ($i = 0; $i < 256; $i++) {
322
$data[] = array(chr($i));