merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / tests / WebTestCase.php
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) {
89             $this->assertNotEquals('', $page);
90         }
91         $this->assertStringNotContainsStringIgnoringCase('Fatal error', $page);
92         $this->assertStringNotContainsStringIgnoringCase('Notice:', $page);
93
94         return $page;
95     }
96 }