-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
CoverageListenerTrait.php
113 lines (91 loc) · 3.02 KB
/
CoverageListenerTrait.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\Legacy;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\Warning;
/**
* PHP 5.3 compatible trait-like shared implementation.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*
* @internal
*/
class CoverageListenerTrait
{
private $sutFqcnResolver;
private $warningOnSutNotFound;
private $warnings;
public function __construct(callable $sutFqcnResolver = null, $warningOnSutNotFound = false)
{
$this->sutFqcnResolver = $sutFqcnResolver;
$this->warningOnSutNotFound = $warningOnSutNotFound;
$this->warnings = array();
}
public function startTest($test)
{
$annotations = $test->getAnnotations();
$ignoredAnnotations = array('covers', 'coversDefaultClass', 'coversNothing');
foreach ($ignoredAnnotations as $annotation) {
if (isset($annotations['class'][$annotation]) || isset($annotations['method'][$annotation])) {
return;
}
}
$sutFqcn = $this->findSutFqcn($test);
if (!$sutFqcn) {
if ($this->warningOnSutNotFound) {
$message = 'Could not find the tested class.';
// addWarning does not exist on old PHPUnit version
if (method_exists($test->getTestResultObject(), 'addWarning') && class_exists(Warning::class)) {
$test->getTestResultObject()->addWarning($test, new Warning($message), 0);
} else {
$this->warnings[] = sprintf("%s::%s\n%s", get_class($test), $test->getName(), $message);
}
}
return;
}
$testClass = \PHPUnit\Util\Test::class;
if (!class_exists($testClass, false)) {
$testClass = \PHPUnit_Util_Test::class;
}
$r = new \ReflectionProperty($testClass, 'annotationCache');
$r->setAccessible(true);
$cache = $r->getValue();
$cache = array_replace_recursive($cache, array(
get_class($test) => array(
'covers' => array($sutFqcn),
),
));
$r->setValue($testClass, $cache);
}
private function findSutFqcn($test)
{
if ($this->sutFqcnResolver) {
$resolver = $this->sutFqcnResolver;
return $resolver($test);
}
$class = get_class($test);
$sutFqcn = str_replace('\\Tests\\', '\\', $class);
$sutFqcn = preg_replace('{Test$}', '', $sutFqcn);
if (!class_exists($sutFqcn)) {
return;
}
return $sutFqcn;
}
public function __destruct()
{
if (!$this->warnings) {
return;
}
echo "\n";
foreach ($this->warnings as $key => $warning) {
echo sprintf("%d) %s\n", ++$key, $warning);
}
}
}