Passed
Push — master ( cc36f0...573db6 )
by Gaetano
09:45
created

ClientTest::testCustomHeaders()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 17
rs 9.9
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 = 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
        $this->client->setOption(\PhpXmlRpc\Client::OPT_USE_CURL, \PhpXmlRpc\Client::USE_CURL_NEVER);
50
        $r = $this->client->send($m);
51
        $this->assertEquals(\PhpXmlRpc\PhpXmlRpc::$xmlrpcerr['unsupported_option'], $r->faultCode());
52
    }
53
54
    public function testSrvNotFound()
55
    {
56
        $m = new xmlrpcmsg('examples.echo', array(
57
            new xmlrpcval('hello', 'string'),
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
            $r = $this->client->send($m, 5);
65
            // make sure there's no freaking catchall DNS in effect
66
            $this->assertEquals(5, $r->faultCode());
67
        }
68
    }
69
70
    public function testCurlKAErr()
71
    {
72
        if (!function_exists('curl_init')) {
73
            $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
74
75
            return;
76
        }
77
        $m = new xmlrpcmsg('examples.stringecho', array(
78
            new xmlrpcval('hello', 'string'),
79
        ));
80
        // test 2 calls w. keepalive: 1st time connection ko, second time ok
81
        $this->client->server .= 'XXX';
82
        $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...
83
        $r = $this->client->send($m, 5, 'http11');
84
        // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
85
        $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
86
87
        // now test a successful connection
88
        $server = explode(':', $this->args['HTTPSERVER']);
89
        if (count($server) > 1) {
90
            $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...
91
        }
92
        $this->client->server = $server[0];
93
        $this->client->path = $this->args['HTTPURI'];
94
        $this->client->setCookie('PHPUNIT_RANDOM_TEST_ID', static::$randId);
95
        $r = $this->client->send($m, 5, 'http11');
96
        $this->assertEquals(0, $r->faultCode());
97
        $ro = $r->value();
98
        is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
99
    }
100
101
    public function testCustomHeaders()
102
    {
103
        $opts = array(\PhpXmlRpc\Client::USE_CURL_NEVER);
104
        if (function_exists('curl_init'))
105
        {
106
            $opts[] = \PhpXmlRpc\Client::USE_CURL_ALWAYS;
107
        }
108
109
        $this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_HEADERS, array('X-PXR-Test: yes'));
110
        $r = new \PhpXmlRpc\Request('tests.getallheaders');
111
112
        foreach ($opts as $opt) {
113
            $this->client->setOption(\PhpXmlRpc\Client::OPT_USE_CURL, $opt);
114
            $r = $this->client->send($r);
0 ignored issues
show
Bug introduced by
It seems like $r can also be of type PhpXmlRpc\Response; however, parameter $req of PhpXmlRpc\Client::send() does only seem to accept PhpXmlRpc\Request|PhpXmlRpc\Request[]|string, maybe add an additional type check? ( Ignorable by Annotation )

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

114
            $r = $this->client->send(/** @scrutinizer ignore-type */ $r);
Loading history...
115
            $this->assertEquals(0, $r->faultCode());
116
            $ro = $r->value();
117
            $this->assertArrayHasKey('X-Pxr-Test', $ro->scalarVal(), "Testing with curl mode: $opt");
118
        }
119
    }
120
121
    public function testgetUrl()
122
    {
123
        $m = $this->client->getUrl(PHP_URL_SCHEME);
124
        $this->assertEquals($m, $this->client->method);
125
        $h = $this->client->getUrl(PHP_URL_HOST);
126
        $this->assertEquals($h, $this->client->server);
127
        $p = $this->client->getUrl(PHP_URL_PORT);
128
        $this->assertEquals($p, $this->client->port);
129
        $p = $this->client->getUrl(PHP_URL_PATH);
130
        $this->assertEquals($p, $this->client->path);
131
    }
132
}
133