From dd640f52f76401ef30fd238ace6387b97052ef66 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 9 Oct 2015 18:17:11 +0200 Subject: [PATCH] [PhpUnitBridge] Add SkippedTestsListener to collect and replay skipped tests --- .../Bridge/PhpUnit/SkippedTestsListener.php | 85 +++++++++++++++++++ src/Symfony/Bridge/PhpUnit/bootstrap.php | 2 +- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bridge/PhpUnit/SkippedTestsListener.php diff --git a/src/Symfony/Bridge/PhpUnit/SkippedTestsListener.php b/src/Symfony/Bridge/PhpUnit/SkippedTestsListener.php new file mode 100644 index 0000000000..f95563ce3b --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/SkippedTestsListener.php @@ -0,0 +1,85 @@ + + * + * 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 + */ +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, '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; + } + } +} diff --git a/src/Symfony/Bridge/PhpUnit/bootstrap.php b/src/Symfony/Bridge/PhpUnit/bootstrap.php index e5b1a1a2c4..0a43d98bab 100644 --- a/src/Symfony/Bridge/PhpUnit/bootstrap.php +++ b/src/Symfony/Bridge/PhpUnit/bootstrap.php @@ -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'); }