|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the doyo/behat-code-coverage project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Doyo\Bridge\CodeCoverage\Session; |
|
15
|
|
|
|
|
16
|
|
|
use Doyo\Bridge\CodeCoverage\Exception\SessionException; |
|
17
|
|
|
use Doyo\Bridge\CodeCoverage\Processor; |
|
18
|
|
|
use Doyo\Bridge\CodeCoverage\ProcessorInterface; |
|
19
|
|
|
use Doyo\Bridge\CodeCoverage\TestCase; |
|
20
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
|
21
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
|
22
|
|
|
|
|
23
|
|
|
abstract class Session implements \Serializable, SessionInterface |
|
24
|
|
|
{ |
|
25
|
|
|
const CACHE_KEY = 'session'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var TestCase |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $testCase; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var FilesystemAdapter|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $adapter; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $data = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ProcessorInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $processor; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \Exception[] |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $exceptions = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $hasStarted = false; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Code coverage for this session. |
|
64
|
|
|
* |
|
65
|
|
|
* @var CodeCoverage |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $codeCoverage; |
|
68
|
|
|
|
|
69
|
|
|
protected $patchXdebug = true; |
|
70
|
|
|
|
|
71
|
21 |
|
public function __construct($name, $patchXdebug = true) |
|
72
|
|
|
{ |
|
73
|
21 |
|
$dir = sys_get_temp_dir().'/doyo/behat-coverage-extension'; |
|
74
|
21 |
|
$adapter = new FilesystemAdapter($name, 0, $dir); |
|
75
|
21 |
|
$this->adapter = $adapter; |
|
76
|
21 |
|
$this->name = $name; |
|
77
|
21 |
|
$this->patchXdebug = $patchXdebug; |
|
78
|
21 |
|
$this->refresh(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
5 |
|
public function reset() |
|
85
|
|
|
{ |
|
86
|
5 |
|
$this->testCase = null; |
|
87
|
5 |
|
$this->exceptions = []; |
|
88
|
5 |
|
$this->processor->clear(); |
|
89
|
|
|
|
|
90
|
5 |
|
$this->save(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
11 |
|
public function serialize() |
|
97
|
|
|
{ |
|
98
|
|
|
$data = [ |
|
99
|
11 |
|
$this->name, |
|
100
|
11 |
|
$this->testCase, |
|
101
|
11 |
|
$this->exceptions, |
|
102
|
11 |
|
$this->processor, |
|
103
|
11 |
|
$this->patchXdebug, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
11 |
|
return serialize($data); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
14 |
|
public function unserialize($serialized) |
|
113
|
|
|
{ |
|
114
|
|
|
list( |
|
115
|
14 |
|
$this->name, |
|
116
|
14 |
|
$this->testCase, |
|
117
|
14 |
|
$this->exceptions, |
|
118
|
14 |
|
$this->processor, |
|
119
|
14 |
|
$this->patchXdebug |
|
120
|
14 |
|
) = unserialize($serialized); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
23 |
|
public function refresh() |
|
127
|
|
|
{ |
|
128
|
23 |
|
$adapter = $this->adapter; |
|
129
|
23 |
|
$cached = $adapter->getItem(static::CACHE_KEY)->get(); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
23 |
|
if ($cached instanceof self) { |
|
132
|
13 |
|
$this->name = $cached->getName(); |
|
133
|
13 |
|
$this->testCase = $cached->getTestCase(); |
|
134
|
13 |
|
$this->exceptions = $cached->getExceptions(); |
|
135
|
13 |
|
$this->processor = $cached->getProcessor(); |
|
136
|
13 |
|
$this->patchXdebug = $cached->getPatchXdebug(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public function setPatchXdebug(bool $flag) |
|
141
|
|
|
{ |
|
142
|
1 |
|
$this->patchXdebug = $flag; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
14 |
|
public function getPatchXdebug(): bool |
|
149
|
|
|
{ |
|
150
|
14 |
|
return $this->patchXdebug; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string|null |
|
155
|
|
|
*/ |
|
156
|
15 |
|
public function getName() |
|
157
|
|
|
{ |
|
158
|
15 |
|
return $this->name; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return TestCase |
|
163
|
|
|
*/ |
|
164
|
15 |
|
public function getTestCase() |
|
165
|
|
|
{ |
|
166
|
15 |
|
return $this->testCase; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
9 |
|
public function setTestCase(TestCase $testCase = null) |
|
173
|
|
|
{ |
|
174
|
9 |
|
$this->testCase = $testCase; |
|
175
|
|
|
|
|
176
|
9 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return FilesystemAdapter|null |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function getAdapter(): FilesystemAdapter |
|
183
|
|
|
{ |
|
184
|
1 |
|
return $this->adapter; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param FilesystemAdapter|null $adapter |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public function setAdapter(FilesystemAdapter $adapter) |
|
191
|
|
|
{ |
|
192
|
1 |
|
$this->adapter = $adapter; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param ProcessorInterface $processor |
|
197
|
|
|
*/ |
|
198
|
19 |
|
public function setProcessor(ProcessorInterface $processor = null) |
|
199
|
|
|
{ |
|
200
|
19 |
|
$this->processor = $processor; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return ProcessorInterface|null |
|
205
|
|
|
*/ |
|
206
|
14 |
|
public function getProcessor() |
|
207
|
|
|
{ |
|
208
|
14 |
|
return $this->processor; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* {@inheritdoc} |
|
213
|
|
|
*/ |
|
214
|
4 |
|
public function hasExceptions() |
|
215
|
|
|
{ |
|
216
|
4 |
|
return \count($this->exceptions) > 0; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* {@inheritdoc} |
|
221
|
|
|
*/ |
|
222
|
14 |
|
public function getExceptions() |
|
223
|
|
|
{ |
|
224
|
14 |
|
return $this->exceptions; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param \Exception $e |
|
229
|
|
|
*/ |
|
230
|
3 |
|
public function addException(\Exception $e) |
|
231
|
|
|
{ |
|
232
|
3 |
|
$id = md5($e->getMessage()); |
|
233
|
3 |
|
$this->exceptions[$id] = $e; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @codeCoverageIgnore |
|
238
|
|
|
*/ |
|
239
|
5 |
|
public function xdebugPatch() |
|
240
|
|
|
{ |
|
241
|
5 |
|
if (!$this->patchXdebug) { |
|
242
|
|
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
5 |
|
$filter = $this->getProcessor()->getCodeCoverageFilter(); |
|
246
|
5 |
|
$options = $filter->getWhitelistedFiles(); |
|
247
|
5 |
|
$filterKey = 'whitelistedFiles'; |
|
248
|
|
|
|
|
249
|
|
|
if ( |
|
250
|
5 |
|
!\extension_loaded('xdebug') |
|
251
|
5 |
|
|| !\function_exists('xdebug_set_filter') |
|
252
|
5 |
|
|| !isset($options[$filterKey]) |
|
253
|
|
|
) { |
|
254
|
5 |
|
return; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$dirs = []; |
|
258
|
|
|
foreach ($options[$filterKey] as $fileName => $status) { |
|
259
|
|
|
$dir = \dirname($fileName); |
|
260
|
|
|
if (!\in_array($dir, $dirs, true)) { |
|
261
|
|
|
$dirs[] = $dir; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
xdebug_set_filter( |
|
266
|
|
|
XDEBUG_FILTER_CODE_COVERAGE, |
|
|
|
|
|
|
267
|
|
|
XDEBUG_PATH_WHITELIST, |
|
|
|
|
|
|
268
|
|
|
$dirs |
|
269
|
|
|
); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* {@inheritdoc} |
|
274
|
|
|
*/ |
|
275
|
7 |
|
final public function start($driver = null) |
|
276
|
|
|
{ |
|
277
|
7 |
|
if (null === $this->testCase) { |
|
278
|
1 |
|
return; |
|
279
|
|
|
} |
|
280
|
|
|
try { |
|
281
|
6 |
|
$this->hasStarted = false; |
|
282
|
6 |
|
$this->codeCoverage = $this->createCodeCoverage($driver); |
|
283
|
5 |
|
$this->xdebugPatch(); |
|
284
|
5 |
|
$this->codeCoverage->start($this->testCase->getName()); |
|
285
|
6 |
|
$this->hasStarted = true; |
|
286
|
2 |
|
} catch (\Exception $e) { |
|
287
|
2 |
|
$message = sprintf( |
|
288
|
2 |
|
"Can not start code coverage with error message:\n%s", |
|
289
|
2 |
|
$e->getMessage() |
|
290
|
|
|
); |
|
291
|
2 |
|
$exception = new SessionException($message); |
|
292
|
2 |
|
throw $exception; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
4 |
|
public function stop() |
|
297
|
|
|
{ |
|
298
|
|
|
try { |
|
299
|
4 |
|
$codeCoverage = $this->codeCoverage; |
|
300
|
4 |
|
$processor = $this->processor; |
|
301
|
|
|
|
|
302
|
4 |
|
$codeCoverage->stop(); |
|
303
|
|
|
|
|
304
|
2 |
|
$processor->merge($codeCoverage); |
|
305
|
3 |
|
} catch (\Exception $e) { |
|
306
|
3 |
|
throw new SessionException( |
|
307
|
3 |
|
sprintf( |
|
308
|
3 |
|
'Failed to stop coverage for session %s: %s', |
|
309
|
3 |
|
$this->name, |
|
310
|
3 |
|
$e->getMessage() |
|
311
|
|
|
) |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
1 |
|
$this->hasStarted = false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* {@inheritdoc} |
|
319
|
|
|
*/ |
|
320
|
9 |
|
public function save() |
|
321
|
|
|
{ |
|
322
|
9 |
|
$adapter = $this->adapter; |
|
323
|
9 |
|
$item = $adapter->getItem(static::CACHE_KEY); |
|
324
|
|
|
|
|
325
|
9 |
|
$item->set($this); |
|
326
|
9 |
|
$adapter->save($item); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
2 |
|
public function shutdown() |
|
330
|
|
|
{ |
|
331
|
2 |
|
if ($this->hasStarted && null !== $this->processor) { |
|
332
|
|
|
try { |
|
333
|
2 |
|
$this->stop(); |
|
334
|
2 |
|
} catch (\Exception $e) { |
|
335
|
2 |
|
$this->addException(new SessionException($e->getMessage())); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
2 |
|
$this->hasStarted = false; |
|
339
|
2 |
|
$this->save(); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @param mixed|null $driver |
|
344
|
|
|
* |
|
345
|
|
|
* @return CodeCoverage |
|
346
|
|
|
*/ |
|
347
|
6 |
|
protected function createCodeCoverage($driver): CodeCoverage |
|
348
|
|
|
{ |
|
349
|
6 |
|
$filter = $this->processor->getCodeCoverageFilter(); |
|
350
|
5 |
|
$options = $this->processor->getCodeCoverageOptions(); |
|
351
|
5 |
|
$coverage = new CodeCoverage($driver, $filter); |
|
352
|
5 |
|
foreach ($options as $method => $option) { |
|
353
|
1 |
|
if (method_exists($coverage, $method)) { |
|
354
|
1 |
|
$method = 'set'.ucfirst($method); |
|
355
|
1 |
|
\call_user_func_array([$coverage, $method], [$option]); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
5 |
|
return $coverage; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.