diff --git a/src/Framework/Constraint/IsEqual.php b/src/Framework/Constraint/IsEqual.php index d3a00a0fde6..930fd74ece7 100644 --- a/src/Framework/Constraint/IsEqual.php +++ b/src/Framework/Constraint/IsEqual.php @@ -117,7 +117,7 @@ public function evaluate($other, $description = '', $returnResult = false) /** * Returns a string representation of the constraint. * - * @throws SebastianBergmann\RecursionContext\InvalidArgumentException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function toString(): string { diff --git a/src/Framework/Constraint/IsIdentical.php b/src/Framework/Constraint/IsIdentical.php index 7975966b382..c4362f00887 100644 --- a/src/Framework/Constraint/IsIdentical.php +++ b/src/Framework/Constraint/IsIdentical.php @@ -57,7 +57,7 @@ public function __construct($value) * @param bool $returnResult Whether to return a result or throw an exception * * @throws ExpectationFailedException - * @throws SebastianBergmann\RecursionContext\InvalidArgumentException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function evaluate($other, $description = '', $returnResult = false) { @@ -103,7 +103,7 @@ public function evaluate($other, $description = '', $returnResult = false) /** * Returns a string representation of the constraint. * - * @throws SebastianBergmann\RecursionContext\InvalidArgumentException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function toString(): string { @@ -123,7 +123,7 @@ public function toString(): string * * @param mixed $other evaluated value or object * - * @throws SebastianBergmann\RecursionContext\InvalidArgumentException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ protected function failureDescription($other): string { diff --git a/src/Runner/TestSuiteSorter.php b/src/Runner/TestSuiteSorter.php index c19c0e88591..7785701ca89 100644 --- a/src/Runner/TestSuiteSorter.php +++ b/src/Runner/TestSuiteSorter.php @@ -203,6 +203,10 @@ function ($left, $right) { */ private function cmpDefectPriorityAndTime(Test $a, Test $b): int { + if (!$a instanceof TestCase || !$b instanceof TestCase) { + return 0; + } + $priorityA = $this->defectSortOrder[$a->getName()] ?? 0; $priorityB = $this->defectSortOrder[$b->getName()] ?? 0; @@ -224,6 +228,10 @@ private function cmpDefectPriorityAndTime(Test $a, Test $b): int */ private function cmpDuration(Test $a, Test $b): int { + if (!$a instanceof TestCase || !$b instanceof TestCase) { + return 0; + } + return $this->cache->getTime($a->getName()) <=> $this->cache->getTime($b->getName()); } diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php index 6aa2b9fe119..ebcdb976811 100644 --- a/src/TextUI/TestRunner.php +++ b/src/TextUI/TestRunner.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\Error\Warning; use PHPUnit\Framework\Exception; use PHPUnit\Framework\Test; +use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestListener; use PHPUnit\Framework\TestResult; use PHPUnit\Framework\TestSuite; @@ -161,16 +162,18 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te $GLOBALS['__PHPUNIT_BOOTSTRAP'] = $arguments['bootstrap']; } - if ($arguments['backupGlobals'] === true) { - $suite->setBackupGlobals(true); - } + if ($suite instanceof TestCase || $suite instanceof TestSuite) { + if ($arguments['backupGlobals'] === true) { + $suite->setBackupGlobals(true); + } - if ($arguments['backupStaticAttributes'] === true) { - $suite->setBackupStaticAttributes(true); - } + if ($arguments['backupStaticAttributes'] === true) { + $suite->setBackupStaticAttributes(true); + } - if ($arguments['beStrictAboutChangesToGlobalState'] === true) { - $suite->setBeStrictAboutChangesToGlobalState(true); + if ($arguments['beStrictAboutChangesToGlobalState'] === true) { + $suite->setBeStrictAboutChangesToGlobalState(true); + } } if ($arguments['executionOrder'] === TestSuiteSorter::ORDER_RANDOMIZED) { diff --git a/src/Util/Log/JUnit.php b/src/Util/Log/JUnit.php index 8154af3e7a3..f81402e4ad4 100644 --- a/src/Util/Log/JUnit.php +++ b/src/Util/Log/JUnit.php @@ -287,24 +287,27 @@ public function endTestSuite(TestSuite $suite): void * A test started. * * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + * @throws ReflectionException */ public function startTest(Test $test): void { + if (!$test instanceof TestCase) { + return; + } + $testCase = $this->document->createElement('testcase'); $testCase->setAttribute('name', $test->getName()); - if ($test instanceof TestCase) { - $class = new ReflectionClass($test); - $methodName = $test->getName(!$test->usesDataProvider()); + $class = new ReflectionClass($test); + $methodName = $test->getName(!$test->usesDataProvider()); - if ($class->hasMethod($methodName)) { - $method = $class->getMethod($methodName); + if ($class->hasMethod($methodName)) { + $method = $class->getMethod($methodName); - $testCase->setAttribute('class', $class->getName()); - $testCase->setAttribute('classname', \str_replace('\\', '.', $class->getName())); - $testCase->setAttribute('file', $class->getFileName()); - $testCase->setAttribute('line', $method->getStartLine()); - } + $testCase->setAttribute('class', $class->getName()); + $testCase->setAttribute('classname', \str_replace('\\', '.', $class->getName())); + $testCase->setAttribute('file', $class->getFileName()); + $testCase->setAttribute('line', $method->getStartLine()); } $this->currentTestCase = $testCase; @@ -315,16 +318,18 @@ public function startTest(Test $test): void */ public function endTest(Test $test, float $time): void { - if ($test instanceof TestCase) { - $numAssertions = $test->getNumAssertions(); - $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions; - - $this->currentTestCase->setAttribute( - 'assertions', - $numAssertions - ); + if (!$test instanceof TestCase) { + return; } + $numAssertions = $test->getNumAssertions(); + $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions; + + $this->currentTestCase->setAttribute( + 'assertions', + $numAssertions + ); + $this->currentTestCase->setAttribute( 'time', \sprintf('%F', $time) @@ -337,7 +342,7 @@ public function endTest(Test $test, float $time): void $this->testSuiteTests[$this->testSuiteLevel]++; $this->testSuiteTimes[$this->testSuiteLevel] += $time; - if (\method_exists($test, 'hasOutput') && $test->hasOutput()) { + if ($test->hasOutput()) { $systemOut = $this->document->createElement( 'system-out', Xml::prepareString($test->getActualOutput()) diff --git a/src/Util/Log/TeamCity.php b/src/Util/Log/TeamCity.php index 4e8f446c4d4..a6c27b8a980 100644 --- a/src/Util/Log/TeamCity.php +++ b/src/Util/Log/TeamCity.php @@ -57,6 +57,10 @@ public function printResult(TestResult $result): void */ public function addError(Test $test, \Throwable $t, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $this->printEvent( 'testFailed', [ @@ -75,6 +79,10 @@ public function addError(Test $test, \Throwable $t, float $time): void */ public function addWarning(Test $test, Warning $e, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $this->printEvent( 'testFailed', [ @@ -93,6 +101,10 @@ public function addWarning(Test $test, Warning $e, float $time): void */ public function addFailure(Test $test, AssertionFailedError $e, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $parameters = [ 'name' => $test->getName(), 'message' => self::getMessage($e), @@ -132,6 +144,10 @@ public function addFailure(Test $test, AssertionFailedError $e, float $time): vo */ public function addIncompleteTest(Test $test, \Throwable $t, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $this->printIgnoredTest($test->getName(), $t, $time); } @@ -142,6 +158,10 @@ public function addIncompleteTest(Test $test, \Throwable $t, float $time): void */ public function addRiskyTest(Test $test, \Throwable $t, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $this->addError($test, $t, $time); } @@ -152,6 +172,10 @@ public function addRiskyTest(Test $test, \Throwable $t, float $time): void */ public function addSkippedTest(Test $test, \Throwable $t, float $time): void { + if (!$test instanceof TestCase) { + return; + } + $testName = $test->getName(); if ($this->startedTestName !== $testName) { @@ -253,6 +277,10 @@ public function endTestSuite(TestSuite $suite): void */ public function startTest(Test $test): void { + if (!$test instanceof TestCase) { + return; + } + $testName = $test->getName(); $this->startedTestName = $testName; $params = ['name' => $testName]; @@ -271,6 +299,10 @@ public function startTest(Test $test): void */ public function endTest(Test $test, float $time): void { + if (!$test instanceof TestCase) { + return; + } + parent::endTest($test, $time); $this->printEvent(