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 b949098 commit 355525f
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 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,25 +34,39 @@ 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 ($this->runFromCli()) {
return;
}

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

} else {
foreach ($methods 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)));
}


private function runFromCli(): bool
{
$args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
$arg = reset($args);
if (!$arg) {
return false;
} elseif ($arg === self::LIST_METHODS) {
Environment::$checkAssertions = false;
header('Content-Type: text/plain');
echo '[' . implode(',', $this::findMethods()) . ']';
} elseif ($arg) {
$this->runTest($arg);
}
return true;
}


Expand Down

0 comments on commit 355525f

Please sign in to comment.