forked from Codeception/Codeception
/
LocalServer.php
282 lines (239 loc) · 8.94 KB
/
LocalServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Codeception\Coverage\Subscriber;
use Codeception\Configuration;
use Codeception\Coverage\SuiteSubscriber;
use Codeception\Event\StepEvent;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\ModuleException;
use Codeception\Exception\RemoteException;
/**
* When collecting code coverage data from local server HTTP requests are sent to c3.php file.
* Coverage Collection is started by sending cookies/headers.
* Result is taken from the local file and merged with local code coverage results.
*
* Class LocalServer
* @package Codeception\Coverage\Subscriber
*/
class LocalServer extends SuiteSubscriber
{
// headers
const COVERAGE_HEADER = 'X-Codeception-CodeCoverage';
const COVERAGE_HEADER_ERROR = 'X-Codeception-CodeCoverage-Error';
const COVERAGE_HEADER_CONFIG = 'X-Codeception-CodeCoverage-Config';
const COVERAGE_HEADER_SUITE = 'X-Codeception-CodeCoverage-Suite';
// cookie names
const COVERAGE_COOKIE = 'CODECEPTION_CODECOVERAGE';
const COVERAGE_COOKIE_ERROR = 'CODECEPTION_CODECOVERAGE_ERROR';
protected $suiteName;
protected $c3Access = [
'http' => [
'method' => "GET",
'header' => ''
]
];
/**
* @var \Codeception\Lib\Interfaces\Web
*/
protected $module;
public static $events = [
Events::SUITE_BEFORE => 'beforeSuite',
Events::TEST_BEFORE => 'beforeTest',
Events::STEP_AFTER => 'afterStep',
Events::SUITE_AFTER => 'afterSuite',
];
protected function isEnabled()
{
return $this->module && !$this->settings['remote'] && $this->settings['enabled'];
}
public function beforeSuite(SuiteEvent $e)
{
$this->module = $this->getServerConnectionModule($e->getSuite()->getModules());
$this->applySettings($e->getSettings());
if (!$this->isEnabled()) {
return;
}
$this->suiteName = $e->getSuite()->getBaseName();
if ($this->settings['remote_config']) {
$this->addC3AccessHeader(self::COVERAGE_HEADER_CONFIG, $this->settings['remote_config']);
$knock = $this->c3Request('clear');
if ($knock === false) {
throw new RemoteException(
'
CodeCoverage Error.
Check the file "c3.php" is included in your application.
We tried to access "/c3/report/clear" but this URI was not accessible.
You can review actual error messages in c3tmp dir.
'
);
}
}
}
public function beforeTest(TestEvent $e)
{
if (!$this->isEnabled()) {
return;
}
$this->startCoverageCollection($e->getTest()->getName());
}
public function afterStep(StepEvent $e)
{
if (!$this->isEnabled()) {
return;
}
$this->fetchErrors();
}
public function afterSuite(SuiteEvent $e)
{
if (!$this->isEnabled()) {
return;
}
$coverageFile = Configuration::outputDir() . 'c3tmp/codecoverage.serialized';
$retries = 5;
while (!file_exists($coverageFile) && --$retries >= 0) {
usleep(0.5 * 1000000); // 0.5 sec
}
if (!file_exists($coverageFile)) {
if (file_exists(Configuration::outputDir() . 'c3tmp/error.txt')) {
throw new \RuntimeException(file_get_contents(Configuration::outputDir() . 'c3tmp/error.txt'));
}
return;
}
$contents = file_get_contents($coverageFile);
$coverage = @unserialize($contents);
if ($coverage === false) {
return;
}
$this->preProcessCoverage($coverage)
->mergeToPrint($coverage);
}
/**
* Allows Translating Remote Paths To Local (IE: When Using Docker)
*
* @param \SebastianBergmann\CodeCoverage\CodeCoverage $coverage
* @return $this
*/
protected function preProcessCoverage($coverage)
{
//Only Process If Work Directory Set
if ($this->settings['work_dir'] === null) {
return $this;
}
$workDir = rtrim($this->settings['work_dir'], '/\\') . DIRECTORY_SEPARATOR;
$projectDir = Configuration::projectDir();
$data = $coverage->getData(true); //We only want covered files, not all whitelisted ones.
codecept_debug("Replacing all instances of {$workDir} with {$projectDir}");
foreach ($data as $path => $datum) {
unset($data[$path]);
$path = str_replace($workDir, $projectDir, $path);
$data[$path] = $datum;
}
$coverage->setData($data);
return $this;
}
protected function c3Request($action)
{
$this->addC3AccessHeader(self::COVERAGE_HEADER, 'remote-access');
$context = stream_context_create($this->c3Access);
$c3Url = $this->settings['c3_url'] ? $this->settings['c3_url'] : $this->module->_getUrl();
$contents = file_get_contents($c3Url . '/c3/report/' . $action, false, $context);
$okHeaders = array_filter(
$http_response_header,
function ($h) {
return preg_match('~^HTTP(.*?)\s200~', $h);
}
);
if (empty($okHeaders)) {
throw new RemoteException("Request was not successful. See response header: " . $http_response_header[0]);
}
if ($contents === false) {
$this->getRemoteError($http_response_header);
}
return $contents;
}
protected function startCoverageCollection($testName)
{
$value = [
'CodeCoverage' => $testName,
'CodeCoverage_Suite' => $this->suiteName,
'CodeCoverage_Config' => $this->settings['remote_config']
];
$value = json_encode($value);
if ($this->module instanceof \Codeception\Module\WebDriver) {
$this->module->amOnPage('/');
}
$cookieDomain = isset($this->settings['cookie_domain']) ? $this->settings['cookie_domain'] : null;
if (!$cookieDomain) {
$c3Url = parse_url($this->settings['c3_url'] ? $this->settings['c3_url'] : $this->module->_getUrl());
// we need to separate coverage cookies by host; we can't separate cookies by port.
$cookieDomain = isset($c3Url['host']) ? $c3Url['host'] : 'localhost';
}
$cookieParams = [];
if ($cookieDomain !== 'localhost') {
$cookieParams['domain'] = $cookieDomain;
}
$this->module->setCookie(self::COVERAGE_COOKIE, $value, $cookieParams);
// putting in configuration ensures the cookie is used for all sessions of a MultiSession test
$cookies = $this->module->_getConfig('cookies');
if (!$cookies || !is_array($cookies)) {
$cookies = [];
}
$found = false;
foreach ($cookies as &$cookie) {
if (!is_array($cookie) || !array_key_exists('Name', $cookie) || !array_key_exists('Value', $cookie)) {
// \Codeception\Lib\InnerBrowser will complain about this
continue;
}
if ($cookie['Name'] === self::COVERAGE_COOKIE) {
$found = true;
$cookie['Value'] = $value;
break;
}
}
unset($cookie);
if (!$found) {
$cookies[] = [
'Name' => self::COVERAGE_COOKIE,
'Value' => $value
];
}
$this->module->_setConfig(['cookies' => $cookies]);
}
protected function fetchErrors()
{
try {
$error = $this->module->grabCookie(self::COVERAGE_COOKIE_ERROR);
} catch (ModuleException $e) {
// when a new session is started we can't get cookies because there is no
// current page, but there can be no code coverage error either
$error = null;
}
if (!empty($error)) {
$this->module->resetCookie(self::COVERAGE_COOKIE_ERROR);
throw new RemoteException($error);
}
}
protected function getRemoteError($headers)
{
foreach ($headers as $header) {
if (strpos($header, self::COVERAGE_HEADER_ERROR) === 0) {
throw new RemoteException($header);
}
}
}
protected function addC3AccessHeader($header, $value)
{
$headerString = "$header: $value\r\n";
if (strpos($this->c3Access['http']['header'], $headerString) === false) {
$this->c3Access['http']['header'] .= $headerString;
}
}
protected function applySettings($settings)
{
parent::applySettings($settings);
if (isset($settings['coverage']['remote_context_options'])) {
$this->c3Access = array_replace_recursive($this->c3Access, $settings['coverage']['remote_context_options']);
}
}
}