5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Mike Naberezny <mike@maintainable.com>
40 * @author Derek DeVries <derek@maintainable.com>
41 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
42 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
43 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45 * @link http://www.phpunit.de/
46 * @since File available since Release 3.3.0
49 require_once 'PHPUnit/Framework/TestCase.php';
51 require_once 'PHPUnit/Util/XML.php';
58 * @author Mike Naberezny <mike@maintainable.com>
59 * @author Derek DeVries <derek@maintainable.com>
60 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
61 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
62 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
63 * @version Release: 3.3.17
64 * @link http://www.phpunit.de/
65 * @since Class available since Release 3.3.0
67 class Util_XMLTest extends PHPUnit_Framework_TestCase
69 public function testAssertValidKeysValidKeys()
71 $options = array('testA' => 1, 'testB' => 2, 'testC' => 3);
72 $valid = array('testA', 'testB', 'testC');
73 $expected = array('testA' => 1, 'testB' => 2, 'testC' => 3);
74 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
76 $this->assertEquals($expected, $validated);
79 public function testAssertValidKeysValidKeysEmpty()
81 $options = array('testA' => 1, 'testB' => 2);
82 $valid = array('testA', 'testB', 'testC');
83 $expected = array('testA' => 1, 'testB' => 2, 'testC' => NULL);
84 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
86 $this->assertEquals($expected, $validated);
89 public function testAssertValidKeysDefaultValuesA()
91 $options = array('testA' => 1, 'testB' => 2);
92 $valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
93 $expected = array('testA' => 1, 'testB' => 2, 'testC' => 25);
94 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
96 $this->assertEquals($expected, $validated);
99 public function testAssertValidKeysDefaultValuesB()
102 $valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
103 $expected = array('testA' => 23, 'testB' => 24, 'testC' => 25);
104 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
106 $this->assertEquals($expected, $validated);
109 public function testAssertValidKeysInvalidKey()
111 $options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
112 $valid = array('testA', 'testB', 'testC');
115 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
119 catch (InvalidArgumentException $e) {
120 $this->assertEquals('Unknown key(s): testD', $e->getMessage());
124 public function testAssertValidKeysInvalidKeys()
126 $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
127 $valid = array('testA', 'testB', 'testC');
130 $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
134 catch (InvalidArgumentException $e) {
135 $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
139 public function testConvertAssertSelect()
141 $selector = 'div#folder.open a[href="http://www.xerox.com"][title="xerox"].selected.big > span';
142 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
143 $tag = array('tag' => 'div',
146 'descendant' => array('tag' => 'a',
147 'class' => 'selected big',
148 'attributes' => array('href' => 'http://www.xerox.com',
150 'child' => array('tag' => 'span')));
151 $this->assertEquals($tag, $converted);
154 public function testConvertAssertSelectElt()
157 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
158 $tag = array('tag' => 'div');
160 $this->assertEquals($tag, $converted);
163 public function testConvertAssertClass()
166 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
167 $tag = array('class' => 'foo');
169 $this->assertEquals($tag, $converted);
172 public function testConvertAssertId()
175 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
176 $tag = array('id' => 'foo');
178 $this->assertEquals($tag, $converted);
181 public function testConvertAssertAttribute()
183 $selector = '[foo="bar"]';
184 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
185 $tag = array('attributes' => array('foo' => 'bar'));
187 $this->assertEquals($tag, $converted);
190 public function testConvertAssertAttributeSpaces()
192 $selector = '[foo="bar baz"] div[value="foo bar"]';
193 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
194 $tag = array('attributes' => array('foo' => 'bar baz'),
195 'descendant' => array('tag' => 'div',
196 'attributes' => array('value' => 'foo bar')));
197 $this->assertEquals($tag, $converted);
200 public function testConvertAssertAttributeMultipleSpaces()
202 $selector = '[foo="bar baz"] div[value="foo bar baz"]';
203 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
204 $tag = array('attributes' => array('foo' => 'bar baz'),
205 'descendant' => array('tag' => 'div',
206 'attributes' => array('value' => 'foo bar baz')));
207 $this->assertEquals($tag, $converted);
210 public function testConvertAssertSelectEltClass()
212 $selector = 'div.foo';
213 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
214 $tag = array('tag' => 'div', 'class' => 'foo');
216 $this->assertEquals($tag, $converted);
219 public function testConvertAssertSelectEltId()
221 $selector = 'div#foo';
222 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
223 $tag = array('tag' => 'div', 'id' => 'foo');
225 $this->assertEquals($tag, $converted);
228 public function testConvertAssertSelectEltAttrEqual()
230 $selector = 'div[foo="bar"]';
231 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
232 $tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
234 $this->assertEquals($tag, $converted);
237 public function testConvertAssertSelectEltMultiAttrEqual()
239 $selector = 'div[foo="bar"][baz="fob"]';
240 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
241 $tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar', 'baz' => 'fob'));
243 $this->assertEquals($tag, $converted);
246 public function testConvertAssertSelectEltAttrHasOne()
248 $selector = 'div[foo~="bar"]';
249 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
250 $tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
252 $this->assertEquals($tag, $converted);
255 public function testConvertAssertSelectEltAttrContains()
257 $selector = 'div[foo*="bar"]';
258 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
259 $tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
261 $this->assertEquals($tag, $converted);
264 public function testConvertAssertSelectEltChild()
266 $selector = 'div > a';
267 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
268 $tag = array('tag' => 'div', 'child' => array('tag' => 'a'));
270 $this->assertEquals($tag, $converted);
273 public function testConvertAssertSelectEltDescendant()
276 $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
277 $tag = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
279 $this->assertEquals($tag, $converted);
282 public function testConvertAssertSelectContent()
285 $content = 'div contents';
286 $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
287 $tag = array('id' => 'foo', 'content' => 'div contents');
289 $this->assertEquals($tag, $converted);
292 public function testConvertAssertSelectTrue()
296 $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
297 $tag = array('id' => 'foo');
299 $this->assertEquals($tag, $converted);
302 public function testConvertAssertSelectFalse()
306 $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
307 $tag = array('id' => 'foo');
309 $this->assertEquals($tag, $converted);
312 public function testConvertAssertNumber()
316 $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
317 $tag = array('class' => 'foo');
319 $this->assertEquals($tag, $converted);
322 public function testConvertAssertRange()
325 $content = array('greater_than' => 5, 'less_than' => 10);
326 $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
327 $tag = array('id' => 'foo');
329 $this->assertEquals($tag, $converted);