diff --git a/src/TestProcessor.php b/src/TestProcessor.php index 16431c6..fe332c2 100644 --- a/src/TestProcessor.php +++ b/src/TestProcessor.php @@ -52,21 +52,21 @@ public function setup() : void { $this->playerManager = new TestPlayerManager($this->pmmpUnit); $this->playerBag = new PlayerBag(); - try { - $this->runner->onLoad(); - } catch (Throwable $e) { - TestResults::fatal($e); - $this->finish(); - } + try { + $this->runner->onLoad(); + } catch (Throwable $e) { + TestResults::fatal($e); + $this->finish(); + } } public function prepare() : void { - try { - $this->runner->onEnable(); - } catch (Throwable $e) { - TestResults::fatal($e); - $this->finish(); - } + try { + $this->runner->onEnable(); + } catch (Throwable $e) { + TestResults::fatal($e); + $this->finish(); + } } public function start() : void { @@ -81,14 +81,15 @@ public function start() : void { } public function stop() : void { - try { - $this->runner->onDisable(); - } catch (Throwable $e) { - TestResults::fatal($e); - - $this->finish(false); - return; - } + try { + $this->runner->onDisable(); + } catch (Throwable $e) { + TestResults::fatal($e); + + $this->finish(false); + + return; + } if (TestMemory::$currentTest !== null) { global $lastExceptionError, $lastError; $error = $lastExceptionError ?? $lastError; diff --git a/src/framework/attribute/dataProvider/DataProviderAttribute.php b/src/framework/attribute/DataProviderAttribute.php similarity index 79% rename from src/framework/attribute/dataProvider/DataProviderAttribute.php rename to src/framework/attribute/DataProviderAttribute.php index 966091f..bfb74c6 100644 --- a/src/framework/attribute/dataProvider/DataProviderAttribute.php +++ b/src/framework/attribute/DataProviderAttribute.php @@ -1,11 +1,10 @@ $class + */ public function getRunner(ReflectionClass $class, ReflectionMethod $method) : TestRunnerInterface; } diff --git a/src/framework/attribute/dataProvider/DataProviderTest.php b/src/framework/attribute/dataProvider/DataProviderTest.php deleted file mode 100644 index 408ddd5..0000000 --- a/src/framework/attribute/dataProvider/DataProviderTest.php +++ /dev/null @@ -1,79 +0,0 @@ - $class - */ - public function __construct( - private ReflectionClass $class, - private ReflectionMethod $method, - private DataProviderAttribute $attribute - ) { - $methodProvider = $this->attribute->provider; - Assert::true($this->class->hasMethod($methodProvider), "Method $methodProvider does not exist in class {$this->class->getName()}"); - Assert::false($this->class->getMethod($methodProvider)->isStatic(), "Method $methodProvider is static in class {$this->class->getName()}"); - Assert::true($this->class->getMethod($methodProvider)->isPublic(), "Method $methodProvider is not public in class {$this->class->getName()}"); - } - - /** - * @phpstan-return PromiseInterface - */ - public function run() : PromiseInterface { - return $this->runRec($this->getIterator()); - } - - /** - * @phpstan-return Iterator - */ - public function getIterator() : Iterator { - $test = $this->getInstance(false); - $data = $this->getDataProvidingClosure()->call($this, $test); - $iterator = new ArrayIterator(); - foreach ($data as $key => $args) { - Assert::isArray($args); - $iterator->append(new TestMethodFeeded( - $this->class, - $this->method, - $args, - is_string($key) ? $key : null, - )); - } - - return $iterator; - } - - /** - * @return Closure(TestCase $object): iterable> - */ - private function getDataProvidingClosure() : Closure { - $provider = $this->attribute->provider; - $closure = function (TestCase $object) use ($provider) : iterable { - return $this->class->getMethod($provider)->invoke($object); - }; - - return $closure; - } - - public function __toString() : string { - return $this->class->getName() . "::" . $this->method->getName() . " with data provider"; - } -} diff --git a/src/framework/attribute/dataProvider/TestMethodFeeded.php b/src/framework/attribute/dataProvider/TestMethodFeeded.php deleted file mode 100644 index 86b7f65..0000000 --- a/src/framework/attribute/dataProvider/TestMethodFeeded.php +++ /dev/null @@ -1,44 +0,0 @@ - $class - * @param iterable $args - */ - public function __construct( - private ReflectionClass $class, - private ReflectionMethod $method, - iterable $args, - private ?string $name = null - ) { - $this->args = is_array($args) ? $args : iterator_to_array($args); - } - - /** - * @return PromiseInterface - */ - private function invokeTest(TestCase $test) : PromiseInterface { - return $this->method->invoke($test, ...$this->args); - } - - public function __toString() : string { - return $this->name ?? $this->class->getName() . "::" . $this->method->getName() . "(" . - implode(", ", array_map(fn ($v) => stringify($v), $this->args)) - . ")"; - } -} diff --git a/src/framework/result/FailedTest.php b/src/framework/result/FailedTest.php index fda8394..1edace7 100644 --- a/src/framework/result/FailedTest.php +++ b/src/framework/result/FailedTest.php @@ -3,21 +3,21 @@ namespace ShockedPlot7560\PmmpUnit\framework\result; use ShockedPlot7560\PmmpUnit\framework\runner\TestRunnerInterface; +use Stringable; use Throwable; -class FailedTest implements TestResult, ThrowableResult, \Stringable { +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 getThrowable() : Throwable { + return $this->throwable; + } - public function __toString(): string - { - return $this->test->__toString(); - } + public function __toString() : string { + return $this->test->__toString(); + } } diff --git a/src/framework/result/Fatal.php b/src/framework/result/Fatal.php index dac88a3..7765899 100644 --- a/src/framework/result/Fatal.php +++ b/src/framework/result/Fatal.php @@ -2,17 +2,20 @@ namespace ShockedPlot7560\PmmpUnit\framework\result; -class Fatal implements TestResult, ThrowableResult, \Stringable { - public function __construct( - public readonly \Throwable $throwable - ) { } +use Stringable; +use Throwable; - public function getThrowable(): \Throwable { - return $this->throwable; - } +class Fatal implements TestResult, ThrowableResult, Stringable { + public function __construct( + public readonly Throwable $throwable + ) { + } - public function __toString(): string - { - return "Runtime exception"; - } -} \ No newline at end of file + public function getThrowable() : Throwable { + return $this->throwable; + } + + public function __toString() : string { + return "Runtime exception"; + } +} diff --git a/src/framework/result/FatalTest.php b/src/framework/result/FatalTest.php index a3c4d94..c36be6c 100644 --- a/src/framework/result/FatalTest.php +++ b/src/framework/result/FatalTest.php @@ -10,11 +10,10 @@ public function __construct( public readonly TestRunnerInterface $test, Throwable $throwable ) { - parent::__construct($throwable); + parent::__construct($throwable); } - public function __toString(): string - { - return $this->test->__toString(); - } + public function __toString() : string { + return $this->test->__toString(); + } } diff --git a/src/framework/result/TestResults.php b/src/framework/result/TestResults.php index 1ed20aa..d4f03d3 100644 --- a/src/framework/result/TestResults.php +++ b/src/framework/result/TestResults.php @@ -21,9 +21,9 @@ 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); - } + public static function fatal(Throwable $throwable) : void { + self::$testResults[] = new Fatal($throwable); + } /** * @return TestResult[] diff --git a/src/framework/result/ThrowableResult.php b/src/framework/result/ThrowableResult.php index 89284bf..4cbf4e3 100644 --- a/src/framework/result/ThrowableResult.php +++ b/src/framework/result/ThrowableResult.php @@ -2,6 +2,8 @@ namespace ShockedPlot7560\PmmpUnit\framework\result; +use Throwable; + interface ThrowableResult { - public function getThrowable(); -} \ No newline at end of file + public function getThrowable() : Throwable; +} diff --git a/src/framework/result/printer/TestResultPrinter.php b/src/framework/result/printer/TestResultPrinter.php index a9b25ca..9e2ec77 100644 --- a/src/framework/result/printer/TestResultPrinter.php +++ b/src/framework/result/printer/TestResultPrinter.php @@ -3,9 +3,8 @@ namespace ShockedPlot7560\PmmpUnit\framework\result\printer; use Logger; -use ShockedPlot7560\PmmpUnit\framework\result\FailedTest; -use ShockedPlot7560\PmmpUnit\framework\result\FatalTest; use ShockedPlot7560\PmmpUnit\framework\result\ThrowableResult; +use Stringable; class TestResultPrinter { public function __construct( @@ -60,7 +59,7 @@ private function printStats(Logger $logger) : void { ); } - private function errorToString(ThrowableResult&\Stringable $error) : string { + private function errorToString(ThrowableResult&Stringable $error) : string { $ret = $error->__toString() . ": "; $ret .= str_replace("ยง", "&", $error->getThrowable()->getMessage()); $ret .= " (line: " . $error->getThrowable()->getLine() . ")"; diff --git a/src/framework/result/printer/TestResultsBag.php b/src/framework/result/printer/TestResultsBag.php index fa9f7b8..7605c2c 100644 --- a/src/framework/result/printer/TestResultsBag.php +++ b/src/framework/result/printer/TestResultsBag.php @@ -4,12 +4,11 @@ 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; class TestResultsBag { - /** @var FatalTest[] $fatalErrors */ + /** @var Fatal[] $fatalErrors */ private array $fatalErrors = []; /** @var FailedTest[] $failedTests */ @@ -57,7 +56,7 @@ public function getPassedTests() : array { } /** - * @return FatalTest[] + * @return Fatal[] */ public function getFatalErrors() : array { return $this->fatalErrors; diff --git a/src/framework/runner/DataProviderRunner.php b/src/framework/runner/DataProviderRunner.php index c541d72..20dc7a3 100644 --- a/src/framework/runner/DataProviderRunner.php +++ b/src/framework/runner/DataProviderRunner.php @@ -8,7 +8,7 @@ use React\Promise\PromiseInterface; use ReflectionClass; use ReflectionMethod; -use ShockedPlot7560\PmmpUnit\framework\attribute\dataProvider\DataProviderAttribute; +use ShockedPlot7560\PmmpUnit\framework\attribute\DataProviderAttribute; use ShockedPlot7560\PmmpUnit\framework\TestCase; use Webmozart\Assert\Assert; diff --git a/src/framework/runner/EndChildRunner.php b/src/framework/runner/EndChildRunner.php index 369b462..f1cd153 100644 --- a/src/framework/runner/EndChildRunner.php +++ b/src/framework/runner/EndChildRunner.php @@ -15,6 +15,9 @@ abstract class EndChildRunner implements TestRunnerInterface { private ?TestCase $instance = null; + /** + * @param ReflectionClass $class + */ public function __construct( protected ReflectionClass $class, protected ReflectionMethod $method @@ -128,6 +131,9 @@ protected function getInstance(bool $regenerate = true) : TestCase { } private function createInstance() : TestCase { - return $this->class->newInstance(new ExpectedExceptionHandler($this->method)); + /** @var TestCase $instance */ + $instance = $this->class->newInstance(new ExpectedExceptionHandler($this->method)); + + return $instance; } } diff --git a/src/framework/runner/SuiteTestRunner.php b/src/framework/runner/SuiteTestRunner.php index 0daa92a..d9948fe 100644 --- a/src/framework/runner/SuiteTestRunner.php +++ b/src/framework/runner/SuiteTestRunner.php @@ -5,6 +5,7 @@ use React\Promise\PromiseInterface; use ReflectionClass; use ShockedPlot7560\PmmpUnit\framework\attribute\MethodAttributeToRunner; +use ShockedPlot7560\PmmpUnit\framework\TestCase; use ShockedPlot7560\PmmpUnit\utils\Utils; class SuiteTestRunner extends MultipleTestRunner implements TestRunnerInterface { @@ -24,8 +25,11 @@ public function __toString() : string { return "SuiteTest: " . $this->name; } + /** + * @param ReflectionClass $class + */ public static function fromClassReflection(ReflectionClass $class) : self { - $testSuite = new static($class->getName()); + $testSuite = new self($class->getName()); foreach (Utils::getTestMethodsInTestCase($class) as $method) { foreach ($method->getAttributes() as $attribute) { diff --git a/src/framework/runner/TestRunnerUtils.php b/src/framework/runner/TestRunnerUtils.php index 29dfce0..0f55cb7 100644 --- a/src/framework/runner/TestRunnerUtils.php +++ b/src/framework/runner/TestRunnerUtils.php @@ -10,7 +10,6 @@ use ShockedPlot7560\PmmpUnit\framework\event\TestStartEvent; use ShockedPlot7560\PmmpUnit\framework\event\TestSuccessEvent; use Throwable; -use Webmozart\Assert\InvalidArgumentException; class TestRunnerUtils { /** @@ -23,6 +22,7 @@ public static function runRec(Iterator $iterator) : PromiseInterface { (new TestStartEvent($test))->call(); $iterator->next(); + $promise = null; try { $promise = $test->run() ->then(function () use ($test) { @@ -30,14 +30,14 @@ public static function runRec(Iterator $iterator) : PromiseInterface { }, function (Throwable $throwable) use ($test) { return self::failed($test, $throwable); }); - } catch (InvalidArgumentException $assertFailed) { + } catch (Throwable $assertFailed) { $promise = self::failed($test, $assertFailed); } finally { - return $promise->then(function () use ($iterator, $test) { + return $promise?->then(function () use ($iterator, $test) { (new TestEndEvent($test))->call(); return self::runRec($iterator); - }); + }) ?? resolve(null); } } else { return resolve(null); diff --git a/tests/pmmpunit/suitetest/normal/tests/attribute/DataProviderAttributeTest.php b/tests/pmmpunit/suitetest/normal/tests/attribute/DataProviderAttributeTest.php index b0d7001..058e355 100644 --- a/tests/pmmpunit/suitetest/normal/tests/attribute/DataProviderAttributeTest.php +++ b/tests/pmmpunit/suitetest/normal/tests/attribute/DataProviderAttributeTest.php @@ -7,7 +7,7 @@ use Iterator; use React\Promise\PromiseInterface; use function React\Promise\resolve; -use ShockedPlot7560\PmmpUnit\framework\attribute\dataProvider\DataProviderAttribute; +use ShockedPlot7560\PmmpUnit\framework\attribute\DataProviderAttribute; use ShockedPlot7560\PmmpUnit\framework\TestCase; class DataProviderAttributeTest extends TestCase {