Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactoring TestManager::runAllTests() and improving the TestManager …
…test case
  • Loading branch information
lorenzo committed May 4, 2010
1 parent 4a152d3 commit cdf4006
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 41 deletions.
66 changes: 53 additions & 13 deletions cake/tests/cases/libs/test_manager.test.php
Expand Up @@ -20,6 +20,13 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */


class TestTestManager extends TestManager {

public function setTestSuite($testSuite) {
$this->_testSuite = $testSuite;
}
}

/** /**
* TestManagerTest class * TestManagerTest class
* *
Expand All @@ -28,14 +35,37 @@
*/ */
class TestManagerTest extends CakeTestCase { class TestManagerTest extends CakeTestCase {


/**
* Number of times the funcion PHPUnit_Framework_TestSuite::addTestFile() has been called
*
* @var integer
*/
protected $_countFiles = 0;

/** /**
* setUp method * setUp method
* *
* @return void * @return void
*/ */
public function setUp() { public function setUp() {
$this->TestManager = new TestManager(); $this->_countFiles = 0;
$this->Reporter = new CakeHtmlReporter(); $this->TestManager = new TestTestManager();
$testSuiteStub = $this->getMock('PHPUnit_Framework_TestSuite');
$testSuiteStub
->expects($this->any())
->method('addTestFile')
->will($this->returnCallback(array(&$this, '_countIncludedTestFiles')));
$this->TestManager->setTestSuite($testSuiteStub);
$this->Reporter = $this->getMock('CakeHtmlReporter');
}

/**
* Helper method to count the number of times the
* function PHPUnit_Framework_TestSuite::addTestFile() has been called
* @return void
*/
public function _countIncludedTestFiles() {
$this->_countFiles++;
} }


/** /**
Expand All @@ -44,14 +74,28 @@ public function setUp() {
* @return void * @return void
*/ */
public function testRunAllTests() { public function testRunAllTests() {
$folder = new Folder(CORE_TEST_CASES); $folder = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(CORE_TEST_CASES));
$extension = str_replace('.', '\.', $this->TestManager->getExtension('test')); $extension = str_replace('.', '\.', $this->TestManager->getExtension('test'));
$out = $folder->findRecursive('.*' . $extension); $out = new RegexIterator($folder, '#^.+'.$extension.'$#i', RecursiveRegexIterator::GET_MATCH);

$files = array();
foreach ($out as $testFile) {
$files[] = $testFile[0];
}


$reporter = new CakeHtmlReporter(); $result = $this->TestManager->runAllTests($this->Reporter, true);
$list = $this->TestManager->runAllTests($reporter, true);


$this->assertEqual(count($out), count($list)); $this->assertEqual(count($files), $this->_countFiles);
$this->assertType('PHPUnit_Framework_TestResult', $result);
}

/**
* Tests that trying to run an unexistent file throws an exception
* @expectedException InvalidArgumentException
*/
public function testRunUnexistentCase() {
$file = md5(time());
$result = $this->TestManager->runTestCase($file, $this->Reporter);
} }


/** /**
Expand All @@ -60,14 +104,10 @@ public function testRunAllTests() {
* @return void * @return void
*/ */
public function testRunTestCase() { public function testRunTestCase() {
$file = md5(time());
$result = $this->TestManager->runTestCase($file, $this->Reporter);
$this->assertError('Test case ' . $file . ' cannot be found');
$this->assertFalse($result);

$file = str_replace(CORE_TEST_CASES, '', __FILE__); $file = str_replace(CORE_TEST_CASES, '', __FILE__);
$result = $this->TestManager->runTestCase($file, $this->Reporter, true); $result = $this->TestManager->runTestCase($file, $this->Reporter, true);
$this->assertTrue($result); $this->assertEqual(1, $this->_countFiles);
$this->assertType('PHPUnit_Framework_TestResult', $result);
} }


/** /**
Expand Down
71 changes: 43 additions & 28 deletions cake/tests/lib/test_manager.php
Expand Up @@ -58,6 +58,13 @@ class TestManager extends PHPUnit_Runner_BaseTestRunner {
*/ */
public $pluginTest = false; public $pluginTest = false;


/**
* TestSuite container for single or grouped test files
*
* @var PHPUnit_Framework_TestSuiteboolean
*/
protected $_testSuit = null;

/** /**
* Constructor for the TestManager class * Constructor for the TestManager class
* *
Expand All @@ -77,61 +84,55 @@ public function __construct() {
/** /**
* Runs all tests in the Application depending on the current appTest setting * Runs all tests in the Application depending on the current appTest setting
* *
* @param Object $reporter Reporter object for the tests being run. * @param PHPUnit_Framework_TestListener $reporter Reporter instance to attach to the test case.
* @param boolean $testing Are tests supposed to be auto run. Set to true to return testcase list.
* @return mixed * @return mixed
*/ */
public function runAllTests(&$reporter, $testing = false) { public function runAllTests(&$reporter) {
$testCases = $this->_getTestFileList($this->_getTestsPath()); $testCases = $this->_getTestFileList($this->_getTestsPath());

if ($this->appTest) { if ($this->appTest) {
$test = new TestSuite(__('All App Tests', true)); $test = $this->getTestSuite(__('All App Tests', true));
} else if ($this->pluginTest) { } else if ($this->pluginTest) {
$test = new TestSuite(sprintf(__('All %s Plugin Tests', true), Inflector::humanize($this->pluginTest))); $test = $this->getTestSuite(sprintf(__('All %s Plugin Tests', true), Inflector::humanize($this->pluginTest)));
} else { } else {
$test = new TestSuite(__('All Core Tests', true)); $test = $this->getTestSuite(__('All Core Tests', true));
}

if ($testing) {
return $testCases;
} }


foreach ($testCases as $testCase) { foreach ($testCases as $testCase) {
$test->addTestFile($testCase); $test->addTestFile($testCase);
} }


return $test->run($reporter); $result = new PHPUnit_Framework_TestResult;
$result->addListener($reporter);
$reporter->paintHeader();
$run = $test->run($result);
$reporter->paintResult($result);
return $result;
} }


/** /**
* Runs a specific test case file * Runs a specific test case file
* *
* @param string $testCaseFile Filename of the test to be run. * @param string $testCaseFile Filename of the test to be run.
* @param PHPUnit_Framework_TestListener $reporter Reporter instance to attach to the test case. * @param PHPUnit_Framework_TestListener $reporter Reporter instance to attach to the test case.
* @param boolean $testing Set to true if testing, otherwise test case will be run. * @throws InvalidArgumentException if the supplied $testCaseFile does not exists
* @return mixed Result of test case being run. * @return mixed Result of test case being run.
*/ */
public function runTestCase($testCaseFile, PHPUnit_Framework_TestListener &$reporter, $testing = false) { public function runTestCase($testCaseFile, PHPUnit_Framework_TestListener &$reporter) {
$testCaseFileWithPath = $this->_getTestsPath() . DS . $testCaseFile; $testCaseFileWithPath = $this->_getTestsPath() . DS . $testCaseFile;


if (!file_exists($testCaseFileWithPath)) { if (!file_exists($testCaseFileWithPath)) {
trigger_error(sprintf(__('Test case %s cannot be found', true), $testCaseFile), E_USER_ERROR); throw new InvalidArgumentException(sprintf(__('Unable to load test file %s'), $testCaseFile));
return false;
}

if ($testing) {
return true;
} }


$testSuite = new PHPUnit_Framework_TestSuite; $testSuite = $this->getTestSuite(sprintf(__('Individual test case: %s', true), $testCaseFile));
$testSuite->setName(sprintf(__('Individual test case: %s', true), $testCaseFile));
$testSuite->addTestFile($testCaseFileWithPath); $testSuite->addTestFile($testCaseFileWithPath);

$result = new PHPUnit_Framework_TestResult; $result = new PHPUnit_Framework_TestResult;
$result->addListener($reporter); $result->addListener($reporter);
$reporter->paintHeader(); $reporter->paintHeader();
$run = $testSuite->run($result); $run = $testSuite->run($result);
$reporter->paintResult($result); $reporter->paintResult($result);
return $run; return $result;
} }


/** /**
Expand Down Expand Up @@ -300,13 +301,14 @@ protected function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
return $fileList; return $fileList;
} }


$files = glob($directory . DS . '*'); $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
$files = $files ? $files : array();


foreach ($files as $file) { foreach ($files as $file) {
if (is_dir($file)) { if (!$file->isFile()) {
$fileList = array_merge($fileList, $this->_getRecursiveFileList($file, $fileTestFunction)); continue;
} elseif ($fileTestFunction[0]->$fileTestFunction[1]($file)) { }
$file = $file->getRealPath();
if ($fileTestFunction[0]->$fileTestFunction[1]($file)) {
$fileList[] = $file; $fileList[] = $file;
} }
} }
Expand Down Expand Up @@ -387,6 +389,19 @@ public function getExtension($type = 'test') {
return $this->_groupExtension; return $this->_groupExtension;
} }


/**
* Get the container testSuite instance for this runner or creates a new one
*
* @param string $name The name for the container test suite
* @return PHPUnit_Framework_TestSuite container test suite
*/
protected function getTestSuite($name = '') {
if (!empty($this->_testSuite)) {
return $this->_testSuite;
}
return $this->_testSuite = new PHPUnit_Framework_TestSuite($name);
}

protected function runFailed($message) { protected function runFailed($message) {


} }
Expand Down

0 comments on commit cdf4006

Please sign in to comment.