Skip to content
Permalink
Browse files

Adding test cases and ability to pull all

fixtures for a model.
  • Loading branch information...
markstory committed May 24, 2009
1 parent f20a7e4 commit 61191d88cd46fc0c3afdbb42aecdfac422bddab3
Showing with 132 additions and 9 deletions.
  1. +57 −0 cake/console/libs/tasks/test.php
  2. +75 −9 cake/tests/cases/console/libs/tasks/test.test.php
@@ -220,6 +220,63 @@ function getTestableMethods($className) {
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.
*
@@ -49,25 +49,75 @@
'TestTask', 'MockTestTask',
array('in', 'out', 'createFile')
);
/**
* Test subject for method extraction
*
**/
class TestTaskSubjectClass extends Object {
function methodOne() {
}
function methodTwo() {
}
function _noTest() {
function methodOne() { }
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
*
* @package cake
* @subpackage cake.tests.cases.console.libs.tasks
*/
class TestTaskTest extends CakeTestCase {
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
/**
* setUp method
*
@@ -79,6 +129,7 @@ function setUp() {
$this->Task =& new MockTestTask($this->Dispatcher);
$this->Task->Dispatch = new $this->Dispatcher;
}
/**
* tearDown method
*
@@ -88,6 +139,7 @@ function setUp() {
function tearDown() {
ClassRegistry::flush();
}
/**
* Test that file path generation doesn't continuously append paths.
*
@@ -119,6 +171,20 @@ function testMethodIntrospection() {
$result = $this->Task->getTestableMethods('TestTaskSubjectClass');
$expected = array('methodOne', 'methodTwo');
$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));
}
}

0 comments on commit 61191d8

Please sign in to comment.
You can’t perform that action at this time.