Skip to content

Commit

Permalink
Adding method extraction and test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 24, 2009
1 parent dee9b63 commit f20a7e4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
20 changes: 20 additions & 0 deletions cake/console/libs/tasks/test.php
Expand Up @@ -200,6 +200,26 @@ function __extras($class) {
return $extras;
}

/**
* Get methods declared in the class given.
* No parent methods will be returned
*
* @param string $className Name of class to look at.
* @return array Array of method names.
**/
function getTestableMethods($className) {
$classMethods = get_class_methods($className);
$parentMethods = get_class_methods(get_parent_class($className));
$thisMethods = array_diff($classMethods, $parentMethods);
$out = array();
foreach ($thisMethods as $method) {
if (substr($method, 0, 1) != '_') {
$out[] = $method;
}
}
return $out;
}

/**
* Create a test for a Model object.
*
Expand Down
37 changes: 31 additions & 6 deletions cake/tests/cases/console/libs/tasks/test.test.php
Expand Up @@ -42,13 +42,25 @@
}

Mock::generatePartial(
'ShellDispatcher', 'TestTestTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
'ShellDispatcher', 'TestTestTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
Mock::generatePartial(
'TestTask', 'MockTestTask',
array('in', 'out', 'createFile')
);
'TestTask', 'MockTestTask',
array('in', 'out', 'createFile')
);

class TestTaskSubjectClass extends Object {
function methodOne() {

}
function methodTwo() {

}
function _noTest() {

}
}
/**
* TestTaskTest class
*
Expand Down Expand Up @@ -96,5 +108,18 @@ function testFilePathGeneration () {
$this->Task->expectAt(1, 'createFile', array($file, '*'));
$this->Task->bake('Model', 'MyClass');
}

/**
* Test that method introspection pulls all relevant non parent class
* methods into the test case.
*
* @return void
**/
function testMethodIntrospection() {
$result = $this->Task->getTestableMethods('TestTaskSubjectClass');
$expected = array('methodOne', 'methodTwo');
$this->assertEqual($result, $expected);

}
}
?>

0 comments on commit f20a7e4

Please sign in to comment.