]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Util/XMLTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Util / XMLTest.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @category   Testing
38  * @package    PHPUnit
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
44
45  * @link       http://www.phpunit.de/
46  * @since      File available since Release 3.3.0
47  */
48
49 require_once 'PHPUnit/Framework/TestCase.php';
50
51 require_once 'PHPUnit/Util/XML.php';
52
53 /**
54  *
55  *
56  * @category   Testing
57  * @package    PHPUnit
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
66  */
67 class Util_XMLTest extends PHPUnit_Framework_TestCase
68 {
69     public function testAssertValidKeysValidKeys()
70     {
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);
75
76         $this->assertEquals($expected, $validated);
77     }
78
79     public function testAssertValidKeysValidKeysEmpty()
80     {
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);
85
86         $this->assertEquals($expected, $validated);
87     }
88
89     public function testAssertValidKeysDefaultValuesA()
90     {
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);
95
96         $this->assertEquals($expected, $validated);
97     }
98
99     public function testAssertValidKeysDefaultValuesB()
100     {
101         $options   = array();
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);
105
106         $this->assertEquals($expected, $validated);
107     }
108
109     public function testAssertValidKeysInvalidKey()
110     {
111         $options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
112         $valid   = array('testA', 'testB', 'testC');
113
114         try {
115             $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
116             $this->fail();
117         }
118
119         catch (InvalidArgumentException $e) {
120             $this->assertEquals('Unknown key(s): testD', $e->getMessage());
121         }
122     }
123
124     public function testAssertValidKeysInvalidKeys()
125     {
126         $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
127         $valid   = array('testA', 'testB', 'testC');
128
129         try {
130             $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
131             $this->fail();
132         }
133
134         catch (InvalidArgumentException $e) {
135             $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
136         }
137     }
138
139     public function testConvertAssertSelect()
140     {
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',
144                            'id'    => 'folder',
145                            'class' => 'open',
146                            'descendant' => array('tag'        => 'a',
147                                                  'class'      => 'selected big',
148                                                  'attributes' => array('href'  => 'http://www.xerox.com',
149                                                                        'title' => 'xerox'),
150                                                  'child'      => array('tag' => 'span')));
151          $this->assertEquals($tag, $converted);
152     }
153
154     public function testConvertAssertSelectElt()
155     {
156         $selector  = 'div';
157         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
158         $tag       = array('tag' => 'div');
159
160         $this->assertEquals($tag, $converted);
161     }
162
163     public function testConvertAssertClass()
164     {
165         $selector  = '.foo';
166         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
167         $tag       = array('class' => 'foo');
168
169         $this->assertEquals($tag, $converted);
170     }
171
172     public function testConvertAssertId()
173     {
174         $selector  = '#foo';
175         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
176         $tag       = array('id' => 'foo');
177
178         $this->assertEquals($tag, $converted);
179     }
180
181     public function testConvertAssertAttribute()
182     {
183         $selector  = '[foo="bar"]';
184         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
185         $tag       = array('attributes' => array('foo' => 'bar'));
186
187         $this->assertEquals($tag, $converted);
188     }
189
190     public function testConvertAssertAttributeSpaces()
191     {
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);
198     }
199
200     public function testConvertAssertAttributeMultipleSpaces()
201     {
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);
208     }
209
210     public function testConvertAssertSelectEltClass()
211     {
212         $selector  = 'div.foo';
213         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
214         $tag       = array('tag' => 'div', 'class' => 'foo');
215
216         $this->assertEquals($tag, $converted);
217     }
218
219     public function testConvertAssertSelectEltId()
220     {
221         $selector  = 'div#foo';
222         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
223         $tag       = array('tag' => 'div', 'id' => 'foo');
224
225         $this->assertEquals($tag, $converted);
226     }
227
228     public function testConvertAssertSelectEltAttrEqual()
229     {
230         $selector  = 'div[foo="bar"]';
231         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
232         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
233
234         $this->assertEquals($tag, $converted);
235     }
236
237     public function testConvertAssertSelectEltMultiAttrEqual()
238     {
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'));
242
243         $this->assertEquals($tag, $converted);
244     }
245
246     public function testConvertAssertSelectEltAttrHasOne()
247     {
248         $selector  = 'div[foo~="bar"]';
249         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
250         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
251
252         $this->assertEquals($tag, $converted);
253     }
254
255     public function testConvertAssertSelectEltAttrContains()
256     {
257         $selector  = 'div[foo*="bar"]';
258         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
259         $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
260
261         $this->assertEquals($tag, $converted);
262     }
263
264     public function testConvertAssertSelectEltChild()
265     {
266         $selector  = 'div > a';
267         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
268         $tag       = array('tag' => 'div', 'child' => array('tag' => 'a'));
269
270         $this->assertEquals($tag, $converted);
271     }
272
273     public function testConvertAssertSelectEltDescendant()
274     {
275         $selector  = 'div a';
276         $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
277         $tag       = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
278
279         $this->assertEquals($tag, $converted);
280     }
281
282     public function testConvertAssertSelectContent()
283     {
284         $selector  = '#foo';
285         $content   = 'div contents';
286         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
287         $tag       = array('id' => 'foo', 'content' => 'div contents');
288
289         $this->assertEquals($tag, $converted);
290     }
291
292     public function testConvertAssertSelectTrue()
293     {
294         $selector  = '#foo';
295         $content   = TRUE;
296         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
297         $tag       = array('id' => 'foo');
298
299         $this->assertEquals($tag, $converted);
300     }
301
302     public function testConvertAssertSelectFalse()
303     {
304         $selector  = '#foo';
305         $content   = FALSE;
306         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
307         $tag       = array('id' => 'foo');
308
309         $this->assertEquals($tag, $converted);
310     }
311
312     public function testConvertAssertNumber()
313     {
314         $selector  = '.foo';
315         $content   = 3;
316         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
317         $tag       = array('class' => 'foo');
318
319         $this->assertEquals($tag, $converted);
320     }
321
322     public function testConvertAssertRange()
323     {
324         $selector  = '#foo';
325         $content   = array('greater_than' => 5, 'less_than' => 10);
326         $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
327         $tag       = array('id' => 'foo');
328
329         $this->assertEquals($tag, $converted);
330     }
331 }
332 ?>