Issues (323)

tests/WebTestCase.php (1 issue)

1
<?php
2
3
include_once __DIR__ . '/ServerAwareTestCase.php';
4
5
use PhpXmlRpc\Client;
6
7
abstract class PhpXmlRpc_WebTestCase extends PhpXmlRpc_ServerAwareTestCase
8
{
9
    /**
10
     * Make an HTTP request, check that the result is a 200 OK page with no php fatal error or warning messages.
11
     *
12
     * @param string $path
13
     * @param string $method
14
     * @param string $payload
15
     * @param false $emptyPageOk
16
     * @return bool|string
17
     */
18
    protected function request($path, $method = 'GET', $payload = '', $emptyPageOk = false)
19
    {
20
        $url = $this->baseUrl . $path;
21
22
        $ch = curl_init($url);
23
        curl_setopt_array($ch, array(
24
            CURLOPT_RETURNTRANSFER => true,
25
            CURLOPT_FAILONERROR => true
26
        ));
27
        if ($method == 'POST')
28
        {
29
            curl_setopt_array($ch, array(
30
                CURLOPT_POST => true,
31
                CURLOPT_POSTFIELDS => $payload
32
            ));
33
        }
34
        $cookie = 'PHPUNIT_RANDOM_TEST_ID=' . static::$randId;
35
        if ($this->collectCodeCoverageInformation)
36
        {
37
            $cookie .= '; PHPUNIT_SELENIUM_TEST_ID=' . $this->testId;
38
        }
39
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
40
41
        if ($this->args['DEBUG'] > 0) {
42
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
43
        }
44
        $page = curl_exec($ch);
45
        $info = curl_getinfo($ch);
46
        if (PHP_MAJOR_VERSION < 8) curl_close($ch);
47
48
        $this->assertNotFalse($page, 'Curl request should not fail. Url: ' . @$info['url'] . ', Http code: ' . @$info['http_code']);
49
        if (!$emptyPageOk) {
0 ignored issues
show
The condition $emptyPageOk is always false.
Loading history...
50
            $this->assertNotEquals('', $page, 'Retrieved web page should not be empty');
51
        }
52
        $this->assertStringNotContainsStringIgnoringCase('Fatal error', $page, 'Retrieved web page should not contain a fatal error string');
53
        $this->assertStringNotContainsStringIgnoringCase('Notice:', $page, 'Retrieved web page should not contain a notice string');
54
55
        return $page;
56
    }
57
58
    /**
59
     * Build an xml-rpc client, tweaked if needed to collect code-coverage information of the server.
60
     * @see also ServerTest::set_up
61
     *
62
     * @param string $path
63
     * @return Client
64
     */
65
    protected function newClient($path)
66
    {
67
        $client = new Client($this->baseUrl . $path);
68
        $client->setCookie('PHPUNIT_RANDOM_TEST_ID', static::$randId);
69
        if ($this->collectCodeCoverageInformation) {
70
            $client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
71
        }
72
        // let's just assume that the client works fine for these tests, and avoid polluting output, even in debug mode
73
        //$client->setOption(Client::OPT_ACCEPTED_COMPRESSION, false);
74
        //$client->setDebug($this->args['DEBUG']);
75
        return $client;
76
    }
77
}
78