Passed
Push — master ( bb780a...ebcf65 )
by Gaetano
08:40
created

ValueTests   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 123
dl 0
loc 219
rs 10
c 0
b 0
f 0
wmc 27

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testUTF8String() 0 12 1
A testAddScalarToStruct() 0 6 1
A testMinusOneString() 0 7 1
A testAddStructToStruct() 0 8 1
A testAddArrayToArray() 0 6 1
A testArrayAccess() 0 27 4
A testLatin15InternalEncoding() 0 18 2
A set_up() 0 5 2
A tear_down() 0 9 4
A testStringInt() 0 5 1
A testLocale() 0 16 3
A testMinusOneInt() 0 5 1
A testUTF8IntString() 0 5 1
A testStructMemExists() 0 7 1
A testDate() 0 25 1
A testBigXML() 0 16 2
1
<?php
2
3
include_once __DIR__ . '/../lib/xmlrpc.inc';
4
include_once __DIR__ . '/../lib/xmlrpcs.inc';
5
6
include_once __DIR__ . '/parse_args.php';
7
8
include_once __DIR__ . '/PolyfillTestCase.php';
9
10
use PHPUnit\Runner\BaseTestRunner;
11
12
/**
13
 * Tests involving the Value class.
14
 * NB: these tests do not involve the parsing of xml into Value objects - look in 03ParsingTest for that
15
 */
