Passed
Push — master ( c803c8...b166f8 )
by Gaetano
05:21
created

PhpXmlRpc_WebTestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 34 5
A _run() 0 26 3
A getClient() 0 9 2
1
<?php
2
3
include_once __DIR__ . '/parse_args.php';
4
5
include_once __DIR__ . '/PolyfillTestCase.php';
6
7
use PHPUnit\Extensions\SeleniumCommon\RemoteCoverage;
8
9
abstract class PhpXmlRpc_WebTestCase extends PhpXmlRpc_PolyfillTestCase
10
{
11
    public $args = array();
12
13
    protected $baseUrl;
14
15
    protected $testId;
16
    /** @var boolean $collectCodeCoverageInformation */
17
    protected $collectCodeCoverageInformation;
18
    protected $coverageScriptUrl;
19
20
    /**
21
     * Reimplemented to allow us to collect code coverage info for the target php files executed via an http request.
22
     * Code taken from PHPUnit_Extensions_Selenium2TestCase
23
     *
24
     * @todo instead of overriding run via _run, try to achieve this by implementing Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation
25
     */
26
    public function _run($result = NULL)
27
    {
28
        $this->testId = get_class($this) . '__' . $this->getName();
29
30
        if ($result === NULL) {
31
            $result = $this->createResult();
32
        }
33
34
        $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
35
36
        parent::_run($result);
37
38
        if ($this->collectCodeCoverageInformation) {
39
            $coverage = new RemoteCoverage(
40
                $this->coverageScriptUrl,
41
                $this->testId
42
            );
43
            $result->getCodeCoverage()->append(
44
                $coverage->get(), $this
45
            );
46
        }
47
48
        // do not call this before to give the time to the Listeners to run
49
        //$this->getStrategy()->endOfTest($this->session);
50
51
        return $result;
52
    }
53
54
    /**
55
     * @param string $path
56
     * @param string $method
57
     * @param string $payload
58
     * @param false $emptyPageOk
59
     * @return bool|string
60
     */
61
    protected function request($path, $method = 'GET', $payload = '', $emptyPageOk = false)
62
    {
63
        $url = $this->baseUrl . $path;
64
65
        $ch = curl_init($url);
66
        curl_setopt_array($ch, array(
67
            CURLOPT_RETURNTRANSFER => true,
68
            CURLOPT_FAILONERROR => true
69
        ));
70
        if ($method == 'POST')
71
        {
72
            curl_setopt_array($ch, array(
73
                CURLOPT_POST => true,
74
                CURLOPT_POSTFIELDS => $payload
75
            ));
76
        }
77
        if ($this->collectCodeCoverageInformation)
78
        {
79
            curl_setopt($ch, CURLOPT_COOKIE, 'PHPUNIT_SELENIUM_TEST_ID='.$this->testId);
80
        }
81
        if ($this->args['DEBUG'] > 0) {
82
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
83
        }
84
        $page = curl_exec($ch);
85
        curl_close($ch);
86
87
        $this->assertNotFalse($page);
88
        if (!$emptyPageOk) {
0 ignored issues
show
introduced by
The condition $emptyPageOk is always false.
Loading history...
89
            $this->assertNotEquals('', $page);
90
        }
91
        $this->assertStringNotContainsStringIgnoringCase('Fatal error', $page);
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

91
        $this->assertStringNotContainsStringIgnoringCase('Fatal error', /** @scrutinizer ignore-type */ $page);
Loading history...
92
        $this->assertStringNotContainsStringIgnoringCase('Notice:', $page);
93
94
        return $page;
95
    }
96
97
    protected function getClient($path)
98
    {
99
        $client = new xmlrpc_client($this->baseUrl . $path);
100
        if ($this->collectCodeCoverageInformation) {
101
            $client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
102
        }
103
        $client->setAcceptedCompression(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $compMethod of PhpXmlRpc\Client::setAcceptedCompression(). ( Ignorable by Annotation )

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

103
        $client->setAcceptedCompression(/** @scrutinizer ignore-type */ false);
Loading history...
104
        $client->setDebug($this->args['DEBUG']);
105
        return $client;
106
    }
107
}
108