diff --git a/demo/TestFour.php b/demo/TestFour.php new file mode 100644 index 00000000..ef695009 --- /dev/null +++ b/demo/TestFour.php @@ -0,0 +1,19 @@ +findTests(__DIR__ . '/Test*.php') + ->run(); diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 28d86873..de7f72d3 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -34,7 +34,7 @@ public function run(): void throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.'); } - if (self::getCliArgument()) { + if (TestCaseRunner::getCliArgument()) { $this->runFromCli(); return; } @@ -54,16 +54,9 @@ public static function findMethods(): array } - public static function getCliArgument(): ?string - { - $args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []); - return reset($args) ?: null; - } - - private function runFromCli(): void { - $arg = self::getCliArgument(); + $arg = TestCaseRunner::getCliArgument(); if ($arg === self::LIST_METHODS) { Environment::$checkAssertions = false; header('Content-Type: text/plain'); diff --git a/src/Framework/TestCaseRunner.php b/src/Framework/TestCaseRunner.php new file mode 100644 index 00000000..58861fd7 --- /dev/null +++ b/src/Framework/TestCaseRunner.php @@ -0,0 +1,87 @@ +classes[] = basename($file, '.php'); + } + return $this; + } + + + public function run(): void + { + if ($this::getCliArgument()) { + $this->runFromCli(); + } else { + $this->runTests(); + } + } + + + private function runTests(): void + { + foreach ($this->classes as $class) { + $test = $this->createInstance($class); + $test->run(); + } + } + + + public static function getCliArgument(): ?string + { + $args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []); + return reset($args) ?: null; + } + + + private function runFromCli(): void + { + $arg = self::getCliArgument(); + if ($arg === self::LIST_METHODS) { + Environment::$checkAssertions = false; + $methods = []; + foreach ($this->classes as $class) { + foreach ($class::findMethods() as $method) { + $methods[] = $class . '::' . $method; + } + } + header('Content-Type: text/plain'); + echo '[' . implode(',', $methods) . ']'; + + } else { + [$class, $method] = explode('::', $arg); + $test = $this->createInstance($class); + $test->runTest($method); + } + } + + + protected function createInstance(string $class): TestCase + { + // TOO: can be altered via setFactory(callable) + return new $class; + } +} diff --git a/src/bootstrap.php b/src/bootstrap.php index 7c6b9a90..1a059117 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -14,6 +14,7 @@ require __DIR__ . '/Framework/Dumper.php'; require __DIR__ . '/Framework/FileMock.php'; require __DIR__ . '/Framework/TestCase.php'; +require __DIR__ . '/Framework/TestCaseRunner.php'; require __DIR__ . '/Framework/DomQuery.php'; require __DIR__ . '/Framework/FileMutator.php'; require __DIR__ . '/CodeCoverage/Collector.php';