feature #16189 [PhpUnitBridge] Add SkippedTestsListener to collect and replay skipped tests (nicolas-grekas)

This PR was merged into the 2.8 branch.

Discussion
----------

[PhpUnitBridge] Add SkippedTestsListener to collect and replay skipped tests

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16184
| License       | MIT
| Doc PR        | -

Commits
-------

dd640f5 [PhpUnitBridge] Add SkippedTestsListener to collect and replay skipped tests
This commit is contained in:
Fabien Potencier 2015-10-10 11:09:57 +02:00
commit 10389e353e
2 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,85 @@
<?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;
use Doctrine\Common\Annotations\AnnotationRegistry;
/**
* Collects and replays skipped tests.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class SkippedTestsListener extends \PHPUnit_Framework_BaseTestListener
{
private $state = -1;
private $skippedFile = false;
private $wasSkipped = array();
private $isSkipped = array();
public function __destruct()
{
if (0 < $this->state) {
file_put_contents($this->skippedFile, '<?php return '.var_export($this->isSkipped, true).';');
}
}
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
$suiteName = $suite->getName();
if (-1 === $this->state) {
echo "Testing $suiteName\n";
$this->state = 0;
if (!class_exists('Doctrine\Common\Annotations\AnnotationRegistry', false) && class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
AnnotationRegistry::registerLoader('class_exists');
}
if ($this->skippedFile = getenv('SYMFONY_PHPUNIT_SKIPPED_TESTS')) {
$this->state = 1;
if (file_exists($this->skippedFile)) {
$this->state = 2;
if (!$this->wasSkipped = require $this->skippedFile) {
exit("All tests already ran successfully.\n");
}
}
}
} elseif (2 === $this->state) {
$skipped = array();
foreach ($suite->tests() as $test) {
if (!$test instanceof \PHPUnit_Framework_TestCase
|| isset($this->wasSkipped[$suiteName]['*'])
|| isset($this->wasSkipped[$suiteName][$test->getName()])) {
$skipped[] = $test;
}
}
$suite->setTests($skipped);
}
}
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
{
if (0 < $this->state) {
if ($test instanceof \PHPUnit_Framework_TestCase) {
$class = get_class($test);
$method = $test->getName();
} else {
$class = $test->getName();
$method = '*';
}
$this->isSkipped[$class][$method] = 1;
}
}
}

View File

@ -26,7 +26,7 @@ if (PHP_VERSION_ID >= 50400 && gc_enabled()) {
// Enforce a consistent locale
setlocale(LC_ALL, 'C');
if (class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
if (!class_exists('Doctrine\Common\Annotations\AnnotationRegistry', false) && class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
AnnotationRegistry::registerLoader('class_exists');
}