From 846d913f1a037ef9f14632fcf2fff180ecd3889d Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 28 Apr 2023 22:48:52 +0200 Subject: [PATCH 1/3] Add regression test --- tests/Unit/Logs/EqualsToken.php | 45 ++++++++++++++++++++++++++++++ tests/Unit/Logs/LogHandlerTest.php | 43 ++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 tests/Unit/Logs/EqualsToken.php diff --git a/tests/Unit/Logs/EqualsToken.php b/tests/Unit/Logs/EqualsToken.php new file mode 100644 index 00000000..a30ab862 --- /dev/null +++ b/tests/Unit/Logs/EqualsToken.php @@ -0,0 +1,45 @@ +util = $util ?? new StringUtil(); + } + + /** + * Scores 11 if argument matches preset value. + */ + public function scoreArgument($argument): bool|int + { + return $argument == $this->value ? 11 : false; + } + + public function isLast(): bool + { + return false; + } + + /** + * Returns string representation for token. + */ + public function __toString(): string + { + if (! isset($this->string)) { + $this->string = sprintf('equals(%s)', $this->util->stringify($this->value)); + } + + return $this->string; + } +} diff --git a/tests/Unit/Logs/LogHandlerTest.php b/tests/Unit/Logs/LogHandlerTest.php index 193a759a..e75cc8fe 100644 --- a/tests/Unit/Logs/LogHandlerTest.php +++ b/tests/Unit/Logs/LogHandlerTest.php @@ -9,9 +9,9 @@ use Paraunit\Logs\ValueObject\LogStatus; use Paraunit\Logs\ValueObject\Test; use Paraunit\TestResult\TestResultContainer; +use Paraunit\TestResult\ValueObject\TestIssue; use Paraunit\TestResult\ValueObject\TestOutcome; use Paraunit\TestResult\ValueObject\TestResult; -use Prophecy\Argument; use Psr\EventDispatcher\EventDispatcherInterface; use Tests\BaseUnitTestCase; use Tests\Stub\StubbedParaunitProcess; @@ -23,20 +23,45 @@ public function testParseHandlesNoTestExecuted(): void $process = new StubbedParaunitProcess(); $test = new Test($process->filename); - $testResultContainer = $this->prophesize(TestResultContainer::class); - $testResultContainer->addTestResult(Argument::that(function (TestResult $testResult) use ($test): bool { - $this->assertEquals(TestOutcome::NoTestExecuted, $testResult->status); - $this->assertEquals($test, $testResult->test); + $logHandler = new LogHandler( + $this->prophesize(EventDispatcherInterface::class)->reveal(), + $this->mockTestResultContainer($test, [TestOutcome::NoTestExecuted]), + ); - return true; - })); + $logHandler->processLog($process, new LogData(LogStatus::Started, $test, '0')); + $logHandler->processLog($process, new LogData(LogStatus::LogTerminated, $test, '')); + } + + public function testRegressionDoubleOutcomeWithSecondMoreImportant(): void + { + $process = new StubbedParaunitProcess(); + $test = new Test($process->filename); $logHandler = new LogHandler( $this->prophesize(EventDispatcherInterface::class)->reveal(), - $testResultContainer->reveal(), + $this->mockTestResultContainer($test, [TestOutcome::Passed, TestIssue::Deprecation]) ); - $logHandler->processLog($process, new LogData(LogStatus::Started, $test, '0')); + $logHandler->processLog($process, new LogData(LogStatus::Started, $test, '1')); + $logHandler->processLog($process, new LogData(LogStatus::Prepared, $test, '')); + $logHandler->processLog($process, new LogData(LogStatus::Passed, $test, '')); + $logHandler->processLog($process, new LogData(LogStatus::Deprecation, $test, '')); + $logHandler->processLog($process, new LogData(LogStatus::Finished, $test, '')); $logHandler->processLog($process, new LogData(LogStatus::LogTerminated, $test, '')); } + + /** + * @param array $expectedStatuses + */ + private function mockTestResultContainer(Test $test, array $expectedStatuses): TestResultContainer + { + $testResultContainer = $this->prophesize(TestResultContainer::class); + + foreach ($expectedStatuses as $expectedStatus) { + $testResultContainer->addTestResult(new EqualsToken(new TestResult($test, $expectedStatus))) + ->shouldBeCalledOnce(); + } + + return $testResultContainer->reveal(); + } } From a82f3ea2711ba5350dbffbbe051d0d38b3fc1cc5 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 28 Apr 2023 22:55:26 +0200 Subject: [PATCH 2/3] Fix wrong handling of log termination when a second outcome is emitted --- src/Logs/JSON/LogHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Logs/JSON/LogHandler.php b/src/Logs/JSON/LogHandler.php index a2feaa77..9916807f 100644 --- a/src/Logs/JSON/LogHandler.php +++ b/src/Logs/JSON/LogHandler.php @@ -75,7 +75,7 @@ public function processLog(Process $process, LogData $log): void } if ($log->status === LogStatus::LogTerminated) { - $this->handleLogEnding($process, $log); + $this->handleLogEnding($process); return; } @@ -88,14 +88,14 @@ public function processLog(Process $process, LogData $log): void } } - private function handleLogEnding(Process $process, LogData $log): void + private function handleLogEnding(Process $process): void { if ($process->getExitCode() === 0 && $this->actuallyPreparedTestCount === 0) { $this->testResultContainer->addTestResult(new TestResult($this->currentTest, TestOutcome::NoTestExecuted)); } if ($this->currentTestOutcome !== null) { - $this->testResultContainer->addTestResult(TestResult::from($log)); + $this->testResultContainer->addTestResult(new TestResult($this->currentTest, $this->currentTestOutcome)); } if ( From 09154a6dd895fe6d645d7eaeddca257d41b1744c Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 28 Apr 2023 22:58:09 +0200 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dece397..2cb82262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased * ... +## [2.0.1] - 2023-04-28 +## Fixed + * Fix handling of second outcome on last test of class (i.e. deprecation emitted after the last test has passed) [#204](https://github.com/facile-it/paraunit/pull/204) + ## [2.0.0] - 2023-03-06 ### Added * Support for PHPUnit 10