Total Complexity | 122 |
Total Lines | 1030 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
Complex classes like ServerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ServerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ServerTest extends PhpXmlRpc_PolyfillTestCase |
||
20 | { |
||
21 | /** @var xmlrpc_client $client */ |
||
22 | protected $client = null; |
||
23 | protected $method = 'http'; |
||
24 | protected $timeout = 10; |
||
25 | protected $request_compression = null; |
||
26 | protected $accepted_compression = ''; |
||
27 | protected $args = array(); |
||
28 | |||
29 | protected static $failed_tests = array(); |
||
30 | |||
31 | protected $testId; |
||
32 | /** @var boolean $collectCodeCoverageInformation */ |
||
33 | protected $collectCodeCoverageInformation; |
||
34 | protected $coverageScriptUrl; |
||
35 | |||
36 | /** |
||
37 | * @todo instead of overriding fail via _fail, implement Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation |
||
38 | */ |
||
39 | public static function _fail($message = '') |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Reimplemented to allow us to collect code coverage info from the target server. |
||
58 | * Code taken from PHPUnit_Extensions_Selenium2TestCase |
||
59 | * |
||
60 | * @param TestResult $result |
||
61 | * @return TestResult |
||
62 | * @throws Exception |
||
63 | * |
||
64 | * @todo instead of overriding run via _run, try to achieve this by implementing Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation |
||
65 | */ |
||
66 | public function _run($result = NULL) |
||
67 | { |
||
68 | $this->testId = get_class($this) . '__' . $this->getName(); |
||
69 | |||
70 | if ($result === NULL) { |
||
71 | $result = $this->createResult(); |
||
72 | } |
||
73 | |||
74 | $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation(); |
||
75 | |||
76 | parent::_run($result); |
||
77 | |||
78 | if ($this->collectCodeCoverageInformation) { |
||
79 | $coverage = new RemoteCoverage( |
||
80 | $this->coverageScriptUrl, |
||
81 | $this->testId |
||
82 | ); |
||
83 | $result->getCodeCoverage()->append( |
||
84 | $coverage->get(), $this |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | // do not call this before to give the time to the Listeners to run |
||
89 | //$this->getStrategy()->endOfTest($this->session); |
||
90 | |||
91 | return $result; |
||
92 | } |
||
93 | |||
94 | public function set_up() |
||
95 | { |
||
96 | $this->args = argParser::getArgs(); |
||
97 | |||
98 | $server = explode(':', $this->args['HTTPSERVER']); |
||
99 | if (count($server) > 1) { |
||
100 | $this->client = new xmlrpc_client($this->args['HTTPURI'], $server[0], $server[1]); |
||
101 | } else { |
||
102 | $this->client = new xmlrpc_client($this->args['HTTPURI'], $this->args['HTTPSERVER']); |
||
103 | } |
||
104 | |||
105 | $this->client->setDebug($this->args['DEBUG']); |
||
106 | $this->client->request_compression = $this->request_compression; |
||
107 | $this->client->accepted_compression = $this->accepted_compression; |
||
108 | |||
109 | $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . preg_replace('|/tests/index\.php(\?.*)?|', '/tests/phpunit_coverage.php', $this->args['HTTPURI']); |
||
110 | |||
111 | if ($this->args['DEBUG'] == 1) |
||
112 | ob_start(); |
||
113 | } |
||
114 | |||
115 | protected function tear_down() |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param PhpXmlRpc\Request|array $msg |
||
129 | * @param int|array $errorCode expected error codes |
||
130 | * @param bool $returnResponse |
||
131 | * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|null |
||
132 | */ |
||
133 | protected function send($msg, $errorCode = 0, $returnResponse = false) |
||
134 | { |
||
135 | if ($this->collectCodeCoverageInformation) { |
||
136 | $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId); |
||
137 | } |
||
138 | |||
139 | $r = $this->client->send($msg, $this->timeout, $this->method); |
||
140 | // for multicall, return directly array of responses |
||
141 | if (is_array($r)) { |
||
142 | return $r; |
||
143 | } |
||
144 | $this->validateResponse($r); |
||
145 | if (is_array($errorCode)) { |
||
146 | $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString()); |
||
147 | } else { |
||
148 | $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString()); |
||
149 | } |
||
150 | if (!$r->faultCode()) { |
||
151 | if ($returnResponse) { |
||
152 | return $r; |
||
153 | } else { |
||
154 | return $r->value(); |
||
155 | } |
||
156 | } else { |
||
157 | return null; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | protected function validateResponse($r) |
||
163 | // to be implemented in subclasses |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Adds (and replaces) query params to the url currently used by the client |
||
168 | * @param array $data |
||
169 | */ |
||
170 | protected function addQueryParams($data) |
||
176 | } |
||
177 | |||
178 | public function testString() |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | public function testLatin1String() |
||
216 | } |
||
217 | } |
||
218 | |||
219 | public function testExoticCharsetsRequests() |
||
249 | } |
||
250 | |||
251 | public function testExoticCharsetsRequests2() |
||
276 | } |
||
277 | |||
278 | public function testExoticCharsetsRequests3() |
||
279 | { |
||
280 | // note that we should disable this call also when mbstring is missing server-side |
||
281 | if (!function_exists('mb_convert_encoding')) { |
||
282 | $this->markTestSkipped('Miss mbstring extension to test exotic charsets'); |
||
283 | return; |
||
284 | } |
||
285 | // the warning suppression is due to utf8_decode being deprecated in php 8.2 |
||
286 | $sendString = @utf8_decode('élève'); |
||
287 | $str = '<?xml version="1.0"?> |
||
288 | <methodCall> |
||
289 | <methodName>examples.stringecho</methodName> |
||
290 | <params> |
||
291 | <param> |
||
292 | <value><string>'.$sendString.'</string></value> |
||
293 | </param> |
||
294 | </params> |
||
295 | </methodCall>'; |
||
296 | |||
297 | // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding |
||
298 | // (used on the server side) sort it out |
||
299 | $this->addQueryParams(array('DETECT_ENCODINGS' => array('ISO-8859-1', 'UTF-8'))); |
||
300 | $v = $this->send($str); |
||
301 | $this->assertEquals($sendString, $v->scalarval()); |
||
302 | } |
||
303 | |||
304 | /*public function testLatin1Method() |
||
305 | { |
||
306 | $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array( |
||
307 | new xmlrpcval('hello') |
||
308 | )); |
||
309 | $v = $this->send($f); |
||
310 | if ($v) { |
||
311 | $this->assertEquals('hello', $v->scalarval()); |
||
312 | } |
||
313 | }*/ |
||
314 | |||
315 | public function testUtf8Method() |
||
316 | { |
||
317 | PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8'; |
||
318 | $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array( |
||
319 | new xmlrpcval('hello') |
||
320 | )); |
||
321 | $v = $this->send($m); |
||
322 | PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1'; |
||
323 | if ($v) { |
||
324 | $this->assertEquals('hello', $v->scalarval()); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | public function testAddingDoubles() |
||
329 | { |
||
330 | // note that rounding errors mean we |
||
331 | // keep precision to sensible levels here ;-) |
||
332 | $a = 12.13; |
||
333 | $b = -23.98; |
||
334 | $m = new xmlrpcmsg('examples.addtwodouble', array( |
||
335 | new xmlrpcval($a, 'double'), |
||
336 | new xmlrpcval($b, 'double'), |
||
337 | )); |
||
338 | $v = $this->send($m); |
||
339 | if ($v) { |
||
340 | $this->assertEquals($a + $b, $v->scalarval()); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | public function testAdding() |
||
345 | { |
||
346 | $m = new xmlrpcmsg('examples.addtwo', array( |
||
347 | new xmlrpcval(12, 'int'), |
||
348 | new xmlrpcval(-23, 'int'), |
||
349 | )); |
||
350 | $v = $this->send($m); |
||
351 | if ($v) { |
||
352 | $this->assertEquals(12 - 23, $v->scalarval()); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | public function testInvalidNumber() |
||
357 | { |
||
358 | $m = new xmlrpcmsg('examples.addtwo', array( |
||
359 | new xmlrpcval('fred', 'int'), |
||
360 | new xmlrpcval("\"; exec('ls')", 'int'), |
||
361 | )); |
||
362 | $v = $this->send($m); |
||
363 | /// @todo a fault condition should be generated here |
||
364 | /// by the server, which we pick up on |
||
365 | if ($v) { |
||
366 | $this->assertEquals(0, $v->scalarval()); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | public function testBoolean() |
||
371 | { |
||
372 | $m = new xmlrpcmsg('examples.invertBooleans', array( |
||
373 | new xmlrpcval(array( |
||
374 | new xmlrpcval(true, 'boolean'), |
||
375 | new xmlrpcval(false, 'boolean'), |
||
376 | new xmlrpcval(1, 'boolean'), |
||
377 | new xmlrpcval(0, 'boolean') |
||
378 | ), |
||
379 | 'array' |
||
380 | ),)); |
||
381 | $answer = '0101'; |
||
382 | $v = $this->send($m); |
||
383 | if ($v) { |
||
384 | $sz = $v->arraysize(); |
||
385 | $got = ''; |
||
386 | for ($i = 0; $i < $sz; $i++) { |
||
387 | $b = $v->arraymem($i); |
||
388 | if ($b->scalarval()) { |
||
389 | $got .= '1'; |
||
390 | } else { |
||
391 | $got .= '0'; |
||
392 | } |
||
393 | } |
||
394 | $this->assertEquals($answer, $got); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | public function testBase64() |
||
399 | { |
||
400 | $sendString = 'Mary had a little lamb, |
||
401 | Whose fleece was white as snow, |
||
402 | And everywhere that Mary went |
||
403 | the lamb was sure to go. |
||
404 | |||
405 | Mary had a little lamb |
||
406 | She tied it to a pylon |
||
407 | Ten thousand volts went down its back |
||
408 | And turned it into nylon'; |
||
409 | $m = new xmlrpcmsg('examples.decode64', array( |
||
410 | new xmlrpcval($sendString, 'base64'), |
||
411 | )); |
||
412 | $v = $this->send($m); |
||
413 | if ($v) { |
||
414 | if (strlen($sendString) == strlen($v->scalarval())) { |
||
415 | $this->assertEquals($sendString, $v->scalarval()); |
||
416 | } else { |
||
417 | $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval()); |
||
418 | } |
||
419 | } |
||
420 | } |
||
421 | |||
422 | public function testDateTime() |
||
423 | { |
||
424 | $time = time(); |
||
425 | $t1 = new xmlrpcval($time, 'dateTime.iso8601'); |
||
426 | $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601'); |
||
427 | $this->assertEquals($t1->serialize(), $t2->serialize()); |
||
428 | if (class_exists('DateTime')) { |
||
429 | $datetime = new DateTime(); |
||
430 | // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info |
||
431 | if (is_callable(array($datetime, 'setTimestamp'))) { |
||
432 | $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601'); |
||
433 | $this->assertEquals($t1->serialize(), $t3->serialize()); |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | |||
438 | public function testCountEntities() |
||
439 | { |
||
440 | $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<"; |
||
441 | $m = new xmlrpcmsg('validator1.countTheEntities', array( |
||
442 | new xmlrpcval($sendString, 'string'), |
||
443 | )); |
||
444 | $v = $this->send($m); |
||
445 | if ($v) { |
||
446 | $got = ''; |
||
447 | $expected = '37210'; |
||
448 | $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes'); |
||
449 | foreach($expect_array as $val) { |
||
450 | $b = $v->structmem($val); |
||
451 | $got .= $b->me['int']; |
||
452 | } |
||
453 | $this->assertEquals($expected, $got); |
||
454 | } |
||
455 | } |
||
456 | |||
457 | public function _multicall_msg($method, $params) |
||
463 | } |
||
464 | |||
465 | public function testServerMulticall() |
||
466 | { |
||
467 | // We manually construct a system.multicall() call to ensure |
||
468 | // that the server supports it. |
||
469 | |||
470 | // NB: This test will NOT pass if server does not support system.multicall. |
||
471 | |||
472 | // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py |
||
473 | $good1 = $this->_multicall_msg( |
||
474 | 'system.methodHelp', |
||
475 | array(php_xmlrpc_encode('system.listMethods'))); |
||
476 | $bad = $this->_multicall_msg( |
||
477 | 'test.nosuch', |
||
478 | array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); |
||
479 | $recursive = $this->_multicall_msg( |
||
480 | 'system.multicall', |
||
481 | array(new xmlrpcval(array(), 'array'))); |
||
482 | $good2 = $this->_multicall_msg( |
||
483 | 'system.methodSignature', |
||
484 | array(php_xmlrpc_encode('system.listMethods'))); |
||
485 | $arg = new xmlrpcval( |
||
486 | array($good1, $bad, $recursive, $good2), |
||
487 | 'array' |
||
488 | ); |
||
489 | |||
490 | $m = new xmlrpcmsg('system.multicall', array($arg)); |
||
491 | $v = $this->send($m); |
||
492 | if ($v) { |
||
493 | //$this->assertEquals(0, $r->faultCode(), "fault from system.multicall"); |
||
494 | $this->assertEquals(4, $v->arraysize(), "bad number of return values"); |
||
495 | |||
496 | $r1 = $v->arraymem(0); |
||
497 | $this->assertTrue( |
||
498 | $r1->kindOf() == 'array' && $r1->arraysize() == 1, |
||
499 | "did not get array of size 1 from good1" |
||
500 | ); |
||
501 | |||
502 | $r2 = $v->arraymem(1); |
||
503 | $this->assertEquals('struct', $r2->kindOf(), "no fault from bad"); |
||
504 | |||
505 | $r3 = $v->arraymem(2); |
||
506 | $this->assertEquals('struct', $r3->kindOf(), "recursive system.multicall did not fail"); |
||
507 | |||
508 | $r4 = $v->arraymem(3); |
||
509 | $this->assertTrue( |
||
510 | $r4->kindOf() == 'array' && $r4->arraysize() == 1, |
||
511 | "did not get array of size 1 from good2" |
||
512 | ); |
||
513 | } |
||
514 | } |
||
515 | |||
516 | public function testClientMulticall1() |
||
517 | { |
||
518 | // NB: This test will NOT pass if server does not support system.multicall. |
||
519 | |||
520 | $noMultiCall = $this->client->no_multicall; |
||
521 | $this->client->no_multicall = false; |
||
522 | |||
523 | $good1 = new xmlrpcmsg('system.methodHelp', |
||
524 | array(php_xmlrpc_encode('system.listMethods'))); |
||
525 | $bad = new xmlrpcmsg('test.nosuch', |
||
526 | array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); |
||
527 | $recursive = new xmlrpcmsg('system.multicall', |
||
528 | array(new xmlrpcval(array(), 'array'))); |
||
529 | $good2 = new xmlrpcmsg('system.methodSignature', |
||
530 | array(php_xmlrpc_encode('system.listMethods')) |
||
531 | ); |
||
532 | |||
533 | $r = $this->send(array($good1, $bad, $recursive, $good2)); |
||
534 | if ($r) { |
||
535 | $this->assertEquals(4, count($r), "wrong number of return values"); |
||
536 | } |
||
537 | |||
538 | $this->assertEquals(0, $r[0]->faultCode(), "fault from good1"); |
||
539 | if (!$r[0]->faultCode()) { |
||
540 | $val = $r[0]->value(); |
||
541 | $this->assertTrue( |
||
542 | $val->kindOf() == 'scalar' && $val->scalartyp() == 'string', |
||
543 | "good1 did not return string" |
||
544 | ); |
||
545 | } |
||
546 | $this->assertNotEquals(0, $r[1]->faultCode(), "no fault from bad"); |
||
547 | $this->assertNotEquals(0, $r[2]->faultCode(), "no fault from recursive system.multicall"); |
||
548 | $this->assertEquals(0, $r[3]->faultCode(), "fault from good2"); |
||
549 | if (!$r[3]->faultCode()) { |
||
550 | $val = $r[3]->value(); |
||
551 | $this->assertEquals('array', $val->kindOf(), "good2 did not return array"); |
||
552 | } |
||
553 | // This is the only assert in this test which should fail |
||
554 | // if the test server does not support system.multicall. |
||
555 | $this->assertEquals(false, $this->client->no_multicall, "server does not support system.multicall"); |
||
556 | |||
557 | $this->client->no_multicall = $noMultiCall; |
||
558 | } |
||
559 | |||
560 | public function testClientMulticall2() |
||
561 | { |
||
562 | // NB: This test will NOT pass if server does not support system.multicall. |
||
563 | |||
564 | $noMultiCall = $this->client->no_multicall; |
||
565 | $this->client->no_multicall = true; |
||
566 | |||
567 | $good1 = new xmlrpcmsg('system.methodHelp', |
||
568 | array(php_xmlrpc_encode('system.listMethods'))); |
||
569 | $bad = new xmlrpcmsg('test.nosuch', |
||
570 | array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); |
||
571 | $recursive = new xmlrpcmsg('system.multicall', |
||
572 | array(new xmlrpcval(array(), 'array'))); |
||
573 | $good2 = new xmlrpcmsg('system.methodSignature', |
||
574 | array(php_xmlrpc_encode('system.listMethods')) |
||
575 | ); |
||
576 | |||
577 | $r = $this->send(array($good1, $bad, $recursive, $good2)); |
||
578 | if ($r) { |
||
579 | $this->assertEquals(4, count($r), "wrong number of return values"); |
||
580 | } |
||
581 | |||
582 | $this->assertEquals(0, $r[0]->faultCode(), "fault from good1"); |
||
583 | if (!$r[0]->faultCode()) { |
||
584 | $val = $r[0]->value(); |
||
585 | $this->assertTrue( |
||
586 | $val->kindOf() == 'scalar' && $val->scalartyp() == 'string', |
||
587 | "good1 did not return string"); |
||
588 | } |
||
589 | $this->assertNotEquals(0, $r[1]->faultCode(), "no fault from bad"); |
||
590 | $this->assertEquals(0, $r[2]->faultCode(), "fault from (non recursive) system.multicall"); |
||
591 | $this->assertEquals(0, $r[3]->faultCode(), "fault from good2"); |
||
592 | if (!$r[3]->faultCode()) { |
||
593 | $val = $r[3]->value(); |
||
594 | $this->assertEquals('array', $val->kindOf(), "good2 did not return array"); |
||
595 | } |
||
596 | |||
597 | $this->client->no_multicall = $noMultiCall; |
||
598 | } |
||
599 | |||
600 | public function testClientMulticall3() |
||
601 | { |
||
602 | // NB: This test will NOT pass if server does not support system.multicall. |
||
603 | |||
604 | $noMultiCall = $this->client->no_multicall; |
||
605 | $returnType = $this->client->return_type; |
||
606 | |||
607 | $this->client->return_type = 'phpvals'; |
||
608 | $this->client->no_multicall = false; |
||
609 | |||
610 | $good1 = new xmlrpcmsg('system.methodHelp', |
||
611 | array(php_xmlrpc_encode('system.listMethods'))); |
||
612 | $bad = new xmlrpcmsg('test.nosuch', |
||
613 | array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); |
||
614 | $recursive = new xmlrpcmsg('system.multicall', |
||
615 | array(new xmlrpcval(array(), 'array'))); |
||
616 | $good2 = new xmlrpcmsg('system.methodSignature', |
||
617 | array(php_xmlrpc_encode('system.listMethods')) |
||
618 | ); |
||
619 | |||
620 | $r = $this->send(array($good1, $bad, $recursive, $good2)); |
||
621 | if ($r) { |
||
622 | $this->assertEquals(4, count($r), "wrong number of return values"); |
||
623 | } |
||
624 | $this->assertEquals(0, $r[0]->faultCode(), "fault from good1"); |
||
625 | if (!$r[0]->faultCode()) { |
||
626 | $val = $r[0]->value(); |
||
627 | $this->assertIsString($val, "good1 did not return string"); |
||
628 | } |
||
629 | $this->assertNotEquals(0, $r[1]->faultCode(), "no fault from bad"); |
||
630 | $this->assertNotEquals(0, $r[2]->faultCode(), "no fault from recursive system.multicall"); |
||
631 | $this->assertEquals(0, $r[3]->faultCode(), "fault from good2"); |
||
632 | if (!$r[3]->faultCode()) { |
||
633 | $val = $r[3]->value(); |
||
634 | $this->assertIsArray($val, "good2 did not return array"); |
||
635 | } |
||
636 | |||
637 | $this->client->return_type = $returnType; |
||
638 | $this->client->no_multicall = $noMultiCall; |
||
639 | } |
||
640 | |||
641 | public function testClientMulticall4() |
||
642 | { |
||
643 | // NB: This test will NOT pass if server does not support system.multicall. |
||
644 | |||
645 | $noMultiCall = $this->client->no_multicall; |
||
646 | $returnType = $this->client->return_type; |
||
647 | |||
648 | $this->client->return_type = 'xml'; |
||
649 | $this->client->no_multicall = false; |
||
650 | |||
651 | $good1 = new xmlrpcmsg('system.methodHelp', |
||
652 | array(php_xmlrpc_encode('system.listMethods'))); |
||
653 | $good2 = new xmlrpcmsg('system.methodSignature', |
||
654 | array(php_xmlrpc_encode('system.listMethods')) |
||
655 | ); |
||
656 | |||
657 | $r = $this->send(array($good1, $good2)); |
||
658 | if ($r) { |
||
659 | $this->assertEquals(2, count($r), "wrong number of return values"); |
||
660 | } |
||
661 | $this->assertEquals(0, $r[0]->faultCode(), "fault from good1"); |
||
662 | $this->assertEquals(0, $r[1]->faultCode(), "fault from good2"); |
||
663 | |||
664 | $hr = $r[0]->httpResponse(); |
||
665 | $this->assertEquals(200, $hr['status_code'], "http response of multicall has no status code"); |
||
666 | $this->assertEquals($r[0]->httpResponse(), $r[1]->httpResponse(), "http response of multicall items differs"); |
||
667 | |||
668 | $this->client->return_type = $returnType; |
||
669 | $this->client->no_multicall = $noMultiCall; |
||
670 | } |
||
671 | |||
672 | public function testCatchWarnings() |
||
673 | { |
||
674 | $m = new xmlrpcmsg('tests.generatePHPWarning', array( |
||
675 | new xmlrpcval('whatever', 'string'), |
||
676 | )); |
||
677 | $v = $this->send($m); |
||
678 | if ($v) { |
||
679 | $this->assertEquals(true, $v->scalarval()); |
||
680 | } |
||
681 | } |
||
682 | |||
683 | public function testCatchExceptions() |
||
684 | { |
||
685 | $m = new xmlrpcmsg('tests.raiseException', array( |
||
686 | new xmlrpcval('whatever', 'string'), |
||
687 | )); |
||
688 | $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']); |
||
689 | $this->addQueryParams(array('EXCEPTION_HANDLING' => 1)); |
||
690 | $v = $this->send($m, 1); // the error code of the expected exception |
||
691 | $this->addQueryParams(array('EXCEPTION_HANDLING' => 2)); |
||
692 | // depending on whether display_errors is ON or OFF on the server, we will get back a different error here, |
||
693 | // as php will generate an http status code of either 200 or 500... |
||
694 | $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error'])); |
||
695 | } |
||
696 | |||
697 | public function testZeroParams() |
||
698 | { |
||
699 | $m = new xmlrpcmsg('system.listMethods'); |
||
700 | $v = $this->send($m); |
||
701 | } |
||
702 | |||
703 | public function testNullParams() |
||
725 | } |
||
726 | |||
727 | public function testCodeInjectionServerSide() |
||
728 | { |
||
729 | $m = new xmlrpcmsg('system.MethodHelp'); |
||
730 | $m->payload = "<?xml version=\"1.0\"?><methodCall><methodName>validator1.echoStructTest</methodName><params><param><value><struct><member><name>','')); echo('gotcha!'); die(); //</name></member></struct></value></param></params></methodCall>"; |
||
731 | $v = $this->send($m); |
||
732 | if ($v) { |
||
733 | $this->assertEquals(0, $v->structsize()); |
||
734 | } |
||
735 | } |
||
736 | |||
737 | public function testServerWrappedFunction() |
||
738 | { |
||
739 | $m = new xmlrpcmsg('tests.getStateName.2', array( |
||
740 | new xmlrpcval(23, 'int'), |
||
741 | )); |
||
742 | $v = $this->send($m); |
||
743 | $this->assertEquals('Michigan', $v->scalarval()); |
||
744 | |||
745 | // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response |
||
746 | $m = new xmlrpcmsg('tests.getStateName.2', array( |
||
747 | new xmlrpcval(0, 'int'), |
||
748 | )); |
||
749 | $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']); |
||
750 | |||
751 | // check if the generated function dispatch map is fine, by checking if the server registered it |
||
752 | $m = new xmlrpcmsg('system.methodSignature', array( |
||
753 | new xmlrpcval('tests.getStateName.2'), |
||
754 | )); |
||
755 | $v = $this->send($m); |
||
756 | $encoder = new \PhpXmlRpc\Encoder(); |
||
757 | $this->assertEquals(array(array('string', 'int')), $encoder->decode($v)); |
||
758 | } |
||
759 | |||
760 | public function testServerWrappedFunctionAsSource() |
||
761 | { |
||
762 | $m = new xmlrpcmsg('tests.getStateName.6', array( |
||
763 | new xmlrpcval(23, 'int'), |
||
764 | )); |
||
765 | $v = $this->send($m); |
||
766 | $this->assertEquals('Michigan', $v->scalarval()); |
||
767 | |||
768 | // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response |
||
769 | $m = new xmlrpcmsg('tests.getStateName.6', array( |
||
770 | new xmlrpcval(0, 'int'), |
||
771 | )); |
||
772 | $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']); |
||
773 | } |
||
774 | |||
775 | public function testServerWrappedObjectMethods() |
||
776 | { |
||
777 | $m = new xmlrpcmsg('tests.getStateName.3', array( |
||
778 | new xmlrpcval(23, 'int'), |
||
779 | )); |
||
780 | $v = $this->send($m); |
||
781 | $this->assertEquals('Michigan', $v->scalarval()); |
||
782 | |||
783 | $m = new xmlrpcmsg('tests.getStateName.4', array( |
||
784 | new xmlrpcval(23, 'int'), |
||
785 | )); |
||
786 | $v = $this->send($m); |
||
787 | $this->assertEquals('Michigan', $v->scalarval()); |
||
788 | |||
789 | $m = new xmlrpcmsg('tests.getStateName.5', array( |
||
790 | new xmlrpcval(23, 'int'), |
||
791 | )); |
||
792 | $v = $this->send($m); |
||
793 | $this->assertEquals('Michigan', $v->scalarval()); |
||
794 | |||
795 | $m = new xmlrpcmsg('tests.getStateName.7', array( |
||
796 | new xmlrpcval(23, 'int'), |
||
797 | )); |
||
798 | $v = $this->send($m); |
||
799 | $this->assertEquals('Michigan', $v->scalarval()); |
||
800 | |||
801 | $m = new xmlrpcmsg('tests.getStateName.8', array( |
||
802 | new xmlrpcval(23, 'int'), |
||
803 | )); |
||
804 | $v = $this->send($m); |
||
805 | $this->assertEquals('Michigan', $v->scalarval()); |
||
806 | |||
807 | $m = new xmlrpcmsg('tests.getStateName.9', array( |
||
808 | new xmlrpcval(23, 'int'), |
||
809 | )); |
||
810 | $v = $this->send($m); |
||
811 | $this->assertEquals('Michigan', $v->scalarval()); |
||
812 | } |
||
813 | |||
814 | public function testServerWrappedObjectMethodsAsSource() |
||
815 | { |
||
816 | $m = new xmlrpcmsg('tests.getStateName.7', array( |
||
817 | new xmlrpcval(23, 'int'), |
||
818 | )); |
||
819 | $v = $this->send($m); |
||
820 | $this->assertEquals('Michigan', $v->scalarval()); |
||
821 | |||
822 | $m = new xmlrpcmsg('tests.getStateName.8', array( |
||
823 | new xmlrpcval(23, 'int'), |
||
824 | )); |
||
825 | $v = $this->send($m); |
||
826 | $this->assertEquals('Michigan', $v->scalarval()); |
||
827 | |||
828 | $m = new xmlrpcmsg('tests.getStateName.9', array( |
||
829 | new xmlrpcval(23, 'int'), |
||
830 | )); |
||
831 | $v = $this->send($m); |
||
832 | $this->assertEquals('Michigan', $v->scalarval()); |
||
833 | } |
||
834 | |||
835 | public function testServerClosure() |
||
836 | { |
||
837 | $m = new xmlrpcmsg('tests.getStateName.10', array( |
||
838 | new xmlrpcval(23, 'int'), |
||
839 | )); |
||
840 | $v = $this->send($m); |
||
841 | $this->assertEquals('Michigan', $v->scalarval()); |
||
842 | } |
||
843 | |||
844 | public function testServerWrappedClosure() |
||
845 | { |
||
846 | $m = new xmlrpcmsg('tests.getStateName.11', array( |
||
847 | new xmlrpcval(23, 'int'), |
||
848 | )); |
||
849 | $v = $this->send($m); |
||
850 | $this->assertEquals('Michigan', $v->scalarval()); |
||
851 | } |
||
852 | |||
853 | public function testServerWrappedClass() |
||
854 | { |
||
855 | $m = new xmlrpcmsg('tests.handlersContainer.findState', array( |
||
856 | new xmlrpcval(23, 'int'), |
||
857 | )); |
||
858 | $v = $this->send($m); |
||
859 | $this->assertEquals('Michigan', $v->scalarval()); |
||
860 | } |
||
861 | |||
862 | public function testServerWrappedClassWithNamespace() |
||
863 | { |
||
864 | $m = new xmlrpcmsg('namespacetest.findState', array( |
||
865 | new xmlrpcval(23, 'int'), |
||
866 | )); |
||
867 | $v = $this->send($m); |
||
868 | $this->assertEquals('Michigan', $v->scalarval()); |
||
869 | } |
||
870 | |||
871 | public function testWrapInexistentMethod() |
||
872 | { |
||
873 | // make a 'deep client copy' as the original one might have many properties set |
||
874 | $func = wrap_xmlrpc_method($this->client, 'examples.getStateName.notexisting', array('simple_client_copy' => 0)); |
||
875 | $this->assertEquals(false, $func); |
||
876 | } |
||
877 | |||
878 | public function testWrapInexistentUrl() |
||
879 | { |
||
880 | $this->client->path = '/notexisting'; |
||
881 | // make a 'deep client copy' as the original one might have many properties set |
||
882 | $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0)); |
||
883 | $this->assertEquals(false, $func); |
||
884 | } |
||
885 | |||
886 | public function testWrappedMethod() |
||
899 | } |
||
900 | } |
||
901 | |||
902 | public function testWrappedMethodAsSource() |
||
903 | { |
||
904 | // make a 'deep client copy' as the original one might have many properties set |
||
905 | $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true)); |
||
917 | } |
||
918 | } |
||
919 | |||
920 | public function testWrappedClass() |
||
921 | { |
||
922 | // make a 'deep client copy' as the original one might have many properties set |
||
923 | // also for speed only wrap one method of the whole server |
||
924 | $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' )); |
||
925 | if ($class == '') { |
||
926 | $this->fail('Registration of remote server failed'); |
||
927 | } else { |
||
928 | $obj = new $class(); |
||
929 | if (!is_callable(array($obj, 'examples_getStateName'))) { |
||
930 | $this->fail('Registration of remote server failed to import method "examples_getStateName"'); |
||
931 | } else { |
||
932 | $v = $obj->examples_getStateName(23); |
||
933 | // work around bug in current (or old?) version of phpunit when reporting the error |
||
934 | /*if (is_object($v)) { |
||
935 | $v = var_export($v, true); |
||
936 | }*/ |
||
937 | $this->assertEquals('Michigan', $v); |
||
938 | } |
||
939 | } |
||
940 | } |
||
941 | |||
942 | public function testTransferOfObjectViaWrapping() |
||
943 | { |
||
944 | // make a 'deep client copy' as the original one might have many properties set |
||
945 | $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => 0, |
||
946 | 'decode_php_objs' => true)); |
||
947 | if ($func == false) { |
||
948 | $this->fail('Registration of tests.returnPhpObject failed'); |
||
949 | } else { |
||
950 | $v = $func(); |
||
951 | $obj = new stdClass(); |
||
952 | $obj->hello = 'world'; |
||
953 | $this->assertEquals($obj, $v); |
||
954 | } |
||
955 | } |
||
956 | |||
957 | public function testGetCookies() |
||
958 | { |
||
959 | // let server set to us some cookies we tell it |
||
960 | $cookies = array( |
||
961 | //'c1' => array(), |
||
962 | 'c2' => array('value' => 'c2'), |
||
963 | 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30), |
||
964 | 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'), |
||
965 | 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'), |
||
966 | ); |
||
967 | $cookiesval = php_xmlrpc_encode($cookies); |
||
968 | $m = new xmlrpcmsg('tests.setcookies', array($cookiesval)); |
||
969 | $r = $this->send($m, 0, true); |
||
970 | if ($r) { |
||
971 | $v = $r->value(); |
||
972 | $this->assertEquals(1, $v->scalarval()); |
||
973 | // now check if we decoded the cookies as we had set them |
||
974 | $rcookies = $r->cookies(); |
||
975 | // remove extra cookies which might have been set by proxies |
||
976 | foreach ($rcookies as $c => $v) { |
||
977 | if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) { |
||
978 | unset($rcookies[$c]); |
||
979 | } |
||
980 | // Seems like we get this when using php-fpm and php 5.5+ ... |
||
981 | if (isset($rcookies[$c]['Max-Age'])) { |
||
982 | unset($rcookies[$c]['Max-Age']); |
||
983 | } |
||
984 | } |
||
985 | foreach ($cookies as $c => $v) { |
||
986 | // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT' |
||
987 | // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'... |
||
988 | if (isset($v['expires'])) { |
||
989 | if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) { |
||
990 | $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']); |
||
991 | } else { |
||
992 | $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']); |
||
993 | } |
||
994 | } |
||
995 | } |
||
996 | |||
997 | $this->assertEquals($cookies, $rcookies); |
||
998 | } |
||
999 | } |
||
1000 | |||
1001 | public function testSetCookies() |
||
1028 | } |
||
1029 | } |
||
1030 | |||
1031 | public function testServerComments() |
||
1032 | { |
||
1033 | $m = new xmlrpcmsg('tests.handlersContainer.debugMessageGenerator', array( |
||
1034 | new xmlrpcval('hello world', 'string'), |
||
1035 | )); |
||
1036 | $r = $this->send($m, 0, true); |
||
1037 | $this->assertStringContainsString('hello world', $r->raw_data); |
||
1038 | } |
||
1039 | |||
1040 | public function testSendTwiceSameMsg() |
||
1049 | } |
||
1050 | } |
||
1051 | } |
||
1052 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: