Adding test cases and ability to pull all

fixtures for a model.
This commit is contained in:
mark_story 2009-05-24 01:15:31 -04:00
parent f20a7e4f61
commit 61191d88cd
2 changed files with 133 additions and 10 deletions

View file

@ -220,6 +220,63 @@ class TestTask extends Shell {
return $out; return $out;
} }
/**
* Generate the list of fixtures that will be required to run this test based on
* loaded models.
*
* @param object The object you want to generate fixtures for.
* @return array Array of fixtures to be included in the test.
**/
function generateFixtureList(&$subject) {
$this->_fixtures = array();
if (is_a($subject, 'Model')) {
$this->_processModel($subject);
} elseif (is_a($subject, 'Controller')) {
$this->_processController($subject);
}
return array_values($this->_fixtures);
}
/**
* Process a model recursively and pull out all the
* model names converting them to fixture names.
*
* @return void
**/
function _processModel(&$subject) {
$this->_addFixture($subject->name);
$associated = $subject->getAssociated();
foreach ($associated as $alias => $type) {
$className = $subject->{$alias}->name;
if (!isset($this->_fixtures[$className])) {
$this->_processModel($subject->{$alias});
}
if ($type == 'hasAndBelongsToMany') {
$joinModel = Inflector::classify($subject->hasAndBelongsToMany[$alias]['joinTable']);
if (!isset($this->_fixtures[$joinModel])) {
$this->_processModel($subject->{$joinModel});
}
}
}
}
/**
* Add classname to the fixture list.
* Sets the app. or plugin.plugin_name. prefix.
*
* @return void
**/
function _addFixture($name) {
$parent = get_parent_class($name);
$prefix = 'app.';
if (strtolower($parent) != 'appmodel' && strtolower(substr($parent, -8)) == 'appmodel') {
$pluginName = substr($parent, 0, strlen($parent) -8);
$prefix = 'plugin.' . Inflector::underscore($pluginName) . '.';
}
$fixture = $prefix . Inflector::underscore($name);
$this->_fixtures[$name] = $fixture;
}
/** /**
* Create a test for a Model object. * Create a test for a Model object.
* *

View file

@ -49,18 +49,66 @@ Mock::generatePartial(
'TestTask', 'MockTestTask', 'TestTask', 'MockTestTask',
array('in', 'out', 'createFile') array('in', 'out', 'createFile')
); );
/**
* Test subject for method extraction
*
**/
class TestTaskSubjectClass extends Object { class TestTaskSubjectClass extends Object {
function methodOne() { function methodOne() { }
function methodTwo() { }
} function _noTest() { }
function methodTwo() {
}
function _noTest() {
}
} }
/**
* Test subject models for fixture generation
**/
class TestTaskArticle extends Model {
var $name = 'TestTaskArticle';
var $useTable = 'articles';
var $hasMany = array(
'Comment' => array(
'className' => 'TestTask.TestTaskComment',
'foreignKey' => 'article_id',
)
);
var $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'TestTaskTag',
'joinTable' => 'articles_tags',
'foreignKey' => 'article_id',
'associationForeignKey' => 'tag_id'
)
);
}
class TestTaskTag extends Model {
var $name = 'TestTaskTag';
var $useTable = 'tags';
var $hasAndBelongsToMany = array(
'Article' => array(
'className' => 'TestTaskArticle',
'joinTable' => 'articles_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'article_id'
)
);
}
/**
* Simulated Plugin
**/
class TestTaskAppModel extends Model {
}
class TestTaskComment extends TestTaskAppModel {
var $name = 'TestTaskComment';
var $useTable = 'comments';
var $belongsTo = array(
'Article' => array(
'className' => 'TestTaskArticle',
'foreignKey' => 'article_id',
)
);
}
/** /**
* TestTaskTest class * TestTaskTest class
* *
@ -68,6 +116,8 @@ class TestTaskSubjectClass extends Object {
* @subpackage cake.tests.cases.console.libs.tasks * @subpackage cake.tests.cases.console.libs.tasks
*/ */
class TestTaskTest extends CakeTestCase { class TestTaskTest extends CakeTestCase {
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
/** /**
* setUp method * setUp method
* *
@ -79,6 +129,7 @@ class TestTaskTest extends CakeTestCase {
$this->Task =& new MockTestTask($this->Dispatcher); $this->Task =& new MockTestTask($this->Dispatcher);
$this->Task->Dispatch = new $this->Dispatcher; $this->Task->Dispatch = new $this->Dispatcher;
} }
/** /**
* tearDown method * tearDown method
* *
@ -88,6 +139,7 @@ class TestTaskTest extends CakeTestCase {
function tearDown() { function tearDown() {
ClassRegistry::flush(); ClassRegistry::flush();
} }
/** /**
* Test that file path generation doesn't continuously append paths. * Test that file path generation doesn't continuously append paths.
* *
@ -119,6 +171,20 @@ class TestTaskTest extends CakeTestCase {
$result = $this->Task->getTestableMethods('TestTaskSubjectClass'); $result = $this->Task->getTestableMethods('TestTaskSubjectClass');
$expected = array('methodOne', 'methodTwo'); $expected = array('methodOne', 'methodTwo');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
}
/**
* test that the generation of fixtures works correctly.
*
* @return void
**/
function testFixtureArrayGeneration() {
$subject = ClassRegistry::init('TestTaskArticle');
$result = $this->Task->generateFixtureList($subject);
$expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
'app.test_task_article', 'app.test_task_tag');
$this->assertEqual(sort($result), sort($expected));
} }
} }