diff --git a/cake/console/libs/tasks/test.php b/cake/console/libs/tasks/test.php index c45f75fde10..3039dec9bf7 100644 --- a/cake/console/libs/tasks/test.php +++ b/cake/console/libs/tasks/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. * diff --git a/cake/tests/cases/console/libs/tasks/test.test.php b/cake/tests/cases/console/libs/tasks/test.test.php index b131a604b90..d1a3f0e3844 100644 --- a/cake/tests/cases/console/libs/tasks/test.test.php +++ b/cake/tests/cases/console/libs/tasks/test.test.php @@ -49,18 +49,66 @@ '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 * @@ -68,6 +116,8 @@ function _noTest() { * @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)); } }