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