Skip to content

Commit

Permalink
results handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ShockedPlot7560 committed Dec 24, 2023
1 parent 72dba55 commit 45f1426
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 12 deletions.
23 changes: 20 additions & 3 deletions src/TestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ public function setup() : void {

$this->playerManager = new TestPlayerManager($this->pmmpUnit);
$this->playerBag = new PlayerBag();
$this->runner->onLoad();
try {
$this->runner->onLoad();
} catch (Throwable $e) {
TestResults::fatal($e);
$this->finish();
}
}

public function prepare() : void {
$this->runner->onEnable();
try {
$this->runner->onEnable();
} catch (Throwable $e) {
TestResults::fatal($e);
$this->finish();
}
}

public function start() : void {
Expand All @@ -71,7 +81,14 @@ public function start() : void {
}

public function stop() : void {
$this->runner->onDisable();
try {
$this->runner->onDisable();
} catch (Throwable $e) {
TestResults::fatal($e);

$this->finish(false);
return;
}
if (TestMemory::$currentTest !== null) {
global $lastExceptionError, $lastError;
$error = $lastExceptionError ?? $lastError;
Expand Down
11 changes: 10 additions & 1 deletion src/framework/result/FailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
use ShockedPlot7560\PmmpUnit\framework\runner\TestRunnerInterface;
use Throwable;

class FailedTest implements TestResult {
class FailedTest implements TestResult, ThrowableResult, \Stringable {
public function __construct(
public readonly TestRunnerInterface $test,
public readonly Throwable $throwable
) {
}

public function getThrowable(): Throwable {
return $this->throwable;
}

public function __toString(): string
{
return $this->test->__toString();
}
}
18 changes: 18 additions & 0 deletions src/framework/result/Fatal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ShockedPlot7560\PmmpUnit\framework\result;

class Fatal implements TestResult, ThrowableResult, \Stringable {
public function __construct(
public readonly \Throwable $throwable
) { }

public function getThrowable(): \Throwable {
return $this->throwable;
}

public function __toString(): string
{
return "Runtime exception";
}
}
10 changes: 8 additions & 2 deletions src/framework/result/FatalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
use ShockedPlot7560\PmmpUnit\framework\runner\TestRunnerInterface;
use Throwable;

class FatalTest implements TestResult {
class FatalTest extends Fatal {
public function __construct(
public readonly TestRunnerInterface $test,
public readonly Throwable $throwable
Throwable $throwable
) {
parent::__construct($throwable);
}

public function __toString(): string
{
return $this->test->__toString();
}
}
4 changes: 4 additions & 0 deletions src/framework/result/TestResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static function fatalTest(TestRunnerInterface $test, Throwable $throwable
self::$testResults[] = new FatalTest($test, $throwable);
}

public static function fatal(Throwable $throwable) : void {
self::$testResults[] = new Fatal($throwable);
}

/**
* @return TestResult[]
*/
Expand Down
7 changes: 7 additions & 0 deletions src/framework/result/ThrowableResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ShockedPlot7560\PmmpUnit\framework\result;

interface ThrowableResult {
public function getThrowable();
}
9 changes: 5 additions & 4 deletions src/framework/result/printer/TestResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Logger;
use ShockedPlot7560\PmmpUnit\framework\result\FailedTest;
use ShockedPlot7560\PmmpUnit\framework\result\FatalTest;
use ShockedPlot7560\PmmpUnit\framework\result\ThrowableResult;

class TestResultPrinter {
public function __construct(
Expand Down Expand Up @@ -59,10 +60,10 @@ private function printStats(Logger $logger) : void {
);
}

private function errorToString(FatalTest|FailedTest $error) : string {
$ret = $error->test->__toString() . ": ";
$ret .= str_replace("§", "&", $error->throwable->getMessage());
$ret .= " (line: " . $error->throwable->getLine() . ")";
private function errorToString(ThrowableResult&\Stringable $error) : string {
$ret = $error->__toString() . ": ";
$ret .= str_replace("§", "&", $error->getThrowable()->getMessage());
$ret .= " (line: " . $error->getThrowable()->getLine() . ")";

return $ret;
}
Expand Down
5 changes: 3 additions & 2 deletions src/framework/result/printer/TestResultsBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShockedPlot7560\PmmpUnit\framework\result\printer;

use ShockedPlot7560\PmmpUnit\framework\result\FailedTest;
use ShockedPlot7560\PmmpUnit\framework\result\Fatal;
use ShockedPlot7560\PmmpUnit\framework\result\FatalTest;
use ShockedPlot7560\PmmpUnit\framework\result\SuccessTest;
use ShockedPlot7560\PmmpUnit\framework\result\TestResult;
Expand All @@ -28,7 +29,7 @@ public function __construct(
$this->failedTests[] = $result;
} elseif ($result instanceof SuccessTest) {
$this->passedTests[] = $result;
} elseif ($result instanceof FatalTest) {
} elseif ($result instanceof Fatal) {
$this->fatalErrors[] = $result;
}
}
Expand Down Expand Up @@ -67,7 +68,7 @@ public function isFailed(TestResult $result) : bool {
}

public function isFatal(TestResult $result) : bool {
return $result instanceof FatalTest;
return $result instanceof Fatal;
}

public function isPassed(TestResult $result) : bool {
Expand Down

0 comments on commit 45f1426

Please sign in to comment.