Skip to content

Commit

Permalink
TestCase: refactoring of run()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 11, 2014
1 parent 883dcda commit 066a2eb
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions Tester/Framework/TestCase.php
Expand Up @@ -16,7 +16,8 @@
class TestCase
{
/** @internal */
const LIST_METHODS = 'nette-tester-list-methods';
const LIST_METHODS = 'nette-tester-list-methods',
METHOD_PATTERN = '#^test[A-Z0-9_]#';


/**
Expand All @@ -25,75 +26,73 @@ class TestCase
*/
public function run($method = NULL)
{
$pattern = '#^test[A-Z0-9_]#';
$rc = new \ReflectionClass($this);

if ($method === NULL && isset($_SERVER['argv'][1])) {
if ($_SERVER['argv'][1] === self::LIST_METHODS) {
$tmp = array();
foreach ($rc->getMethods() as $method) {
if (preg_match($pattern, $method->getName())) {
$tmp[] = $method->getName();
}
}
echo json_encode($tmp);
echo json_encode(preg_grep(self::METHOD_PATTERN, get_class_methods($this)));

This comment has been minimized.

Copy link
@milo

milo Jan 11, 2014

Member

I have to laugh, where you dig out such a function like preg_grep 馃槂

This comment has been minimized.

Copy link
@dg

dg Jan 11, 2014

Author Member

:-)

return;
}
$method = $_SERVER['argv'][1];
}

$methods = $method ? array($rc->getMethod($method)) : $rc->getMethods();
$methods = preg_grep(self::METHOD_PATTERN, $method ? array($method) : get_class_methods($this));
foreach ($methods as $method) {
if (!preg_match($pattern, $method->getName())) {
continue;
}
$this->runMethod($method);
}
}

if (!$method->isPublic()) {
throw new TestCaseException("Method {$method->getName()} is not public. Make it public or rename it.");
}

$data = array();
$info = Helpers::parseDocComment($method->getDocComment()) + array('dataprovider' => NULL, 'throws' => NULL);
/**
* Runs the test method.
* @return void
*/
private function runMethod($method)
{
$method = new \ReflectionMethod($this, $method);
if (!$method->isPublic()) {
throw new TestCaseException("Method {$method->getName()} is not public. Make it public or rename it.");
}

$data = array();
$info = Helpers::parseDocComment($method->getDocComment()) + array('dataprovider' => NULL, 'throws' => NULL);

if ($info['throws'] === '') {
throw new TestCaseException("Missing class name in @throws annotation for {$method->getName()}().");
} elseif (is_array($info['throws'])) {
throw new TestCaseException("Annotation @throws for {$method->getName()}() can be specified only once.");
} else {
$throws = preg_split('#\s+#', $info['throws'], 2) + array(NULL, NULL);
}
if ($info['throws'] === '') {
throw new TestCaseException("Missing class name in @throws annotation for {$method->getName()}().");
} elseif (is_array($info['throws'])) {
throw new TestCaseException("Annotation @throws for {$method->getName()}() can be specified only once.");
} else {
$throws = preg_split('#\s+#', $info['throws'], 2) + array(NULL, NULL);
}

foreach ((array) $info['dataprovider'] as $provider) {
$res = $this->getData($provider);
if (!is_array($res)) {
throw new TestCaseException("Data provider $provider() doesn't return array.");
}
$data = array_merge($data, $res);
foreach ((array) $info['dataprovider'] as $provider) {
$res = $this->getData($provider);
if (!is_array($res)) {
throw new TestCaseException("Data provider $provider() doesn't return array.");
}
if (!$info['dataprovider']) {
if ($method->getNumberOfRequiredParameters()) {
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
}
$data[] = array();
$data = array_merge($data, $res);
}
if (!$info['dataprovider']) {
if ($method->getNumberOfRequiredParameters()) {
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
}
$data[] = array();
}

foreach ($data as $args) {
try {
if ($info['throws']) {
$tmp = $this;
$e = Assert::error(function() use ($tmp, $method, $args) {
$tmp->runTest($method->getName(), $args);
}, $throws[0], $throws[1]);
if ($e instanceof AssertException) {
throw $e;
}
} else {
$this->runTest($method->getName(), $args);
foreach ($data as $args) {
try {
if ($info['throws']) {
$tmp = $this;
$e = Assert::error(function() use ($tmp, $method, $args) {
$tmp->runTest($method->getName(), $args);
}, $throws[0], $throws[1]);
if ($e instanceof AssertException) {
throw $e;
}
} catch (AssertException $e) {
$e->message .= " in {$method->getName()}" . (substr(Dumper::toLine($args), 5));
throw $e;
} else {
$this->runTest($method->getName(), $args);
}
} catch (AssertException $e) {
$e->message .= " in {$method->getName()}" . (substr(Dumper::toLine($args), 5));
throw $e;
}
}
}
Expand Down

0 comments on commit 066a2eb

Please sign in to comment.