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
11class Util_XMLTest extends PHPUnit_Framework_TestCase
12{
13    /**
14     * @dataProvider charProvider
15     */
16    public function testPrepareString($char)
17    {
18        $e = null;
19
20        $escapedString = PHPUnit_Util_XML::prepareString($char);
21        $xml           = "<?xml version='1.0' encoding='UTF-8' ?><tag>$escapedString</tag>";
22        $dom           = new DomDocument('1.0', 'UTF-8');
23
24        try {
25            $dom->loadXML($xml);
26        } catch (Exception $e) {
27        }
28
29        $this->assertNull($e, sprintf(
30            'PHPUnit_Util_XML::prepareString("\x%02x") should not crash DomDocument',
31            ord($char)
32        ));
33    }
34
35    public function charProvider()
36    {
37        $data = [];
38
39        for ($i = 0; $i < 256; $i++) {
40            $data[] = [chr($i)];
41        }
42
43        return $data;
44    }
45
46    /**
47     * @expectedException PHPUnit_Framework_Exception
48     * @expectedExceptionMessage Could not load XML from empty string
49     */
50    public function testLoadEmptyString()
51    {
52        PHPUnit_Util_XML::load('');
53    }
54
55    /**
56     * @expectedException PHPUnit_Framework_Exception
57     * @expectedExceptionMessage Could not load XML from array
58     */
59    public function testLoadArray()
60    {
61        PHPUnit_Util_XML::load([1, 2, 3]);
62    }
63
64    /**
65     * @expectedException PHPUnit_Framework_Exception
66     * @expectedExceptionMessage Could not load XML from boolean
67     */
68    public function testLoadBoolean()
69    {
70        PHPUnit_Util_XML::load(false);
71    }
72
73    public function testNestedXmlToVariable()
74    {
75        $xml = '<array><element key="a"><array><element key="b"><string>foo</string></element></array></element><element key="c"><string>bar</string></element></array>';
76        $dom = new DOMDocument();
77        $dom->loadXML($xml);
78
79        $expected = [
80            'a' => [
81                'b' => 'foo',
82            ],
83            'c' => 'bar',
84        ];
85
86        $actual = PHPUnit_Util_XML::xmlToVariable($dom->documentElement);
87
88        $this->assertSame($expected, $actual);
89    }
90}
91