Skip to content

Commit

Permalink
TestHandler: list TestCase methods by running with TestCase::LIST_MET…
Browse files Browse the repository at this point in the history
…HODS argument [Closes #61]
  • Loading branch information
milo committed Jan 8, 2014
1 parent c2b178a commit 96b192c
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 7 deletions.
21 changes: 20 additions & 1 deletion Tester/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,35 @@
*/
class TestCase
{
/** @internal */
const LIST_METHODS = 'nette-tester-list-methods';


/**
* Runs the test case.
* @return void
*/
public function run($method = NULL)
{
$pattern = '#^test[A-Z0-9_]#';
$rc = new \ReflectionClass($this);

if ($method === self::LIST_METHODS) {
$tmp = array();
foreach ($rc->getMethods() as $method) {
if (preg_match($pattern, $method->getName())) {
$tmp[] = $method->getName();
}
}

$mark = self::LIST_METHODS;
echo "\n$mark-begin\n" . json_encode($tmp) . "\n$mark-end\n";
exit(1);
}

$methods = $method ? array($rc->getMethod($method)) : $rc->getMethods();
foreach ($methods as $method) {
if (!preg_match('#^test[A-Z0-9_]#', $method->getName())) {
if (!preg_match($pattern, $method->getName())) {
continue;
}

Expand Down
38 changes: 33 additions & 5 deletions Tester/Runner/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,40 @@ private function initiateMultiple($count, PhpExecutable $php, $file)

private function initiateTestCase($foo, PhpExecutable $php, $file)
{
if (preg_match_all('#\sfunction\s+(test\w+)\(#', file_get_contents($file), $matches)) {
foreach ($matches[1] as $item) {
$this->runner->addJob(new Job($file, $php, escapeshellarg($item)));
}
return TRUE;
$proc = proc_open(
$php->getCommandLine()
. ' '
. escapeshellarg($file)
. ' '
. escapeshellarg(Tester\TestCase::LIST_METHODS),
array(
array('pipe', 'r'),
array('pipe', 'w'),
array('pipe', 'w'),
),
$pipes,
dirname($file),
NULL,
array('bypass_shell' => TRUE)
);

$stdout = stream_get_contents($pipes[1]);
array_map('fclose', $pipes);

$mark = Tester\TestCase::LIST_METHODS;
if (!preg_match('#\n'.$mark.'-begin\n(.*?)\n'.$mark.'-end\n#s', $stdout, $match)) {
return array(Runner::FAILED, "Cannot list TestCase methods in file '$file'. Do you call TestCase::run() in it?");
}

$methods = json_decode($match[1]);
if (!count($methods)) {
return array(Runner::SKIPPED, "TestCase in file '$file' does not contain test methods.");
}

foreach ($methods as $method) {
$this->runner->addJob(new Job($file, $php, escapeshellarg($method)));
}
return TRUE;
}


Expand Down
1 change: 1 addition & 0 deletions Tester/tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require __DIR__ . '/Framework/Assert.php';
require __DIR__ . '/Framework/Dumper.php';
require __DIR__ . '/Framework/DataProvider.php';
require __DIR__ . '/Framework/TestCase.php';

use Tester\Runner\CommandLine as Cmd;

Expand Down
47 changes: 47 additions & 0 deletions tests/Runner.multiple-fails.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Tester\Assert,
Tester\Runner\Runner;

require __DIR__ . '/bootstrap.php';
require __DIR__ . '/../Tester/Runner/OutputHandler.php';
require __DIR__ . '/../Tester/Runner/TestHandler.php';
require __DIR__ . '/../Tester/Runner/PhpExecutable.php';
require __DIR__ . '/../Tester/Runner/Runner.php';

if (PHP_VERSION_ID < 50400) {
Tester\Environment::skip('Requires constant PHP_BINARY available since PHP 5.4.0');
}


class Logger implements Tester\Runner\OutputHandler
{
public $results = array();

function result($testName, $result, $message)
{
$this->results[basename($testName)] = array($result, $message);
}

function begin() {}
function end() {}
}

$runner = new Runner(new Tester\Runner\PhpExecutable(PHP_BINARY));
$runner->paths[] = __DIR__ . '/multiple-fails/*.phptx';
$runner->outputHandlers[] = $logger = new Logger;
$runner->run();

Assert::match(
"TestCase in file '%a%testcase-no-methods.phptx' does not contain test methods.",
$logger->results['testcase-no-methods.phptx'][1]
);
Assert::same( Runner::SKIPPED, $logger->results['testcase-no-methods.phptx'][0] );

Assert::match(
"Cannot list TestCase methods in file '%a%testcase-not-call-run.phptx'. Do you call TestCase::run() in it?",
$logger->results['testcase-not-call-run.phptx'][1]
);
Assert::same( Runner::FAILED, $logger->results['testcase-not-call-run.phptx'][0] );

Assert::same( 2, count($logger->results) );
17 changes: 17 additions & 0 deletions tests/multiple-fails/testcase-no-methods.phptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @testcase
*/

use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';


class MyTest extends TestCase
{
}

$testCase = new MyTest;
$testCase->run(isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : NULL);
19 changes: 19 additions & 0 deletions tests/multiple-fails/testcase-not-call-run.phptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @testcase
*/

use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';


class MyTest extends TestCase
{

public function testFoo()
{
exit(1);
}
}
10 changes: 9 additions & 1 deletion tests/multiple/testcase.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* @testcase
*/

class MyTest
use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';


class MyTest extends TestCase
{

public function testFoo()
Expand Down Expand Up @@ -38,3 +43,6 @@ class MyTest
}

}

$testCase = new MyTest;
$testCase->run(isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : NULL);

0 comments on commit 96b192c

Please sign in to comment.