PhpXmlRpc_WebTestCase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 30
c 4
b 1
f 0
dl 0
loc 69
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 38 5
A newClient() 0 11 2
1
<?php
2
3
include_once __DIR__ . '/ServerAwareTestCase.php';
4
5
abstract class PhpXmlRpc_WebTestCase extends PhpXmlRpc_ServerAwareTestCase
6
{
7
    /**
8
     * Make an HTTP request, check that the result is a 200 OK page with no php fatal error or warning messages.
9
     *
10
     * @param string $path
11
     * @param string $method
12
     * @param string $payload
13
     * @param false $emptyPageOk
14
     * @return bool|string
15
     */
16
    protected function request($path, $method = 'GET', $payload = '', $emptyPageOk = false)
17
    {
18
        $url = $this->baseUrl . $path;
19
20
        $ch = curl_init($url);
21
        curl_setopt_array($ch, array(
22
            CURLOPT_RETURNTRANSFER => true,
23
            CURLOPT_FAILONERROR => true
24
        ));
25
        if ($method == 'POST')
26
        {
27
            curl_setopt_array($ch, array(
28
                CURLOPT_POST => true,
29
                CURLOPT_POSTFIELDS => $payload
30
            ));
31
        }
32
        $cookie = 'PHPUNIT_RANDOM_TEST_ID=' . static::$randId;
33
        if ($this->collectCodeCoverageInformation)
34
        {
35
            $cookie .= '; PHPUNIT_SELENIUM_TEST_ID=' . $this->testId;
36
        }
37
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
38
39
        if ($this->args['DEBUG'] > 0) {
40
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
41
        }
42
        $page = curl_exec($ch);
43
        $info = curl_getinfo($ch);
44
        curl_close($ch);
45
46
        $this->assertNotFalse($page, 'Curl request should not fail. Url: ' . @$info['url'] . ', Http code: ' . @$info['http_code']);
47
        if (!$emptyPageOk) {
0 ignored issues
show
introduced by
The condition $emptyPageOk is always false.
Loading history...
48
            $this->assertNotEquals('', $page, 'Retrieved web page should not be empty');
49
        }
50
        $this->assertStringNotContainsStringIgnoringCase('Fatal error', $page, 'Retrieved web page should not contain a fatal error string');
0 ignored issues
show
Bug introduced by
It seems like $page can also be of type true; however, parameter $haystack of PHPUnit\Framework\Assert...insStringIgnoringCase() does only seem to accept 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

50
        $this->assertStringNotContainsStringIgnoringCase('Fatal error', /** @scrutinizer ignore-type */ $page, 'Retrieved web page should not contain a fatal error string');
Loading history...
51
        $this->assertStringNotContainsStringIgnoringCase('Notice:', $page, 'Retrieved web page should not contain a notice string');
52
53
        return $page;
54
    }
55
56
    /**
57
     * Build an xml-rpc client, tweaked if needed to collect code-coverage information of the server.
58
     * @see also ServerTest::set_up
59
     *
60
     * @param string $path
61
     * @return \PhpXmlRpc\Client
62
     */
63
    protected function newClient($path)
64
    {
65
        $client = new \PhpXmlRpc\Client($this->baseUrl . $path);
66
        $client->setCookie('PHPUNIT_RANDOM_TEST_ID', static::$randId);
67
        if ($this->collectCodeCoverageInformation) {
68
            $client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
69
        }
70
        // let's just assume that the client works fine for these tests, and avoid polluting output, even in debug mode
71
        //$client->setOption(Client::OPT_ACCEPTED_COMPRESSION, false);
72
        //$client->setDebug($this->args['DEBUG']);
73
        return $client;
74
    }
75
}
76