Skip to content

Commit

Permalink
Merge pull request #204 from facile-it/fix-bad-result-handling
Browse files Browse the repository at this point in the history
Fix bad result handling
  • Loading branch information
Jean85 authored Apr 28, 2023
2 parents 7bf83da + 09154a6 commit 10b6965
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Logs/JSON/LogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 (
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Logs/EqualsToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Logs;

use Prophecy\Argument\Token\TokenInterface;
use Prophecy\Util\StringUtil;

class EqualsToken implements TokenInterface, \Stringable
{
private readonly StringUtil $util;

private string $string;

public function __construct(private readonly mixed $value, StringUtil $util = null)
{
$this->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;
}
}
43 changes: 34 additions & 9 deletions tests/Unit/Logs/LogHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TestOutcome|TestIssue> $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();
}
}

0 comments on commit 10b6965

Please sign in to comment.