|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Tester. |
| 5 | + * Copyright (c) 2009 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Tester; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Runner of TestCase. |
| 15 | + */ |
| 16 | +class TestCaseRunner |
| 17 | +{ |
| 18 | + private const LIST_METHODS = 'nette-tester-list-methods'; |
| 19 | + |
| 20 | + /** @var array */ |
| 21 | + private $classes = []; |
| 22 | + |
| 23 | + |
| 24 | + public function findTests(string $fileMask): self |
| 25 | + { |
| 26 | + foreach (glob($fileMask) as $file) { |
| 27 | + require_once $file; // TODO: use autoloading |
| 28 | + $this->classes[] = basename($file, '.php'); |
| 29 | + } |
| 30 | + return $this; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + public function run(): void |
| 35 | + { |
| 36 | + if ($this->runFromCli()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + foreach ($this->classes as $class) { |
| 41 | + $test = $this->createInstance($class); |
| 42 | + $test->run(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + private function runFromCli(): bool |
| 48 | + { |
| 49 | + $args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []); |
| 50 | + $arg = reset($args); |
| 51 | + if (!$arg) { |
| 52 | + return false; |
| 53 | + |
| 54 | + } elseif ($arg === self::LIST_METHODS) { |
| 55 | + Environment::$checkAssertions = false; |
| 56 | + $methods = []; |
| 57 | + foreach ($this->classes as $class) { |
| 58 | + foreach ($class::findMethods() as $method) { |
| 59 | + $methods[] = $class . '::' . $method; |
| 60 | + } |
| 61 | + } |
| 62 | + header('Content-Type: text/plain'); |
| 63 | + echo '[' . implode(',', $methods) . ']'; |
| 64 | + |
| 65 | + } else { |
| 66 | + [$class, $method] = explode('::', $arg); |
| 67 | + $test = $this->createInstance($class); |
| 68 | + $test->runTest($method); |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + protected function createInstance(string $class): TestCase |
| 75 | + { |
| 76 | + // TOO: can be altered via setFactory(callable) |
| 77 | + return new $class; |
| 78 | + } |
| 79 | +} |
0 commit comments