Passed
Push — master ( 573db6...fc8ac8 )
by Gaetano
10:15
created

ClientTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 4
Bugs 3 Features 2
Metric Value
eloc 69
c 4
b 3
f 2
dl 0
loc 131
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A test404() 0 9 1
A getAvailableUseCurlOptions() 0 9 2
A testgetUrl() 0 10 1
A set_up() 0 5 1
A testCustomHeaders() 0 9 1
A testSrvNotFound() 0 13 2
A testCurlKAErr() 0 29 5
A test404Interop() 0 12 1
A testUnsupportedAuth() 0 10 1
1
<?php
2
3
include_once __DIR__ . '/ServerAwareTestCase.php';
4
5
/**
6
 * Tests involving the Client class (and mostly no server).
7
 */
8
class ClientTest extends PhpXmlRpc_ServerAwareTestCase
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 = $this->getClient();
0 ignored issues
show
Bug introduced by
The call to PhpXmlRpc_ServerAwareTestCase::getClient() has too few arguments starting with customPath. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        /** @scrutinizer ignore-call */ 
18
        $this->client = $this->getClient();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
18
    }
19
20
    public function test404()
21
    {
22
        $this->client->path = '/NOTEXIST.php';
23
24
        $m = new xmlrpcmsg('examples.echo', array(
25
            new xmlrpcval('hello', 'string'),
26
        ));
27
        $r = $this->client->send($m, 5);
28
        $this->assertEquals(5, $r->faultCode());
29
    }
30
31
    public function test404Interop()
32
    {
33
        $this->client->path = '/NOTEXIST.php';
34
35
        $m = new xmlrpcmsg('examples.echo', array(
36
            new xmlrpcval('hello', 'string'),
37
        ));
38
        $orig = \PhpXmlRpc\PhpXmlRpc::$xmlrpcerr;
39
        \PhpXmlRpc\PhpXmlRpc::useInteropFaults();
40
        $r = $this->client->send($m, 5);
41
        $this->assertEquals(-32300, $r->faultCode());
42
        \PhpXmlRpc\PhpXmlRpc::$xmlrpcerr = $orig;
43
    }
44
45
    public function testUnsupportedAuth()
46
    {
47
        $m = new xmlrpcmsg('examples.echo', array(
48
            new xmlrpcval('hello', 'string'),
49
        ));
50
        $this->client->setOption(\PhpXmlRpc\Client::OPT_USERNAME, 'user');
51
        $this->client->setOption(\PhpXmlRpc\Client::OPT_AUTH_TYPE, 2);
52
        $this->client->setOption(\PhpXmlRpc\Client::OPT_USE_CURL, \PhpXmlRpc\Client::USE_CURL_NEVER);
53
        $r = $this->client->send($m);
54
        $this->assertEquals(\PhpXmlRpc\PhpXmlRpc::$xmlrpcerr['unsupported_option'], $r->faultCode());
55
    }
56
57
    public function testSrvNotFound()
58
    {
59
        $this->client->server .= 'XXX';
60
        $dnsinfo = @dns_get_record($this->client->server);
61
        if ($dnsinfo) {
62
            $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
63
        } else {
64
            $m = new xmlrpcmsg('examples.echo', array(
65
                new xmlrpcval('hello', 'string'),
66
            ));
67
            $r = $this->client->send($m, 5);
68
            // make sure there's no freaking catchall DNS in effect
69
            $this->assertEquals(5, $r->faultCode());
70
        }
71
    }
72
73
    public function testCurlKAErr()
74
    {
75
        if (!function_exists('curl_init')) {
76
            $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
77
78
            return;
79
        }
80
        $m = new xmlrpcmsg('examples.stringecho', array(
81
            new xmlrpcval('hello', 'string'),
82
        ));
83
        // test 2 calls w. keepalive: 1st time connection ko, second time ok
84
        $this->client->server .= 'XXX';
85
        $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...
86
        $r = $this->client->send($m, 5, 'http11');
87
        // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
88
        $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
89
90
        // now test a successful connection
91
        $server = explode(':', $this->args['HTTPSERVER']);
92
        if (count($server) > 1) {
93
            $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...
94
        }
95
        $this->client->server = $server[0];
96
        //$this->client->path = $this->args['HTTPURI'];
97
        //$this->client->setCookie('PHPUNIT_RANDOM_TEST_ID', static::$randId);
98
        $r = $this->client->send($m, 5, 'http11');
99
        $this->assertEquals(0, $r->faultCode());
100
        $ro = $r->value();
101
        is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
102
    }
103
104
    /**
105
     * @dataProvider getAvailableUseCurlOptions
106
     */
107
    public function testCustomHeaders($curlOpt)
108
    {
109
        $this->client->setOption(\PhpXmlRpc\Client::OPT_USE_CURL, $curlOpt);
110
        $this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_HEADERS, array('X-PXR-Test: yes'));
111
        $r = new \PhpXmlRpc\Request('tests.getallheaders');
112
        $r = $this->client->send($r);
113
        $this->assertEquals(0, $r->faultCode());
114
        $ro = $r->value();
115
        $this->assertArrayHasKey('X-Pxr-Test', $ro->scalarVal(), "Testing with curl mode: $curlOpt");
116
    }
117
118
    public function getAvailableUseCurlOptions()
119
    {
120
        $opts = array(\PhpXmlRpc\Client::USE_CURL_NEVER);
121
        if (function_exists('curl_init'))
122
        {
123
            $opts[] = \PhpXmlRpc\Client::USE_CURL_ALWAYS;
124
        }
125
126
        return array($opts);
127
    }
128
129
    public function testgetUrl()
130
    {
131
        $m = $this->client->getUrl(PHP_URL_SCHEME);
132
        $this->assertEquals($m, $this->client->method);
133
        $h = $this->client->getUrl(PHP_URL_HOST);
134
        $this->assertEquals($h, $this->client->server);
135
        $p = $this->client->getUrl(PHP_URL_PORT);
136
        $this->assertEquals($p, $this->client->port);
137
        $p = $this->client->getUrl(PHP_URL_PATH);
138
        $this->assertEquals($p, $this->client->path);
139
    }
140
}
141