Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 652c5ef

Browse filesBrowse files
committed
[PhpUnitBridge] Added a CoverageListener to enhance the code coverage report
1 parent ddc4b20 commit 652c5ef
Copy full SHA for 652c5ef

File tree

3 files changed

+136
-0
lines changed
Filter options

3 files changed

+136
-0
lines changed
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bridge\PhpUnit;
13+
14+
use PHPUnit\Framework\BaseTestListener;
15+
use PHPUnit\Framework\Test;
16+
use Symfony\Bridge\PhpUnit\Legacy\CoverageListenerTrait;
17+
18+
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
19+
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListener', 'Symfony\Bridge\PhpUnit\CoverageListener');
20+
// Using an early return instead of a else does not work when using the PHPUnit
21+
// phar due to some weird PHP behavior (the class gets defined without executing
22+
// the code before it and so the definition is not properly conditional)
23+
} else {
24+
/**
25+
* CoverageListener adds `@covers <className>` on each test suite when possible
26+
* to make the code coverage more accurate.
27+
*
28+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
29+
*/
30+
class CoverageListener extends BaseTestListener
31+
{
32+
public function __construct()
33+
{
34+
$this->trait = new CoverageListenerTrait();
35+
}
36+
37+
public function startTest(Test $test)
38+
{
39+
$this->trait->startTest($test);
40+
}
41+
}
42+
}
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
/**
15+
* CoverageListener adds `@covers <className>` on each test suite when possible
16+
* to make the code coverage more accurate.
17+
*
18+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
19+
*
20+
* @internal
21+
*/
22+
class CoverageListener extends \PHPUnit_Framework_BaseTestListener
23+
{
24+
public function __construct()
25+
{
26+
$this->trait = new CoverageListenerTrait();
27+
}
28+
29+
public function startTest(\PHPUnit_Framework_Test $test)
30+
{
31+
$this->trait->startTest($test);
32+
}
33+
}
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
use PHPUnit\Framework\BaseTestListener;
15+
use PHPUnit\Framework\Test;
16+
17+
/**
18+
* PHP 5.3 compatible trait-like shared implementation.
19+
*
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
*
22+
* @internal
23+
*/
24+
class CoverageListenerTrait
25+
{
26+
public function startTest(Test $test)
27+
{
28+
$annotations = $test->getAnnotations();
29+
30+
if (isset($annotations['class']['covers']) || isset($annotations['method']['covers'])) {
31+
return;
32+
}
33+
34+
$class = get_class($test);
35+
36+
$coverClass = str_replace('Tests\\', '', $class);
37+
$coverClass = preg_replace('{Test$}', '', $coverClass);
38+
39+
if (!class_exists($coverClass)) {
40+
return;
41+
}
42+
43+
if (true) {
44+
$testClass = \PHPUnit\Util\Test::class;
45+
} else {
46+
// FIXME
47+
$testClass = \PHPUnit_Framework_Test::class;
48+
}
49+
50+
$r = new \ReflectionProperty($testClass, 'annotationCache');
51+
$r->setAccessible(true);
52+
53+
$cache = $r->getValue();
54+
$cache = array_replace_recursive($cache, array(
55+
$class => array(
56+
'covers' => array($coverClass),
57+
),
58+
));
59+
$r->setValue($testClass, $cache);
60+
}
61+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.