Issues (323)

tests/02ValueTest.php (9 issues)

1
<?php
2
3
include_once __DIR__ . '/LoggerAwareTestCase.php';
4
5
use PhpXmlRpc\Encoder;
6
use PhpXmlRpc\Helper\XMLParser;
0 ignored issues
show
This use statement conflicts with another class in this namespace, XMLParser. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use PhpXmlRpc\Request;
8
use PhpXmlRpc\Value;
9
10
/**
11
 * Tests involving the Value class.
12
 * NB: these tests do not involve the parsing of xml into Value objects - look in 04ParsingTest for that
13
 */
14
class ValueTest extends PhpXmlRpc_LoggerAwareTestCase
15
{
16
    public function testMinusOneString()
17
    {
18
        $v = new xmlrpcval('-1');
19
        $u = new xmlrpcval('-1', 'string');
20
        $t = new xmlrpcval(-1, 'string');
21
        $this->assertEquals($v->scalarval(), $u->scalarval());
22
        $this->assertEquals($v->scalarval(), $t->scalarval());
23
    }
24
25
    /**
26
     * This looks funny, and we might call it a bug. But we strive for 100 backwards compat...
27
     */
28
    public function testMinusOneInt()
29
    {
30
        $u = new xmlrpcval();
31
        $v = new xmlrpcval(-1);
32
        $this->assertEquals($u->scalarval(), $v->scalarval());
33
    }
34
35
    public function testAddScalarToStruct()
36
    {
37
        $v = new xmlrpcval(array('a' => 'b'), 'struct');
38
        $r = $v->addscalar('c');
39
        $this->assertEquals(0, $r);
40
    }
41
42
    public function testAddStructToStruct()
43
    {
44
        $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
45
        $r = $v->addstruct(array('b' => new xmlrpcval('c')));
46
        $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

46
        $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...
47
        $this->assertEquals(1, $r);
48
        $r = $v->addstruct(array('b' => new xmlrpcval('b')));
0 ignored issues
show
The assignment to $r is dead and can be removed.
Loading history...
49
        $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

49
        $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...
50
    }
51
52
    public function testAddArrayToArray()
53
    {
54
        $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
55
        $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
56
        $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

56
        $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...
57
        $this->assertEquals(1, $r);
58
    }
59
60
    /// @todo does this test check something useful at all?
61
    public function testUTF8IntString()
62
    {
63
        $v = new xmlrpcval(100, 'int');
64
        $s = $v->serialize('UTF-8');
65
        $this->assertequals("<value><int>100</int></value>\n", $s);
66
    }
67
68
    public function testUTF8String()
69
    {
70
        $sendstring = 'κόσμε'; // Greek word 'kosme'
71
        $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
72
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\PhpXmlRpc::importGlobals() has been deprecated. ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-deprecated */ \PhpXmlRpc\PhpXmlRpc::importGlobals();
Loading history...
73
        $f = new xmlrpcval($sendstring, 'string');
74
        $v = $f->serialize();
75
        $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
76
        $v = $f->serialize('UTF-8');
77
        $this->assertEquals("<value><string>$sendstring</string></value>\n", $v);
78
        $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
79
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\PhpXmlRpc::importGlobals() has been deprecated. ( Ignorable by Annotation )

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

79
        /** @scrutinizer ignore-deprecated */ \PhpXmlRpc\PhpXmlRpc::importGlobals();
Loading history...
80
    }
81
82
    public function testStringInt()
83
    {
84
        $v = new xmlrpcval('hello world', 'int');
85
        $s = $v->serialize();
86
        $this->assertequals("<value><int>0</int></value>\n", $s);
87
    }
88
89
    public function testDate()
90
    {
91
        $tz = date_default_timezone_get();
92
        date_default_timezone_set('UTC');
93
94
        $ts = 86401;
95
        $dt = new DateTime('@86401');
96
97
        $v = new xmlrpcval(86401, 'dateTime.iso8601');
98
        $s = $v->serialize();
99
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
100
101
        $v = new xmlrpcval($dt, 'dateTime.iso8601');
102
        $s = $v->serialize();
103
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
104
105
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($ts), 'dateTime.iso8601');
106
        $s = $v->serialize();
107
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
108
109
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($dt), 'dateTime.iso8601');
110
        $s = $v->serialize();
111
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
112
113
        date_default_timezone_set($tz);
114
    }
115
116
    /// @todo is this included in the above?
117
    public function testDateTime()
118
    {
119
        $time = time();
120
        $t1 = new xmlrpcval($time, 'dateTime.iso8601');
121
        $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
122
        $this->assertEquals($t1->serialize(), $t2->serialize());
123
        $datetime = new DateTime();
124
        $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
125
        $this->assertEquals($t1->serialize(), $t3->serialize());
126
    }
127
128
    public function testStructMemExists()
129
    {
130
        $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct');
131
        $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

131
        $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...
132
        $this->assertequals(true, $b);
133
        $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

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