1 | <?php |
||
2 | |||
3 | include_once __DIR__ . '/LoggerAwareTestCase.php'; |
||
4 | |||
5 | /** |
||
6 | * Tests involving the Value class. |
||
7 | * NB: these tests do not involve the parsing of xml into Value objects - look in 04ParsingTest for that |
||
8 | */ |
||
9 | class ValueTest extends PhpXmlRpc_LoggerAwareTestCase |
||
10 | { |
||
11 | public function testMinusOneString() |
||
12 | { |
||
13 | $v = new xmlrpcval('-1'); |
||
14 | $u = new xmlrpcval('-1', 'string'); |
||
15 | $t = new xmlrpcval(-1, 'string'); |
||
16 | $this->assertEquals($v->scalarval(), $u->scalarval()); |
||
17 | $this->assertEquals($v->scalarval(), $t->scalarval()); |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * This looks funny, and we might call it a bug. But we strive for 100 backwards compat... |
||
22 | */ |
||
23 | public function testMinusOneInt() |
||
24 | { |
||
25 | $u = new xmlrpcval(); |
||
26 | $v = new xmlrpcval(-1); |
||
27 | $this->assertEquals($u->scalarval(), $v->scalarval()); |
||
28 | } |
||
29 | |||
30 | public function testAddScalarToStruct() |
||
31 | { |
||
32 | $v = new xmlrpcval(array('a' => 'b'), 'struct'); |
||
33 | $r = $v->addscalar('c'); |
||
34 | $this->assertEquals(0, $r); |
||
35 | } |
||
36 | |||
37 | public function testAddStructToStruct() |
||
38 | { |
||
39 | $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct'); |
||
40 | $r = $v->addstruct(array('b' => new xmlrpcval('c'))); |
||
41 | $this->assertEquals(2, $v->structsize()); |
||
42 | $this->assertEquals(1, $r); |
||
43 | $r = $v->addstruct(array('b' => new xmlrpcval('b'))); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
44 | $this->assertEquals(2, $v->structsize()); |
||
45 | } |
||
46 | |||
47 | public function testAddArrayToArray() |
||
48 | { |
||
49 | $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array'); |
||
50 | $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c'))); |
||
51 | $this->assertEquals(4, $v->arraysize()); |
||
52 | $this->assertEquals(1, $r); |
||
53 | } |
||
54 | |||
55 | /// @todo does this test check something useful at all? |
||
56 | public function testUTF8IntString() |
||
57 | { |
||
58 | $v = new xmlrpcval(100, 'int'); |
||
59 | $s = $v->serialize('UTF-8'); |
||
60 | $this->assertequals("<value><int>100</int></value>\n", $s); |
||
61 | } |
||
62 | |||
63 | public function testUTF8String() |
||
64 | { |
||
65 | $sendstring = 'κόσμε'; // Greek word 'kosme' |
||
66 | $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8'; |
||
67 | \PhpXmlRpc\PhpXmlRpc::importGlobals(); |
||
68 | $f = new xmlrpcval($sendstring, 'string'); |
||
69 | $v = $f->serialize(); |
||
70 | $this->assertEquals("<value><string>κόσμε</string></value>\n", $v); |
||
71 | $v = $f->serialize('UTF-8'); |
||
72 | $this->assertEquals("<value><string>$sendstring</string></value>\n", $v); |
||
73 | $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1'; |
||
74 | \PhpXmlRpc\PhpXmlRpc::importGlobals(); |
||
75 | } |
||
76 | |||
77 | public function testStringInt() |
||
78 | { |
||
79 | $v = new xmlrpcval('hello world', 'int'); |
||
80 | $s = $v->serialize(); |
||
81 | $this->assertequals("<value><int>0</int></value>\n", $s); |
||
82 | } |
||
83 | |||
84 | public function testDate() |
||
85 | { |
||
86 | $tz = date_default_timezone_get(); |
||
87 | date_default_timezone_set('UTC'); |
||
88 | |||
89 | $ts = 86401; |
||
90 | $dt = new DateTime('@86401'); |
||
91 | |||
92 | $v = new xmlrpcval(86401, 'dateTime.iso8601'); |
||
93 | $s = $v->serialize(); |
||
94 | $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s); |
||
95 | |||
96 | $v = new xmlrpcval($dt, 'dateTime.iso8601'); |
||
97 | $s = $v->serialize(); |
||
98 | $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s); |
||
99 | |||
100 | $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($ts), 'dateTime.iso8601'); |
||
101 | $s = $v->serialize(); |
||
102 | $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s); |
||
103 | |||
104 | $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($dt), 'dateTime.iso8601'); |
||
105 | $s = $v->serialize(); |
||
106 | $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s); |
||
107 | |||
108 | date_default_timezone_set($tz); |
||
109 | } |
||
110 | |||
111 | /// @todo is this included in the above? |
||
112 | public function testDateTime() |
||
113 | { |
||
114 | $time = time(); |
||
115 | $t1 = new xmlrpcval($time, 'dateTime.iso8601'); |
||
116 | $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601'); |
||
117 | $this->assertEquals($t1->serialize(), $t2->serialize()); |
||
118 | $datetime = new DateTime(); |
||
119 | $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601'); |
||
120 | $this->assertEquals($t1->serialize(), $t3->serialize()); |
||
121 | } |
||
122 | |||
123 | public function testStructMemExists() |
||
124 | { |
||
125 | $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct'); |
||
126 | $b = $v->structmemexists('hello'); |
||
127 | $this->assertequals(true, $b); |
||
128 | $b = $v->structmemexists('world'); |
||
129 | $this->assertequals(false, $b); |
||
130 | } |
||
131 | |||
132 | public function testLocale() |
||
133 | { |
||
134 | $locale = setlocale(LC_NUMERIC, 0); |
||
135 | /// @todo on php 5.3/win, possibly later versions, setting locale to german does not seem to set decimal separator to comma... |
||
136 | if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) { |
||
137 | $v = new xmlrpcval(1.1, 'double'); |
||
138 | if (version_compare(PHP_VERSION, '8.0', '>=')) { |
||
139 | $str = sprintf("%f", $v->scalarval()); |
||
140 | } else { |
||
141 | $str = (string)$v->scalarval(); |
||
142 | } |
||
143 | if (strpos($str, ',') == 1) { |
||
144 | $r = $v->serialize(); |
||
145 | $this->assertequals(false, strpos($r, ',')); |
||
146 | setlocale(LC_NUMERIC, $locale); |
||
147 | } else { |
||
148 | setlocale(LC_NUMERIC, $locale); |
||
149 | $this->markTestSkipped('Did not find a locale which sets decimal separator to comma'); |
||
150 | } |
||
151 | } else { |
||
152 | $this->markTestSkipped('Did not find a locale which sets decimal separator to comma'); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public function testArrayAccess() |
||
157 | { |
||
158 | $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array'); |
||
159 | $this->assertequals(1, count($v1)); |
||
160 | $out = array('me' => array(), 'mytype' => 2, '_php_class' => null); |
||
161 | |||
162 | foreach($v1 as $key => $val) |
||
163 | { |
||
164 | $this->assertArrayHasKey($key, $out); |
||
165 | $expected = $out[$key]; |
||
166 | if (gettype($expected) == 'array') { |
||
167 | $this->assertequals('array', gettype($val)); |
||
168 | } else { |
||
169 | $this->assertequals($expected, $val); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array'); |
||
174 | $this->assertequals(2, count($v2)); |
||
175 | $out = array(array('key' => 0, 'value' => 'object'), array('key' => 1, 'value' => 'object')); |
||
176 | $i = 0; |
||
177 | foreach($v2 as $key => $val) |
||
178 | { |
||
179 | $expected = $out[$i]; |
||
180 | $this->assertequals($expected['key'], $key); |
||
181 | $this->assertequals($expected['value'], gettype($val)); |
||
182 | $i++; |
||
183 | } |
||
184 | |||
185 | $v3 = new \PhpXmlRpc\Value(10, 'i4'); |
||
186 | $this->assertEquals(1, count($v3)); |
||
187 | $this->assertEquals(true, isset($v3['int'])); |
||
188 | $this->assertEquals(true, isset($v3['i4'])); |
||
189 | $this->assertEquals(10, $v3['int']); |
||
190 | $this->assertEquals(10, $v3['i4']); |
||
191 | $v3['int'] = 100; |
||
192 | $this->assertEquals(100, $v3['int']); |
||
193 | $this->assertEquals(100, $v3['i4']); |
||
194 | $v3['i4'] = 1000; |
||
195 | $this->assertEquals(1000, $v3['int']); |
||
196 | $this->assertEquals(1000, $v3['i4']); |
||
197 | } |
||
198 | |||
199 | /// @todo do not use \PhpXmlRpc\Encoder for this test |
||
200 | public function testBigXML() |
||
201 | { |
||
202 | // nb: make sure that the serialized xml corresponding to this is > 10MB in size |
||
203 | $data = array(); |
||
204 | for ($i = 0; $i < 500000; $i++ ) { |
||
205 | $data[] = 'hello world'; |
||
206 | } |
||
207 | |||
208 | $encoder = new \PhpXmlRpc\Encoder(); |
||
209 | $val = $encoder->encode($data); |
||
210 | $req = new \PhpXmlRpc\Request('test', array($val)); |
||
211 | $xml = $req->serialize(); |
||
212 | $parser = new \PhpXmlRpc\Helper\XMLParser(); |
||
213 | $_xh = $parser->parse($xml); |
||
214 | |||
215 | $this->assertequals(0, $_xh['isf']); |
||
216 | } |
||
217 | |||
218 | public function testLatin15InternalEncoding() |
||
219 | { |
||
220 | if (!function_exists('mb_convert_encoding')) { |
||
221 | $this->markTestSkipped('Miss mbstring extension to test exotic charsets'); |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | $string = chr(164); |
||
226 | $v = new \PhpXmlRpc\Value($string); |
||
227 | |||
228 | $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding; |
||
229 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-15'; |
||
230 | |||
231 | $this->assertEquals("<value><string>€</string></value>", trim($v->serialize('US-ASCII'))); |
||
232 | $this->assertEquals("<value><string>$string</string></value>", trim($v->serialize('ISO-8859-15'))); |
||
233 | $this->assertEquals("<value><string>€</string></value>", trim($v->serialize('UTF-8'))); |
||
234 | |||
235 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding; |
||
236 | } |
||
237 | } |
||
238 |