merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / tests / 4ClientTest.php
1 <?php
2
3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4
5 include_once __DIR__ . '/parse_args.php';
6
7 include_once __DIR__ . '/PolyfillTestCase.php';
8
9 use PHPUnit\Runner\BaseTestRunner;
10
11 /**
12  * Tests involving the Client class.
13  * So far: only tests requests sent to non-existing servers
14  */
15 class ClientTest extends PhpXmlRpc_PolyfillTestCase
16 {
17     /** @var xmlrpc_client $client */
18     public $client = null;
19     public $args = array();
20
21     public function set_up()
22     {
23         $this->args = argParser::getArgs();
24
25         $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['HTTPSERVER'], 80);
26         $this->client->setDebug($this->args['DEBUG']);
27
28         if ($this->args['DEBUG'] == 1)
29             ob_start();
30     }
31
32     protected function tear_down()
33     {
34         if ($this->args['DEBUG'] != 1)
35             return;
36         $out = ob_get_clean();
37         $status = $this->getStatus();
38         if ($status == BaseTestRunner::STATUS_ERROR
39             || $status == BaseTestRunner::STATUS_FAILURE) {
40             echo $out;
41         }
42     }
43
44     public function test404()
45     {
46         $m = new xmlrpcmsg('examples.echo', array(
47             new xmlrpcval('hello', 'string'),
48         ));
49         $r = $this->client->send($m, 5);
50         $this->assertEquals(5, $r->faultCode());
51     }
52
53     public function testSrvNotFound()
54     {
55         $m = new xmlrpcmsg('examples.echo', array(
56             new xmlrpcval('hello', 'string'),
57         ));
58         $this->client->server .= 'XXX';
59         $dnsinfo = @dns_get_record($this->client->server);
60         if ($dnsinfo) {
61             $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
62         } else {
63             $r = $this->client->send($m, 5);
64             // make sure there's no freaking catchall DNS in effect
65             $this->assertEquals(5, $r->faultCode());
66         }
67     }
68
69     public function testCurlKAErr()
70     {
71         if (!function_exists('curl_init')) {
72             $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
73
74             return;
75         }
76         $m = new xmlrpcmsg('examples.stringecho', array(
77             new xmlrpcval('hello', 'string'),
78         ));
79         // test 2 calls w. keepalive: 1st time connection ko, second time ok
80         $this->client->server .= 'XXX';
81         $this->client->keepalive = true;
82         $r = $this->client->send($m, 5, 'http11');
83         // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
84         $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
85
86         // now test a successful connection
87         $server = explode(':', $this->args['HTTPSERVER']);
88         if (count($server) > 1) {
89             $this->client->port = $server[1];
90         }
91         $this->client->server = $server[0];
92         $this->client->path = $this->args['HTTPURI'];
93
94         $r = $this->client->send($m, 5, 'http11');
95         $this->assertEquals(0, $r->faultCode());
96         $ro = $r->value();
97         is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
98     }
99 }