Completed
Push — master ( 87b7a4...6ce28d )
by Gaetano
11:11 queued 06:38
created

InvalidHostTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 83
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testSrvNotFound() 0 13 2
A tearDown() 0 9 4
A test404() 0 7 1
A testCurlKAErr() 0 29 5
A setUp() 0 9 2
1
<?php
2
3
include_once __DIR__ . '/../lib/xmlrpc.inc';
4
5
include_once __DIR__ . '/parse_args.php';
6
7
/**
8
 * Tests involving requests sent to non-existing servers
9
 */
10
class InvalidHostTest extends PHPUnit_Framework_TestCase
11
{
12
    /** @var xmlrpc_client $client */
13
    public $client = null;
14
    public $args = array();
15
16
    public function setUp()
17
    {
18
        $this->args = argParser::getArgs();
19
20
        $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['LOCALSERVER'], 80);
21
        $this->client->setDebug($this->args['DEBUG']);
22
23
        if ($this->args['DEBUG'] == 1)
24
            ob_start();
25
    }
26
27
    protected function tearDown()
28
    {
29
        if ($this->args['DEBUG'] != 1)
30
            return;
31
        $out = ob_get_clean();
32
        $status = $this->getStatus();
33
        if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
34
            || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
35
            echo $out;
36
        }
37
    }
38
39
    public function test404()
40
    {
41
        $m = new xmlrpcmsg('examples.echo', array(
42
            new xmlrpcval('hello', 'string'),
43
        ));
44
        $r = $this->client->send($m, 5);
45
        $this->assertEquals(5, $r->faultCode());
46
    }
47
48
    public function testSrvNotFound()
49
    {
50
        $m = new xmlrpcmsg('examples.echo', array(
51
            new xmlrpcval('hello', 'string'),
52
        ));
53
        $this->client->server .= 'XXX';
54
        $r = $this->client->send($m, 5);
55
        // make sure there's no freaking catchall DNS in effect
56
        $dnsinfo = dns_get_record($this->client->server);
57
        if ($dnsinfo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dnsinfo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
59
        } else {
60
            $this->assertEquals(5, $r->faultCode());
61
        }
62
    }
63
64
    public function testCurlKAErr()
65
    {
66
        if (!function_exists('curl_init')) {
67
            $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
68
69
            return;
70
        }
71
        $m = new xmlrpcmsg('examples.stringecho', array(
72
            new xmlrpcval('hello', 'string'),
73
        ));
74
        // test 2 calls w. keepalive: 1st time connection ko, second time ok
75
        $this->client->server .= 'XXX';
76
        $this->client->keepalive = true;
77
        $r = $this->client->send($m, 5, 'http11');
78
        // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
79
        $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
80
81
        // now test a successful connection
82
        $server = explode(':', $this->args['LOCALSERVER']);
83
        if (count($server) > 1) {
84
            $this->client->port = $server[1];
85
        }
86
        $this->client->server = $server[0];
87
        $this->client->path = $this->args['URI'];
88
89
        $r = $this->client->send($m, 5, 'http11');
90
        $this->assertEquals(0, $r->faultCode());
91
        $ro = $r->value();
92
        is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
93
    }
94
}
95