Passed
Push — master ( ca95a4...b9175a )
by Gaetano
08:52
created

ClientTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 2
Metric Value
eloc 57
c 3
b 2
f 2
dl 0
loc 102
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testSrvNotFound() 0 13 2
A testCurlKAErr() 0 29 5
A test404() 0 7 1
A set_up() 0 6 1
A test404Interop() 0 10 1
A testgetUrl() 0 10 1
A testUnsupportedAuth() 0 9 1
1
<?php
2
3
include_once __DIR__ . '/LoggerAwareTestCase.php';
4
5
/**
6
 * Tests involving the Client class (and no server).
7
 */
8
class ClientTest extends PhpXmlRpc_LoggerAwareTestCase
9
{
10
    /** @var xmlrpc_client $client */
11
    public $client = null;
12
13
    public function set_up()
14
    {
15
        parent::set_up();
16
17
        $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['HTTPSERVER'], 80);
18
        $this->client->setDebug($this->args['DEBUG']);
19
    }
20
21
    public function test404()
22
    {
23
        $m = new xmlrpcmsg('examples.echo', array(
24
            new xmlrpcval('hello', 'string'),
25
        ));
26
        $r = $this->client->send($m, 5);
27
        $this->assertEquals(5, $r->faultCode());
28
    }
29
30
    public function test404Interop()
31
    {
32
        $m = new xmlrpcmsg('examples.echo', array(
33
            new xmlrpcval('hello', 'string'),
34
        ));
35
        $orig = \PhpXmlRpc\PhpXmlRpc::$xmlrpcerr;
36
        \PhpXmlRpc\PhpXmlRpc::useInteropFaults();
37
        $r = $this->client->send($m, 5);
38
        $this->assertEquals(-32300, $r->faultCode());
39
        \PhpXmlRpc\PhpXmlRpc::$xmlrpcerr = $orig;
40
    }
41
42
    public function testUnsupportedAuth()
43
    {
44
        $m = new xmlrpcmsg('examples.echo', array(
45
            new xmlrpcval('hello', 'string'),
46
        ));
47
        $this->client->setOption(\PhpXmlRpc\Client::OPT_USERNAME, 'user');
48
        $this->client->setOption(\PhpXmlRpc\Client::OPT_AUTH_TYPE, 2);
49
        $r = $this->client->send($m);
50
        $this->assertEquals(\PhpXmlRpc\PhpXmlRpc::$xmlrpcerr['unsupported_option'], $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;
0 ignored issues
show
Bug Best Practice introduced by
The property $keepalive is declared protected in PhpXmlRpc\Client. Since you implement __set, consider adding a @property or @property-write.
Loading history...
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];
0 ignored issues
show
Documentation Bug introduced by
The property $port was declared of type integer, but $server[1] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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
100
    public function testgetUrl()
101
    {
102
        $m = $this->client->getUrl(PHP_URL_SCHEME);
103
        $this->assertEquals($m, $this->client->method);
104
        $h = $this->client->getUrl(PHP_URL_HOST);
105
        $this->assertEquals($h, $this->client->server);
106
        $p = $this->client->getUrl(PHP_URL_PORT);
107
        $this->assertEquals($p, $this->client->port);
108
        $p = $this->client->getUrl(PHP_URL_PATH);
109
        $this->assertEquals($p, $this->client->path);
110
    }
111
}
112