Passed
Push — master ( e9b846...d641b4 )
by Gaetano
04:55
created

PhpXmlRpc_WebTestCase::request()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 16
nop 4
dl 0
loc 34
rs 9.2728
c 0
b 0
f 0
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(
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, 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

66
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array(
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, 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

79
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_COOKIE, 'PHPUNIT_SELENIUM_TEST_ID='.$this->testId);
Loading history...
80
        }
81
        if ($this->args['DEBUG'] > 0) {
82
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
83
        }
84
        $page = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

84
        $page = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
85
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

85
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
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 boolean; 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