Yaffs site version 1.1
[yaffs-website] / vendor / phpunit / phpunit / tests / Util / XMLTest.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * @since      Class available since Release 3.3.0
13  * @covers     PHPUnit_Util_XML
14  */
15 class Util_XMLTest extends PHPUnit_Framework_TestCase
16 {
17     public function testAssertValidKeysValidKeys()
18     {
19         $options   = array('testA' => 1, 'testB' => 2, 'testC' => 3);
20         $valid     = array('testA', 'testB', 'testC');
21         $expected  = array('testA' => 1, 'testB' => 2, 'testC' => 3);
22         $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
23
24         $this->assertEquals($expected, $validated);
25     }
26
27     public function testAssertValidKeysValidKeysEmpty()
28     {
29         $options   = array('testA' => 1, 'testB' => 2);
30         $valid     = array('testA', 'testB', 'testC');
31         $expected  = array('testA' => 1, 'testB' => 2, 'testC' => null);
32         $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
33
34         $this->assertEquals($expected, $validated);
35     }
36
37     public function testAssertValidKeysDefaultValuesA()
38     {
39         $options   = array('testA' => 1, 'testB' => 2);
40         $valid     = array('testA' => 23, 'testB' => 24, 'testC' => 25);
41         $expected  = array('testA' => 1, 'testB' => 2, 'testC' => 25);
42         $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
43
44         $this->assertEquals($expected, $validated);
45     }
46
47     public function testAssertValidKeysDefaultValuesB()
48     {
49         $options   = array();
50         $valid     = array('testA' => 23, 'testB' => 24, 'testC' => 25);
51         $expected  = array('testA' => 23, 'testB' => 24, 'testC' => 25);
52         $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
53
54         $this->assertEquals($expected, $validated);
55     }
56
57     public function testAssertValidKeysInvalidKey()
58     {
59         $options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
60         $valid   = array('testA', 'testB', 'testC');
61
62         try {
63             $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
64             $this->fail();
65         } catch (PHPUnit_Framework_Exception $e) {
66             $this->assertEquals('Unknown key(s): testD', $e->getMessage());
67         }
68     }
69
70     public function testAssertValidKeysInvalidKeys()
71     {
72         $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
73         $valid   = array('testA', 'testB', 'testC');
74
75         try {
76             $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
77             $this->fail();
78         } catch (PHPUnit_Framework_Exception $e) {
79             $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
80         }
81     }
82
83     public function testConvertAssertSelect()
84     {
85         $selector  = 'div#folder.open a[href="http://www.xerox.com"][title="xerox"].selected.big > span + h1';
86         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
87         $tag       = array('tag'        => 'div',
88                            'id'         => 'folder',
89                            'class'      => 'open',
90                            'descendant' => array('tag'        => 'a',
91                                                  'class'      => 'selected big',
92                                                  'attributes' => array('href'             => 'http://www.xerox.com',
93                                                                        'title'            => 'xerox'),
94                                                  'child'      => array('tag'              => 'span',
95                                                                        'adjacent-sibling' => array('tag' => 'h1'))));
96         $this->assertEquals($tag, $converted);
97     }
98
99     public function testConvertAssertSelectElt()
100     {
101         $selector  = 'div';
102         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
103         $tag       = array('tag' => 'div');
104
105         $this->assertEquals($tag, $converted);
106     }
107
108     public function testConvertAssertClass()
109     {
110         $selector  = '.foo';
111         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
112         $tag       = array('class' => 'foo');
113
114         $this->assertEquals($tag, $converted);
115     }
116
117     public function testConvertAssertId()
118     {
119         $selector  = '#foo';
120         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
121         $tag       = array('id' => 'foo');
122
123         $this->assertEquals($tag, $converted);
124     }
125
126     public function testConvertAssertAttribute()
127     {
128         $selector  = '[foo="bar"]';
129         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
130         $tag       = array('attributes' => array('foo' => 'bar'));
131
132         $this->assertEquals($tag, $converted);
133     }
134
135     public function testConvertAssertAttributeSpaces()
136     {
137         $selector  = '[foo="bar baz"] div[value="foo bar"]';
138         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
139         $tag       = array('attributes' => array('foo'        => 'bar baz'),
140                            'descendant' => array('tag'        => 'div',
141                                                  'attributes' => array('value' => 'foo bar')));
142         $this->assertEquals($tag, $converted);
143     }
144
145     public function testConvertAssertAttributeMultipleSpaces()
146     {
147         $selector  = '[foo="bar baz"] div[value="foo bar baz"]';
148         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
149         $tag       = array('attributes' => array('foo'        => 'bar baz'),
150                           'descendant'  => array('tag'        => 'div',
151                                                 'attributes'  => array('value' => 'foo bar baz')));
152         $this->assertEquals($tag, $converted);
153     }
154
155     public function testConvertAssertSelectEltClass()
156     {
157         $selector  = 'div.foo';
158         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
159         $tag       = array('tag' => 'div', 'class' => 'foo');
160
161         $this->assertEquals($tag, $converted);
162     }
163
164     public function testConvertAssertSelectEltId()
165     {
166         $selector  = 'div#foo';
167         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
168         $tag       = array('tag' => 'div', 'id' => 'foo');
169
170         $this->assertEquals($tag, $converted);
171     }
172
173     public function testConvertAssertSelectEltAttrEqual()
174     {
175         $selector  = 'div[foo="bar"]';
176         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
177         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
178
179         $this->assertEquals($tag, $converted);
180     }
181
182     public function testConvertAssertSelectEltMultiAttrEqual()
183     {
184         $selector  = 'div[foo="bar"][baz="fob"]';
185         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
186         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'bar', 'baz' => 'fob'));
187
188         $this->assertEquals($tag, $converted);
189     }
190
191     public function testConvertAssertSelectEltAttrHasOne()
192     {
193         $selector  = 'div[foo~="bar"]';
194         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
195         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
196
197         $this->assertEquals($tag, $converted);
198     }
199
200     public function testConvertAssertSelectEltAttrContains()
201     {
202         $selector  = 'div[foo*="bar"]';
203         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
204         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
205
206         $this->assertEquals($tag, $converted);
207     }
208
209     public function testConvertAssertSelectEltChild()
210     {
211         $selector  = 'div > a';
212         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
213         $tag       = array('tag' => 'div', 'child' => array('tag' => 'a'));
214
215         $this->assertEquals($tag, $converted);
216     }
217
218     public function testConvertAssertSelectEltAdjacentSibling()
219     {
220         $selector  = 'div + a';
221         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
222         $tag       = array('tag' => 'div', 'adjacent-sibling' => array('tag' => 'a'));
223
224         $this->assertEquals($tag, $converted);
225     }
226
227     public function testConvertAssertSelectEltDescendant()
228     {
229         $selector  = 'div a';
230         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
231         $tag       = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
232
233         $this->assertEquals($tag, $converted);
234     }
235
236     public function testConvertAssertSelectContent()
237     {
238         $selector  = '#foo';
239         $content   = 'div contents';
240         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
241         $tag       = array('id' => 'foo', 'content' => 'div contents');
242
243         $this->assertEquals($tag, $converted);
244     }
245
246     public function testConvertAssertSelectTrue()
247     {
248         $selector  = '#foo';
249         $content   = true;
250         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
251         $tag       = array('id' => 'foo');
252
253         $this->assertEquals($tag, $converted);
254     }
255
256     public function testConvertAssertSelectFalse()
257     {
258         $selector  = '#foo';
259         $content   = false;
260         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
261         $tag       = array('id' => 'foo');
262
263         $this->assertEquals($tag, $converted);
264     }
265
266     public function testConvertAssertNumber()
267     {
268         $selector  = '.foo';
269         $content   = 3;
270         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
271         $tag       = array('class' => 'foo');
272
273         $this->assertEquals($tag, $converted);
274     }
275
276     public function testConvertAssertRange()
277     {
278         $selector  = '#foo';
279         $content   = array('greater_than' => 5, 'less_than' => 10);
280         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
281         $tag       = array('id' => 'foo');
282
283         $this->assertEquals($tag, $converted);
284     }
285
286     /**
287      * @dataProvider charProvider
288      */
289     public function testPrepareString($char)
290     {
291         $e = null;
292
293         $escapedString = PHPUnit_Util_XML::prepareString($char);
294         $xml           = "<?xml version='1.0' encoding='UTF-8' ?><tag>$escapedString</tag>";
295         $dom           = new DomDocument('1.0', 'UTF-8');
296
297         try {
298             $dom->loadXML($xml);
299         } catch (Exception $e) {
300         }
301
302         $this->assertNull($e, sprintf(
303             'PHPUnit_Util_XML::prepareString("\x%02x") should not crash DomDocument',
304             ord($char)
305         ));
306     }
307
308     public function charProvider()
309     {
310         $data = array();
311
312         for ($i = 0; $i < 256; $i++) {
313             $data[] = array(chr($i));
314         }
315
316         return $data;
317     }
318
319     /**
320      * @expectedException PHPUnit_Framework_Exception
321      * @expectedExceptionMessage Could not load XML from empty string
322      */
323     public function testLoadEmptyString()
324     {
325         PHPUnit_Util_XML::load('');
326     }
327
328     /**
329      * @expectedException PHPUnit_Framework_Exception
330      * @expectedExceptionMessage Could not load XML from array
331      */
332     public function testLoadArray()
333     {
334         PHPUnit_Util_XML::load(array(1, 2, 3));
335     }
336
337     /**
338      * @expectedException PHPUnit_Framework_Exception
339      * @expectedExceptionMessage Could not load XML from boolean
340      */
341     public function testLoadBoolean()
342     {
343         PHPUnit_Util_XML::load(false);
344     }
345
346     public function testNestedXmlToVariable()
347     {
348         $xml = '<array><element key="a"><array><element key="b"><string>foo</string></element></array></element><element key="c"><string>bar</string></element></array>';
349         $dom = new DOMDocument();
350         $dom->loadXML($xml);
351
352         $expected = array(
353             'a' => array(
354                 'b' => 'foo',
355             ),
356             'c' => 'bar',
357         );
358
359         $actual = PHPUnit_Util_XML::xmlToVariable($dom->documentElement);
360
361         $this->assertSame($expected, $actual);
362     }
363 }