3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
6 include_once __DIR__ . '/parse_args.php';
8 include_once __DIR__ . '/PolyfillTestCase.php';
10 use PHPUnit\Extensions\SeleniumCommon\RemoteCoverage;
11 use PHPUnit\Framework\TestResult;
12 use PHPUnit\Runner\BaseTestRunner;
15 * Tests which involve interaction between the client and the server.
16 * They are run against the server found in demo/server.php.
17 * Includes testing of (some of) the Wrapper class
19 class ServerTest extends PhpXmlRpc_PolyfillTestCase
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();
29 protected static $failed_tests = array();
32 /** @var boolean $collectCodeCoverageInformation */
33 protected $collectCodeCoverageInformation;
34 protected $coverageScriptUrl;
37 * @todo instead of overriding fail via _fail, implement Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation
39 public static function _fail($message = '')
41 // save in a static var that this particular test has failed
42 // (but only if not called from subclass objects / multitests)
43 if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
44 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
45 for ($i = 0; $i < count($trace); $i++) {
46 if (strpos($trace[$i]['function'], 'test') === 0) {
47 self::$failed_tests[$trace[$i]['function']] = true;
53 parent::_fail($message);
57 * Reimplemented to allow us to collect code coverage info from the target server.
58 * Code taken from PHPUnit_Extensions_Selenium2TestCase
60 * @param TestResult $result
64 * @todo instead of overriding run via _run, try to achieve this by implementing Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation
66 public function _run($result = NULL)
68 $this->testId = get_class($this) . '__' . $this->getName();
70 if ($result === NULL) {
71 $result = $this->createResult();
74 $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
76 parent::_run($result);
78 if ($this->collectCodeCoverageInformation) {
79 $coverage = new RemoteCoverage(
80 $this->coverageScriptUrl,
83 $result->getCodeCoverage()->append(
84 $coverage->get(), $this
88 // do not call this before to give the time to the Listeners to run
89 //$this->getStrategy()->endOfTest($this->session);
94 public function set_up()
96 $this->args = argParser::getArgs();
98 $server = explode(':', $this->args['HTTPSERVER']);
99 if (count($server) > 1) {
100 $this->client = new xmlrpc_client($this->args['HTTPURI'], $server[0], $server[1]);
102 $this->client = new xmlrpc_client($this->args['HTTPURI'], $this->args['HTTPSERVER']);
105 $this->client->setDebug($this->args['DEBUG']);
106 $this->client->request_compression = $this->request_compression;
107 $this->client->accepted_compression = $this->accepted_compression;
109 $this->coverageScriptUrl = 'http://' . $this->args['HTTPSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['HTTPURI'] );
111 if ($this->args['DEBUG'] == 1)
115 protected function tear_down()
117 if ($this->args['DEBUG'] != 1)
119 $out = ob_get_clean();
120 $status = $this->getStatus();
121 if ($status == BaseTestRunner::STATUS_ERROR
122 || $status == BaseTestRunner::STATUS_FAILURE) {
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
133 protected function send($msg, $errorCode = 0, $returnResponse = false)
135 if ($this->collectCodeCoverageInformation) {
136 $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
139 $r = $this->client->send($msg, $this->timeout, $this->method);
140 // for multicall, return directly array of responses
144 if (is_array($errorCode)) {
145 $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
147 $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
149 if (!$r->faultCode()) {
150 if ($returnResponse) {
161 * Adds (and replaces) query params to the url currently used by the client
164 protected function addQueryParams($data)
166 $query = parse_url($this->client->path, PHP_URL_QUERY);
167 parse_str($query, $vars);
168 $query = http_build_query(array_merge($vars, $data));
169 $this->client->path = parse_url($this->client->path, PHP_URL_PATH) . '?' . $query;
172 public function testString()
174 $sendString = "here are 3 \"entities\": < > & " .
175 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
176 " - isn't that great? \\\"hackery\\\" at it's best " .
177 " also don't want to miss out on \$item[0]. " .
178 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
179 "a simple CR here" . chr(13) .
180 "a simple LF here" . chr(10) .
181 "and then LFCR" . chr(10) . chr(13) .
182 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
183 $m = new xmlrpcmsg('examples.stringecho', array(
184 new xmlrpcval($sendString, 'string'),
186 $v = $this->send($m);
188 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
189 // so we relax our tests...
190 $l1 = strlen($sendString);
191 $l2 = strlen($v->scalarval());
193 $this->assertEquals($sendString, $v->scalarval());
195 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
200 public function testLatin1String()
203 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
204 $x = '<?xml version="1.0" encoding="ISO-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
206 '</value></param></params></methodCall>';
207 $v = $this->send($x);
209 $this->assertEquals($sendString, $v->scalarval());
213 public function testExoticCharsetsRequests()
215 // note that we should disable this call also when mbstring is missing server-side
216 if (!function_exists('mb_convert_encoding')) {
217 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
220 $sendString = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
221 $str = '<?xml version="1.0" encoding="_ENC_"?>
223 <methodName>examples.stringecho</methodName>
226 <value><string>'.$sendString.'</string></value>
231 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
232 // we have to set the encoding declaration either in the http header or xml prolog, as mb_detect_encoding
233 // (used on the server side) will fail recognizing these 2 charsets
234 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UCS-4', $str), 'UCS-4', 'UTF-8'));
235 $this->assertEquals($sendString, $v->scalarval());
236 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UTF-16', $str), 'UTF-16', 'UTF-8'));
237 $this->assertEquals($sendString, $v->scalarval());
238 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
241 public function testExoticCharsetsRequests2()
243 // note that we should disable this call also when mbstring is missing server-side
244 if (!function_exists('mb_convert_encoding')) {
245 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
248 $sendString = '安室奈美恵'; // No idea what this means :-) NB: NOT a valid ISO8859 string!
249 $str = '<?xml version="1.0"?>
251 <methodName>examples.stringecho</methodName>
254 <value><string>'.$sendString.'</string></value>
259 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
260 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
261 // (used on the server side) sort it out
262 $this->addQueryParams(array('DETECT_ENCODINGS' => array('EUC-JP', 'UTF-8')));
263 $v = $this->send(mb_convert_encoding($str, 'EUC-JP', 'UTF-8'));
264 $this->assertEquals($sendString, $v->scalarval());
265 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
268 public function testExoticCharsetsRequests3()
270 // note that we should disable this call also when mbstring is missing server-side
271 if (!function_exists('mb_convert_encoding')) {
272 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
275 $sendString = utf8_decode('élève');
276 $str = '<?xml version="1.0"?>
278 <methodName>examples.stringecho</methodName>
281 <value><string>'.$sendString.'</string></value>
286 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
287 // (used on the server side) sort it out
288 $this->addQueryParams(array('DETECT_ENCODINGS' => array('ISO-8859-1', 'UTF-8')));
289 $v = $this->send($str);
290 $this->assertEquals($sendString, $v->scalarval());
293 /*public function testLatin1Method()
295 $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
296 new xmlrpcval('hello')
298 $v = $this->send($f);
300 $this->assertEquals('hello', $v->scalarval());
304 public function testUtf8Method()
306 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
307 $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
308 new xmlrpcval('hello')
310 $v = $this->send($m);
312 $this->assertEquals('hello', $v->scalarval());
314 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
317 public function testAddingDoubles()
319 // note that rounding errors mean we
320 // keep precision to sensible levels here ;-)
323 $m = new xmlrpcmsg('examples.addtwodouble', array(
324 new xmlrpcval($a, 'double'),
325 new xmlrpcval($b, 'double'),
327 $v = $this->send($m);
329 $this->assertEquals($a + $b, $v->scalarval());
333 public function testAdding()
335 $m = new xmlrpcmsg('examples.addtwo', array(
336 new xmlrpcval(12, 'int'),
337 new xmlrpcval(-23, 'int'),
339 $v = $this->send($m);
341 $this->assertEquals(12 - 23, $v->scalarval());
345 public function testInvalidNumber()
347 $m = new xmlrpcmsg('examples.addtwo', array(
348 new xmlrpcval('fred', 'int'),
349 new xmlrpcval("\"; exec('ls')", 'int'),
351 $v = $this->send($m);
352 /// @todo a fault condition should be generated here
353 /// by the server, which we pick up on
355 $this->assertEquals(0, $v->scalarval());
359 public function testBoolean()
361 $m = new xmlrpcmsg('examples.invertBooleans', array(
363 new xmlrpcval(true, 'boolean'),
364 new xmlrpcval(false, 'boolean'),
365 new xmlrpcval(1, 'boolean'),
366 new xmlrpcval(0, 'boolean')
371 $v = $this->send($m);
373 $sz = $v->arraysize();
375 for ($i = 0; $i < $sz; $i++) {
376 $b = $v->arraymem($i);
377 if ($b->scalarval()) {
383 $this->assertEquals($answer, $got);
387 public function testBase64()
389 $sendString = 'Mary had a little lamb,
390 Whose fleece was white as snow,
391 And everywhere that Mary went
392 the lamb was sure to go.
394 Mary had a little lamb
395 She tied it to a pylon
396 Ten thousand volts went down its back
397 And turned it into nylon';
398 $m = new xmlrpcmsg('examples.decode64', array(
399 new xmlrpcval($sendString, 'base64'),
401 $v = $this->send($m);
403 if (strlen($sendString) == strlen($v->scalarval())) {
404 $this->assertEquals($sendString, $v->scalarval());
406 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
411 public function testDateTime()
414 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
415 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
416 $this->assertEquals($t1->serialize(), $t2->serialize());
417 if (class_exists('DateTime')) {
418 $datetime = new DateTime();
419 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
420 if (is_callable(array($datetime, 'setTimestamp'))) {
421 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
422 $this->assertEquals($t1->serialize(), $t3->serialize());
427 public function testCountEntities()
429 $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
430 $m = new xmlrpcmsg('validator1.countTheEntities', array(
431 new xmlrpcval($sendString, 'string'),
433 $v = $this->send($m);
437 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
438 foreach($expect_array as $val) {
439 $b = $v->structmem($val);
440 $got .= $b->me['int'];
442 $this->assertEquals($expected, $got);
446 public function _multicall_msg($method, $params)
448 $struct['methodName'] = new xmlrpcval($method, 'string');
449 $struct['params'] = new xmlrpcval($params, 'array');
451 return new xmlrpcval($struct, 'struct');
454 public function testServerMulticall()
456 // We manually construct a system.multicall() call to ensure
457 // that the server supports it.
459 // NB: This test will NOT pass if server does not support system.multicall.
461 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
462 $good1 = $this->_multicall_msg(
464 array(php_xmlrpc_encode('system.listMethods')));
465 $bad = $this->_multicall_msg(
467 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
468 $recursive = $this->_multicall_msg(
470 array(new xmlrpcval(array(), 'array')));
471 $good2 = $this->_multicall_msg(
472 'system.methodSignature',
473 array(php_xmlrpc_encode('system.listMethods')));
474 $arg = new xmlrpcval(
475 array($good1, $bad, $recursive, $good2),
479 $m = new xmlrpcmsg('system.multicall', array($arg));
480 $v = $this->send($m);
482 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
483 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
485 $r1 = $v->arraymem(0);
487 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
488 "did not get array of size 1 from good1"
491 $r2 = $v->arraymem(1);
493 $r2->kindOf() == 'struct',
497 $r3 = $v->arraymem(2);
499 $r3->kindOf() == 'struct',
500 "recursive system.multicall did not fail"
503 $r4 = $v->arraymem(3);
505 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
506 "did not get array of size 1 from good2"
511 public function testClientMulticall1()
513 // NB: This test will NOT pass if server does not support system.multicall.
515 $noMultiCall = $this->client->no_multicall;
516 $this->client->no_multicall = false;
518 $good1 = new xmlrpcmsg('system.methodHelp',
519 array(php_xmlrpc_encode('system.listMethods')));
520 $bad = new xmlrpcmsg('test.nosuch',
521 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
522 $recursive = new xmlrpcmsg('system.multicall',
523 array(new xmlrpcval(array(), 'array')));
524 $good2 = new xmlrpcmsg('system.methodSignature',
525 array(php_xmlrpc_encode('system.listMethods'))
528 $r = $this->send(array($good1, $bad, $recursive, $good2));
530 $this->assertTrue(count($r) == 4, "wrong number of return values");
533 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
534 if (!$r[0]->faultCode()) {
535 $val = $r[0]->value();
537 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
538 "good1 did not return string"
541 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
542 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
543 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
544 if (!$r[3]->faultCode()) {
545 $val = $r[3]->value();
546 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
548 // This is the only assert in this test which should fail
549 // if the test server does not support system.multicall.
550 $this->assertTrue($this->client->no_multicall == false,
551 "server does not support system.multicall"
554 $this->client->no_multicall = $noMultiCall;
557 public function testClientMulticall2()
559 // NB: This test will NOT pass if server does not support system.multicall.
561 $noMultiCall = $this->client->no_multicall;
562 $this->client->no_multicall = true;
564 $good1 = new xmlrpcmsg('system.methodHelp',
565 array(php_xmlrpc_encode('system.listMethods')));
566 $bad = new xmlrpcmsg('test.nosuch',
567 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
568 $recursive = new xmlrpcmsg('system.multicall',
569 array(new xmlrpcval(array(), 'array')));
570 $good2 = new xmlrpcmsg('system.methodSignature',
571 array(php_xmlrpc_encode('system.listMethods'))
574 $r = $this->send(array($good1, $bad, $recursive, $good2));
576 $this->assertTrue(count($r) == 4, "wrong number of return values");
579 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
580 if (!$r[0]->faultCode()) {
581 $val = $r[0]->value();
583 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
584 "good1 did not return string");
586 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
587 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
588 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
589 if (!$r[3]->faultCode()) {
590 $val = $r[3]->value();
591 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
594 $this->client->no_multicall = $noMultiCall;
597 public function testClientMulticall3()
599 // NB: This test will NOT pass if server does not support system.multicall.
601 $noMultiCall = $this->client->no_multicall;
602 $returnType = $this->client->return_type;
604 $this->client->return_type = 'phpvals';
605 $this->client->no_multicall = false;
607 $good1 = new xmlrpcmsg('system.methodHelp',
608 array(php_xmlrpc_encode('system.listMethods')));
609 $bad = new xmlrpcmsg('test.nosuch',
610 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
611 $recursive = new xmlrpcmsg('system.multicall',
612 array(new xmlrpcval(array(), 'array')));
613 $good2 = new xmlrpcmsg('system.methodSignature',
614 array(php_xmlrpc_encode('system.listMethods'))
617 $r = $this->send(array($good1, $bad, $recursive, $good2));
619 $this->assertTrue(count($r) == 4, "wrong number of return values");
621 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
622 if (!$r[0]->faultCode()) {
623 $val = $r[0]->value();
625 is_string($val), "good1 did not return string");
627 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
628 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
629 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
630 if (!$r[3]->faultCode()) {
631 $val = $r[3]->value();
632 $this->assertTrue(is_array($val), "good2 did not return array");
635 $this->client->return_type = $returnType;
636 $this->client->no_multicall = $noMultiCall;
639 public function testCatchWarnings()
641 $m = new xmlrpcmsg('tests.generatePHPWarning', array(
642 new xmlrpcval('whatever', 'string'),
644 $v = $this->send($m);
646 $this->assertEquals(true, $v->scalarval());
650 public function testCatchExceptions()
652 $m = new xmlrpcmsg('tests.raiseException', array(
653 new xmlrpcval('whatever', 'string'),
655 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
656 $this->addQueryParams(array('EXCEPTION_HANDLING' => 1));
657 $v = $this->send($m, 1); // the error code of the expected exception
658 $this->addQueryParams(array('EXCEPTION_HANDLING' => 2));
659 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
660 // as php will generate an http status code of either 200 or 500...
661 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
664 public function testZeroParams()
666 $m = new xmlrpcmsg('system.listMethods');
667 $v = $this->send($m);
670 public function testNullParams()
672 $m = new xmlrpcmsg('tests.getStateName.12', array(
673 new xmlrpcval('whatever', 'null'),
674 new xmlrpcval(23, 'int'),
676 $v = $this->send($m);
678 $this->assertEquals('Michigan', $v->scalarval());
680 $m = new xmlrpcmsg('tests.getStateName.12', array(
681 new xmlrpcval(23, 'int'),
682 new xmlrpcval('whatever', 'null'),
684 $v = $this->send($m);
686 $this->assertEquals('Michigan', $v->scalarval());
688 $m = new xmlrpcmsg('tests.getStateName.12', array(
689 new xmlrpcval(23, 'int')
691 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
694 public function testCodeInjectionServerSide()
696 $m = new xmlrpcmsg('system.MethodHelp');
697 $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>";
698 $v = $this->send($m);
700 $this->assertEquals(0, $v->structsize());
704 public function testServerWrappedFunction()
706 $m = new xmlrpcmsg('tests.getStateName.2', array(
707 new xmlrpcval(23, 'int'),
709 $v = $this->send($m);
710 $this->assertEquals('Michigan', $v->scalarval());
712 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
713 $m = new xmlrpcmsg('tests.getStateName.2', array(
714 new xmlrpcval(0, 'int'),
716 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
718 // check if the generated function dispatch map is fine, by checking if the server registered it
719 $m = new xmlrpcmsg('system.methodSignature', array(
720 new xmlrpcval('tests.getStateName.2'),
722 $v = $this->send($m);
723 $encoder = new \PhpXmlRpc\Encoder();
724 $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
727 public function testServerWrappedFunctionAsSource()
729 $m = new xmlrpcmsg('tests.getStateName.6', array(
730 new xmlrpcval(23, 'int'),
732 $v = $this->send($m);
733 $this->assertEquals('Michigan', $v->scalarval());
735 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
736 $m = new xmlrpcmsg('tests.getStateName.6', array(
737 new xmlrpcval(0, 'int'),
739 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
742 public function testServerWrappedObjectMethods()
744 $m = new xmlrpcmsg('tests.getStateName.3', array(
745 new xmlrpcval(23, 'int'),
747 $v = $this->send($m);
748 $this->assertEquals('Michigan', $v->scalarval());
750 $m = new xmlrpcmsg('tests.getStateName.4', array(
751 new xmlrpcval(23, 'int'),
753 $v = $this->send($m);
754 $this->assertEquals('Michigan', $v->scalarval());
756 $m = new xmlrpcmsg('tests.getStateName.5', array(
757 new xmlrpcval(23, 'int'),
759 $v = $this->send($m);
760 $this->assertEquals('Michigan', $v->scalarval());
762 $m = new xmlrpcmsg('tests.getStateName.7', array(
763 new xmlrpcval(23, 'int'),
765 $v = $this->send($m);
766 $this->assertEquals('Michigan', $v->scalarval());
768 $m = new xmlrpcmsg('tests.getStateName.8', array(
769 new xmlrpcval(23, 'int'),
771 $v = $this->send($m);
772 $this->assertEquals('Michigan', $v->scalarval());
774 $m = new xmlrpcmsg('tests.getStateName.9', array(
775 new xmlrpcval(23, 'int'),
777 $v = $this->send($m);
778 $this->assertEquals('Michigan', $v->scalarval());
781 public function testServerWrappedObjectMethodsAsSource()
783 $m = new xmlrpcmsg('tests.getStateName.7', array(
784 new xmlrpcval(23, 'int'),
786 $v = $this->send($m);
787 $this->assertEquals('Michigan', $v->scalarval());
789 $m = new xmlrpcmsg('tests.getStateName.8', array(
790 new xmlrpcval(23, 'int'),
792 $v = $this->send($m);
793 $this->assertEquals('Michigan', $v->scalarval());
795 $m = new xmlrpcmsg('tests.getStateName.9', array(
796 new xmlrpcval(23, 'int'),
798 $v = $this->send($m);
799 $this->assertEquals('Michigan', $v->scalarval());
802 public function testServerClosure()
804 $m = new xmlrpcmsg('tests.getStateName.10', array(
805 new xmlrpcval(23, 'int'),
807 $v = $this->send($m);
808 $this->assertEquals('Michigan', $v->scalarval());
811 public function testServerWrappedClosure()
813 $m = new xmlrpcmsg('tests.getStateName.11', array(
814 new xmlrpcval(23, 'int'),
816 $v = $this->send($m);
817 $this->assertEquals('Michigan', $v->scalarval());
820 public function testServerWrappedClass()
822 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
823 new xmlrpcval(23, 'int'),
825 $v = $this->send($m);
826 $this->assertEquals('Michigan', $v->scalarval());
829 public function testWrapInexistentMethod()
831 // make a 'deep client copy' as the original one might have many properties set
832 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName.notexisting', array('simple_client_copy' => 0));
833 $this->assertEquals(false, $func);
836 public function testWrapInexistentUrl()
838 $this->client->path = '/notexisting';
839 // make a 'deep client copy' as the original one might have many properties set
840 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
841 $this->assertEquals(false, $func);
844 public function testServerWrappedClassWithNamespace()
846 $m = new xmlrpcmsg('namespacetest.findState', array(
847 new xmlrpcval(23, 'int'),
849 $v = $this->send($m);
850 $this->assertEquals('Michigan', $v->scalarval());
853 public function testWrappedMethod()
855 // make a 'deep client copy' as the original one might have many properties set
856 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
857 if ($func == false) {
858 $this->fail('Registration of examples.getStateName failed');
861 // work around bug in current (or old?) version of phpunit when reporting the error
862 /*if (is_object($v)) {
863 $v = var_export($v, true);
865 $this->assertEquals('Michigan', $v);
869 public function testWrappedMethodAsSource()
871 // make a 'deep client copy' as the original one might have many properties set
872 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
873 if ($func == false) {
874 $this->fail('Registration of examples.getStateName failed');
876 eval($func['source']);
877 $func = $func['function'];
879 // work around bug in current (or old?) version of phpunit when reporting the error
880 /*if (is_object($v)) {
881 $v = var_export($v, true);
883 $this->assertEquals('Michigan', $v);
887 public function testWrappedClass()
889 // make a 'deep client copy' as the original one might have many properties set
890 // also for speed only wrap one method of the whole server
891 $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
893 $this->fail('Registration of remote server failed');
896 if (!is_callable(array($obj, 'examples_getStateName'))) {
897 $this->fail('Registration of remote server failed to import method "examples_getStateName"');
899 $v = $obj->examples_getStateName(23);
900 // work around bug in current (or old?) version of phpunit when reporting the error
901 /*if (is_object($v)) {
902 $v = var_export($v, true);
904 $this->assertEquals('Michigan', $v);
909 public function testTransferOfObjectViaWrapping()
911 // make a 'deep client copy' as the original one might have many properties set
912 $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => 0,
913 'decode_php_objs' => true));
914 if ($func == false) {
915 $this->fail('Registration of tests.returnPhpObject failed');
918 $obj = new stdClass();
919 $obj->hello = 'world';
920 $this->assertEquals($obj, $v);
924 public function testGetCookies()
926 // let server set to us some cookies we tell it
929 'c2' => array('value' => 'c2'),
930 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
931 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
932 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
934 $cookiesval = php_xmlrpc_encode($cookies);
935 $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
936 $r = $this->send($m, 0, true);
939 $this->assertEquals(1, $v->scalarval());
940 // now check if we decoded the cookies as we had set them
941 $rcookies = $r->cookies();
942 // remove extra cookies which might have been set by proxies
943 foreach ($rcookies as $c => $v) {
944 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
945 unset($rcookies[$c]);
947 // Seems like we get this when using php-fpm and php 5.5+ ...
948 if (isset($rcookies[$c]['Max-Age'])) {
949 unset($rcookies[$c]['Max-Age']);
952 foreach ($cookies as $c => $v) {
953 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
954 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
955 if (isset($v['expires'])) {
956 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
957 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
959 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
964 $this->assertEquals($cookies, $rcookies);
968 public function testSetCookies()
970 // let server set to us some cookies we tell it
975 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
977 $m = new xmlrpcmsg('examples.getcookies', array());
978 foreach ($cookies as $cookie => $val) {
979 $this->client->setCookie($cookie, $val);
980 $cookies[$cookie] = (string)$cookies[$cookie];
982 $r = $this->client->send($m, $this->timeout, $this->method);
983 $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
984 if (!$r->faultCode()) {
986 $v = php_xmlrpc_decode($v);
988 // take care for the extra cookie used for coverage collection
989 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
990 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
993 // on IIS and Apache getallheaders returns something slightly different...
994 $this->assertEquals($cookies, $v);
998 public function testServerComments()
1000 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
1001 new xmlrpcval('hello world', 'string'),
1003 $r = $this->send($m, 0, true);
1004 $this->assertStringContainsString('hello world', $r->raw_data);
1007 public function testSendTwiceSameMsg()
1009 $m = new xmlrpcmsg('examples.stringecho', array(
1010 new xmlrpcval('hello world', 'string'),
1012 $v1 = $this->send($m);
1013 $v2 = $this->send($m);
1015 $this->assertEquals($v1, $v2);