~ballot/wordpress/openstack-objectstorage-breaking-insight

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/tests/Util/XMLTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *
 
13
 *
 
14
 * @package    PHPUnit
 
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
 
23
 */
 
24
class Util_XMLTest extends PHPUnit_Framework_TestCase
 
25
{
 
26
    public function testAssertValidKeysValidKeys()
 
27
    {
 
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);
 
32
 
 
33
        $this->assertEquals($expected, $validated);
 
34
    }
 
35
 
 
36
    public function testAssertValidKeysValidKeysEmpty()
 
37
    {
 
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);
 
42
 
 
43
        $this->assertEquals($expected, $validated);
 
44
    }
 
45
 
 
46
    public function testAssertValidKeysDefaultValuesA()
 
47
    {
 
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);
 
52
 
 
53
        $this->assertEquals($expected, $validated);
 
54
    }
 
55
 
 
56
    public function testAssertValidKeysDefaultValuesB()
 
57
    {
 
58
        $options   = array();
 
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);
 
62
 
 
63
        $this->assertEquals($expected, $validated);
 
64
    }
 
65
 
 
66
    public function testAssertValidKeysInvalidKey()
 
67
    {
 
68
        $options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
 
69
        $valid   = array('testA', 'testB', 'testC');
 
70
 
 
71
        try {
 
72
            $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
 
73
            $this->fail();
 
74
        } catch (PHPUnit_Framework_Exception $e) {
 
75
            $this->assertEquals('Unknown key(s): testD', $e->getMessage());
 
76
        }
 
77
    }
 
78
 
 
79
    public function testAssertValidKeysInvalidKeys()
 
80
    {
 
81
        $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
 
82
        $valid   = array('testA', 'testB', 'testC');
 
83
 
 
84
        try {
 
85
            $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
 
86
            $this->fail();
 
87
        } catch (PHPUnit_Framework_Exception $e) {
 
88
            $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
 
89
        }
 
90
    }
 
91
 
 
92
    public function testConvertAssertSelect()
 
93
    {
 
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',
 
97
                           'id'    => 'folder',
 
98
                           'class' => 'open',
 
99
                           'descendant' => array('tag'        => 'a',
 
100
                                                 'class'      => 'selected big',
 
101
                                                 'attributes' => array('href'  => 'http://www.xerox.com',
 
102
                                                                       'title' => 'xerox'),
 
103
                                                 'child'      => array('tag' => 'span',
 
104
                                                                       'adjacent-sibling' => array('tag' => 'h1'))));
 
105
         $this->assertEquals($tag, $converted);
 
106
    }
 
107
 
 
108
    public function testConvertAssertSelectElt()
 
109
    {
 
110
        $selector  = 'div';
 
111
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
112
        $tag       = array('tag' => 'div');
 
113
 
 
114
        $this->assertEquals($tag, $converted);
 
115
    }
 
116
 
 
117
    public function testConvertAssertClass()
 
118
    {
 
119
        $selector  = '.foo';
 
120
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
121
        $tag       = array('class' => 'foo');
 
122
 
 
123
        $this->assertEquals($tag, $converted);
 
124
    }
 
125
 
 
126
    public function testConvertAssertId()
 
127
    {
 
128
        $selector  = '#foo';
 
129
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
130
        $tag       = array('id' => 'foo');
 
131
 
 
132
        $this->assertEquals($tag, $converted);
 
133
    }
 
134
 
 
135
    public function testConvertAssertAttribute()
 
136
    {
 
137
        $selector  = '[foo="bar"]';
 
138
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
139
        $tag       = array('attributes' => array('foo' => 'bar'));
 
140
 
 
141
        $this->assertEquals($tag, $converted);
 
142
    }
 
143
 
 
144
    public function testConvertAssertAttributeSpaces()
 
145
    {
 
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);
 
152
    }
 
153
 
 
154
    public function testConvertAssertAttributeMultipleSpaces()
 
155
    {
 
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);
 
162
    }
 
163
 
 
164
    public function testConvertAssertSelectEltClass()
 
165
    {
 
166
        $selector  = 'div.foo';
 
167
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
168
        $tag       = array('tag' => 'div', 'class' => 'foo');
 
169
 
 
170
        $this->assertEquals($tag, $converted);
 
171
    }
 
172
 
 
173
    public function testConvertAssertSelectEltId()
 
174
    {
 
175
        $selector  = 'div#foo';
 
176
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
177
        $tag       = array('tag' => 'div', 'id' => 'foo');
 
178
 
 
179
        $this->assertEquals($tag, $converted);
 
180
    }
 
181
 
 
182
    public function testConvertAssertSelectEltAttrEqual()
 
183
    {
 
184
        $selector  = 'div[foo="bar"]';
 
185
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
186
        $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
 
187
 
 
188
        $this->assertEquals($tag, $converted);
 
189
    }
 
190
 
 
191
    public function testConvertAssertSelectEltMultiAttrEqual()
 
192
    {
 
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'));
 
196
 
 
197
        $this->assertEquals($tag, $converted);
 
198
    }
 
199
 
 
200
    public function testConvertAssertSelectEltAttrHasOne()
 
201
    {
 
202
        $selector  = 'div[foo~="bar"]';
 
203
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
204
        $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
 
205
 
 
206
        $this->assertEquals($tag, $converted);
 
207
    }
 
208
 
 
209
    public function testConvertAssertSelectEltAttrContains()
 
210
    {
 
211
        $selector  = 'div[foo*="bar"]';
 
212
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
213
        $tag       = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
 
214
 
 
215
        $this->assertEquals($tag, $converted);
 
216
    }
 
217
 
 
218
    public function testConvertAssertSelectEltChild()
 
219
    {
 
220
        $selector  = 'div > a';
 
221
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
222
        $tag       = array('tag' => 'div', 'child' => array('tag' => 'a'));
 
223
 
 
224
        $this->assertEquals($tag, $converted);
 
225
    }
 
226
 
 
227
    public function testConvertAssertSelectEltAdjacentSibling()
 
228
    {
 
229
        $selector  = 'div + a';
 
230
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
231
        $tag       = array('tag' => 'div', 'adjacent-sibling' => array('tag' => 'a'));
 
232
 
 
233
        $this->assertEquals($tag, $converted);
 
234
    }
 
235
 
 
236
    public function testConvertAssertSelectEltDescendant()
 
237
    {
 
238
        $selector  = 'div a';
 
239
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
 
240
        $tag       = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
 
241
 
 
242
        $this->assertEquals($tag, $converted);
 
243
    }
 
244
 
 
245
    public function testConvertAssertSelectContent()
 
246
    {
 
247
        $selector  = '#foo';
 
248
        $content   = 'div contents';
 
249
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
 
250
        $tag       = array('id' => 'foo', 'content' => 'div contents');
 
251
 
 
252
        $this->assertEquals($tag, $converted);
 
253
    }
 
254
 
 
255
    public function testConvertAssertSelectTrue()
 
256
    {
 
257
        $selector  = '#foo';
 
258
        $content   = true;
 
259
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
 
260
        $tag       = array('id' => 'foo');
 
261
 
 
262
        $this->assertEquals($tag, $converted);
 
263
    }
 
264
 
 
265
    public function testConvertAssertSelectFalse()
 
266
    {
 
267
        $selector  = '#foo';
 
268
        $content   = false;
 
269
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
 
270
        $tag       = array('id' => 'foo');
 
271
 
 
272
        $this->assertEquals($tag, $converted);
 
273
    }
 
274
 
 
275
    public function testConvertAssertNumber()
 
276
    {
 
277
        $selector  = '.foo';
 
278
        $content   = 3;
 
279
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
 
280
        $tag       = array('class' => 'foo');
 
281
 
 
282
        $this->assertEquals($tag, $converted);
 
283
    }
 
284
 
 
285
    public function testConvertAssertRange()
 
286
    {
 
287
        $selector  = '#foo';
 
288
        $content   = array('greater_than' => 5, 'less_than' => 10);
 
289
        $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
 
290
        $tag       = array('id' => 'foo');
 
291
 
 
292
        $this->assertEquals($tag, $converted);
 
293
    }
 
294
 
 
295
    /**
 
296
     * @dataProvider charProvider
 
297
     */
 
298
    public function testPrepareString($char)
 
299
    {
 
300
        $e = null;
 
301
 
 
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');
 
305
 
 
306
        try {
 
307
            $dom->loadXML($xml);
 
308
        } catch (Exception $e) {
 
309
        }
 
310
 
 
311
        $this->assertNull($e, sprintf(
 
312
            'PHPUnit_Util_XML::prepareString("\x%02x") should not crash DomDocument',
 
313
            ord($char)
 
314
        ));
 
315
    }
 
316
 
 
317
    public function charProvider()
 
318
    {
 
319
        $data = array();
 
320
 
 
321
        for ($i = 0; $i < 256; $i++) {
 
322
            $data[] = array(chr($i));
 
323
        }
 
324
 
 
325
        return $data;
 
326
    }
 
327
}