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