Skip to content

Commit

Permalink
TestCase: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 7, 2019
1 parent 911c0e6 commit 0708838
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
class TestCase
{
/** @internal */
public const
LIST_METHODS = 'nette-tester-list-methods',
METHOD_PATTERN = '#^test[A-Z0-9_]#';
public const LIST_METHODS = 'nette-tester-list-methods';
private const METHOD_PATTERN = '#^test[A-Z0-9_]#';

/** @var bool */
private $handleErrors = false;
Expand All @@ -36,24 +34,43 @@ public function run(): void
throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.');
}

$methods = array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm): string {
return $rm->getName();
}, (new \ReflectionObject($this))->getMethods())));

if (isset($_SERVER['argv']) && ($tmp = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv']))) {
$method = reset($tmp);
if ($method === self::LIST_METHODS) {
Environment::$checkAssertions = false;
header('Content-Type: text/plain');
echo '[' . implode(',', $methods) . ']';
return;
}
if (self::getCliArgument()) {
$this->runFromCli();
return;
}

foreach ($this::findMethods() as $method) {
$this->runTest($method);
}
}


public static function findMethods(): array
{
$methods = (new \ReflectionClass(static::class))->getMethods();
return array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm): string {
return $rm->getName();
}, $methods)));
}


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;
header('Content-Type: text/plain');
echo '[' . implode(',', $this::findMethods()) . ']';

} else {
foreach ($methods as $method) {
$this->runTest($method);
}
$this->runTest($arg);
}
}

Expand Down

0 comments on commit 0708838

Please sign in to comment.