16
class ValueTests extends PhpXmlRpc_PolyfillTestCase
17
{
18
    public $args = array();
19
20
    protected function set_up()
21
    {
22
        $this->args = argParser::getArgs();
23
        if ($this->args['DEBUG'] == 1)
24
            ob_start();
25
    }
26
27
    protected function tear_down()
28
    {
29
        if ($this->args['DEBUG'] != 1)
30
            return;
31
        $out = ob_get_clean();
32
        $status = $this->getStatus();
33
        if ($status == BaseTestRunner::STATUS_ERROR
34
            || $status == BaseTestRunner::STATUS_FAILURE) {
35
            echo $out;
36
        }
37
    }
38
39
    public function testMinusOneString()
40
    {
41
        $v = new xmlrpcval('-1');
42
        $u = new xmlrpcval('-1', 'string');
43
        $t = new xmlrpcval(-1, 'string');
44
        $this->assertEquals($v->scalarval(), $u->scalarval());
45
        $this->assertEquals($v->scalarval(), $t->scalarval());
46
    }
47
48
    /**
49
     * This looks funny, and we might call it a bug. But we strive for 100 backwards compat...
50
     */
51
    public function testMinusOneInt()
52
    {
53
        $u = new xmlrpcval();
54
        $v = new xmlrpcval(-1);
55
        $this->assertEquals($u->scalarval(), $v->scalarval());
56
    }
57
58
    public function testAddScalarToStruct()
59
    {
60
        $v = new xmlrpcval(array('a' => 'b'), 'struct');
61
        // use @ operator in case error_log gets on screen
62
        $r = @$v->addscalar('c');
63
        $this->assertEquals(0, $r);
64
    }
65
66
    public function testAddStructToStruct()
67
    {
68
        $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
69
        $r = $v->addstruct(array('b' => new xmlrpcval('c')));
70
        $this->assertEquals(2, $v->structsize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structsize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
        $this->assertEquals(2, /** @scrutinizer ignore-deprecated */ $v->structsize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
        $this->assertEquals(1, $r);
72
        $r = $v->addstruct(array('b' => new xmlrpcval('b')));
0 ignored issues
show
Unused Code introduced by
The assignment to $r is dead and can be removed.
Loading history...
73
        $this->assertEquals(2, $v->structsize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structsize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

73
        $this->assertEquals(2, /** @scrutinizer ignore-deprecated */ $v->structsize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
74
    }
75
76
    public function testAddArrayToArray()
77
    {
78
        $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
79
        $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
80
        $this->assertEquals(4, $v->arraysize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::arraysize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
        $this->assertEquals(4, /** @scrutinizer ignore-deprecated */ $v->arraysize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
81
        $this->assertEquals(1, $r);
82
    }
83
84
    /// @todo does this test check something useful at all?
85
    public function testUTF8IntString()
86
    {
87
        $v = new xmlrpcval(100, 'int');
88
        $s = $v->serialize('UTF-8');
89
        $this->assertequals("<value><int>100</int></value>\n", $s);
90
    }
91
92
    public function testUTF8String()
93
    {
94
        $sendstring = 'κόσμε'; // Greek word 'kosme'
95
        $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
96
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
97
        $f = new xmlrpcval($sendstring, 'string');
98
        $v = $f->serialize();
99
        $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
100
        $v = $f->serialize('UTF-8');
101
        $this->assertEquals("<value><string>$sendstring</string></value>\n", $v);
102
        $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
103
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
104
    }
105
106
    public function testStringInt()
107
    {
108
        $v = new xmlrpcval('hello world', 'int');
109
        $s = $v->serialize();
110
        $this->assertequals("<value><int>0</int></value>\n", $s);
111
    }
112
113
    public function testDate()
114
    {
115
        $tz = date_default_timezone_get();
116
        date_default_timezone_set('UTC');
117
118
        $ts = 86401;
119
        $dt = new DateTime('@86401');
120
121
        $v = new xmlrpcval(86401, 'dateTime.iso8601');
122
        $s = $v->serialize();
123
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
124
125
        $v = new xmlrpcval($dt, 'dateTime.iso8601');
126
        $s = $v->serialize();
127
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
128
129
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($ts), 'dateTime.iso8601');
130
        $s = $v->serialize();
131
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
132
133
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($dt), 'dateTime.iso8601');
134
        $s = $v->serialize();
135
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
136
137
        date_default_timezone_set($tz);
138
    }
139
140
    public function testStructMemExists()
141
    {
142
        $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct');
143
        $b = $v->structmemexists('hello');
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structmemexists() has been deprecated: use array access, e.g. isset($val[$key]) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

143
        $b = /** @scrutinizer ignore-deprecated */ $v->structmemexists('hello');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
144
        $this->assertequals(true, $b);
145
        $b = $v->structmemexists('world');
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structmemexists() has been deprecated: use array access, e.g. isset($val[$key]) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

145
        $b = /** @scrutinizer ignore-deprecated */ $v->structmemexists('world');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
146
        $this->assertequals(false, $b);
147
    }
148
149
    public function testLocale()
150
    {
151
        $locale = setlocale(LC_NUMERIC, 0);
152
        /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
153
        if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) {
154
            $v = new xmlrpcval(1.1, 'double');
155
            if (strpos($v->scalarval(), ',') == 1) {
156
                $r = $v->serialize();
157
                $this->assertequals(false, strpos($r, ','));
158
                setlocale(LC_NUMERIC, $locale);
159
            } else {
160
                setlocale(LC_NUMERIC, $locale);
161
                $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
162
            }
163
        } else {
164
            $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
165
        }
166
    }
167
168
    public function testArrayAccess()
169
    {
170
        $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array');
171
        $this->assertequals(1, count($v1));
172
        $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
173
174
        foreach($v1 as $key => $val)
175
        {
176
            $this->assertArrayHasKey($key, $out);
177
            $expected = $out[$key];
178
            if (gettype($expected) == 'array') {
179
                $this->assertequals('array', gettype($val));
180
            } else {
181
                $this->assertequals($expected, $val);
182
            }
183
        }
184
185
        $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array');
186
        $this->assertequals(2, count($v2));
187
        $out = array(array('key' => 0, 'value'  => 'object'), array('key' => 1, 'value'  => 'object'));
188
        $i = 0;
189
        foreach($v2 as $key => $val)
190
        {
191
            $expected = $out[$i];
192
            $this->assertequals($expected['key'], $key);
193
            $this->assertequals($expected['value'], gettype($val));
194
            $i++;
195
        }
196
    }
197
198
    /// @todo do not use \PhpXmlRpc\Encoder for this test
199
    public function testBigXML()
200
    {
201
        // nb: make sure that  the serialized xml corresponding to this is > 10MB in size
202
        $data = array();
203
        for ($i = 0; $i < 500000; $i++ ) {
204
            $data[] = 'hello world';
205
        }
206
207
        $encoder = new \PhpXmlRpc\Encoder();
208
        $val = $encoder->encode($data);
209
        $req = new \PhpXmlRpc\Request('test', array($val));
210
        $xml = $req->serialize();
211
        $parser = new \PhpXmlRpc\Helper\XMLParser();
212
        $parser->parse($xml);
213
214
        $this->assertequals(0, $parser->_xh['isf']);
215
    }
216
217
    public function testLatin15InternalEncoding()
218
    {
219
        if (!function_exists('mb_convert_encoding')) {
220
            $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
221
            return;
222
        }
223
224
        $string = chr(164);
225
        $v = new \PhpXmlRpc\Value($string);
226
227
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
228
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-15';
229
230
        $this->assertEquals("<value><string>&#8364;</string></value>", trim($v->serialize('US-ASCII')));
231
        $this->assertEquals("<value><string>$string</string></value>", trim($v->serialize('ISO-8859-15')));
232
        $this->assertEquals("<value><string>€</string></value>", trim($v->serialize('UTF-8')));
233
234
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding;
235
    }
236
}
237