From 0953ae431e00eba0fad1cb2641d011239c36b38c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 15 Sep 2022 14:10:25 +0100 Subject: [PATCH] Fixes ignorable tests --- src/Kernel.php | 70 ++++++++++++++++++++++++++++++++- src/TestCases/IgnorableTest.php | 10 +---- tests/.snapshots/success.txt | 5 +-- tests/PHPUnit/IgnorableTest.php | 2 + 4 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 tests/PHPUnit/IgnorableTest.php diff --git a/src/Kernel.php b/src/Kernel.php index 98bf867c..5e34b6a5 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -4,13 +4,24 @@ declare(strict_types=1); namespace Pest; +use PHPUnit\TestRunner\TestResult\Facade; +use PHPUnit\TestRunner\TestResult\TestResult; use PHPUnit\TextUI\Application; +use PHPUnit\TextUI\Configuration\Registry; /** * @internal */ final class Kernel { + private const SUCCESS_EXIT = 0; + + private const FAILURE_EXIT = 1; + + private const EXCEPTION_EXIT = 2; + + private const CRASH_EXIT = 255; + /** * The Kernel bootstrappers. * @@ -53,11 +64,15 @@ final class Kernel { $argv = (new Plugins\Actions\HandleArguments())->__invoke($argv); - $result = $this->application->run( + $this->application->run( $argv, false, ); - return (new Plugins\Actions\AddsOutput())->__invoke($result); + $returnCode = $this->returnCode(); + + return (new Plugins\Actions\AddsOutput())->__invoke( + $returnCode, + ); } /** @@ -67,4 +82,55 @@ final class Kernel { // .. } + + /** + * Returns the exit code, based on the facade's result. + */ + private function returnCode(): int + { + $result = Facade::result(); + + $returnCode = self::FAILURE_EXIT; + + if ($result->wasSuccessfulIgnoringPhpunitWarnings() + && ! $result->hasTestTriggeredPhpunitWarningEvents()) { + $returnCode = self::SUCCESS_EXIT; + } + + $configuration = Registry::get(); + + if ($configuration->failOnEmptyTestSuite() && $result->numberOfTests() === 0) { + $returnCode = self::FAILURE_EXIT; + } + + if ($result->wasSuccessfulIgnoringPhpunitWarnings()) { + if ($configuration->failOnRisky() && $result->hasTestConsideredRiskyEvents()) { + $returnCode = self::FAILURE_EXIT; + } + + $warnings = $result->numberOfTestsWithTestTriggeredPhpunitWarningEvents() + + $result->numberOfTestsWithTestTriggeredWarningEvents() + + $result->numberOfTestsWithTestTriggeredPhpWarningEvents(); + + if ($configuration->failOnWarning() && ! empty($warnings)) { + $returnCode = self::FAILURE_EXIT; + } + + if ($configuration->failOnIncomplete() && $result->hasTestMarkedIncompleteEvents()) { + $returnCode = self::FAILURE_EXIT; + } + + + if ($configuration->failOnSkipped() && $result->hasTestSkippedEvents()) { + $returnCode = self::FAILURE_EXIT; + } + + } + + if ($result->hasTestErroredEvents()) { + $returnCode = self::EXCEPTION_EXIT; + } + + return $returnCode; + } } diff --git a/src/TestCases/IgnorableTest.php b/src/TestCases/IgnorableTest.php index 39ea00ab..812e90d4 100644 --- a/src/TestCases/IgnorableTest.php +++ b/src/TestCases/IgnorableTest.php @@ -9,13 +9,7 @@ use PHPUnit\Framework\TestCase; /** * @internal */ -final class IgnorableTest extends TestCase +class IgnorableTest extends TestCase { - /** - * Creates a dummy assertion. - */ - public function testIgnorable(): void - { - self::assertTrue(true); - } + } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index abbb8046..27f5f62a 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -692,9 +692,6 @@ ✓ custom traits can be used ✓ trait applied in this file - WARN Tests\Playground - ! basic → This test did not perform any assertions - PASS Tests\Plugins\Traits ✓ it allows global uses ✓ it allows multiple global uses registered in the same path @@ -770,5 +767,5 @@ WARN Tests\Visual\TeamCity - it is can successfully call all public methods → Not supported yet. - Tests: 1 risky, 4 incompleted, 18 skipped, 514 passed + Tests: 4 incompleted, 18 skipped, 514 passed \ No newline at end of file diff --git a/tests/PHPUnit/IgnorableTest.php b/tests/PHPUnit/IgnorableTest.php new file mode 100644 index 00000000..a4abe2da --- /dev/null +++ b/tests/PHPUnit/IgnorableTest.php @@ -0,0 +1,2 @@ +