Adding _mergeVars to Shell. This allows $tasks and $uses to work much

like $uses in Controllers, and provides consistency in the framework.
Adding tests for merging vars.
This commit is contained in:
mark_story 2010-11-17 23:40:19 -05:00
parent a8554df077
commit b80535573e
2 changed files with 60 additions and 0 deletions

View file

@ -164,6 +164,36 @@ class Shell extends Object {
if ($this->stdin == null) { if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin'); $this->stdin = new ConsoleInput('php://stdin');
} }
$merge = array();
if ($this->tasks !== null && $this->tasks !== false) {
$merge[] = 'tasks';
}
if ($this->uses !== null && $this->uses !== false) {
$merge[] = 'uses';
}
if (!empty($merge)) {
$this->_mergeVars($merge, get_parent_class($this));
}
}
/**
* Merges this objects $property with the property in $class' definition.
* This classes value for the property will be merged on top of $class'
*
* This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
* this method as an empty function.
*
* @param array $properties The name of the properties to merge.
* @param sting $class The class to merge the property with.
* @return void
*/
protected function _mergeVars($properties, $class) {
$classProperties = get_class_vars($class);
foreach ($properties as $var) {
if (isset($classProperties[$var]) && !empty($classProperties[$var]) && is_array($this->{$var})) {
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
}
}
} }
/** /**

View file

@ -69,6 +69,20 @@ class TestShell extends Shell {
protected function no_access() { protected function no_access() {
} }
public function mergeVars($properties, $class) {
return $this->_mergeVars($properties, $class);
}
}
/**
* Class for testing merging vars
*
* @package cake.tests.cases.console
*/
class TestMergeShell extends Shell {
var $tasks = array('DbConfig', 'Fixture');
var $uses = array('Comment');
} }
/** /**
@ -134,6 +148,22 @@ class ShellTest extends CakeTestCase {
$this->assertType('ConsoleOutput', $this->Shell->stderr); $this->assertType('ConsoleOutput', $this->Shell->stderr);
} }
/**
* test merging vars
*
* @return void
*/
function testMergeVars() {
$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
$this->Shell->uses = array('Posts');
$this->Shell->mergeVars(array('tasks', 'uses'), 'TestMergeShell');
$this->assertEquals(array('DbConfig', 'Fixture', 'DbConfig' => array('one', 'two')), $this->Shell->tasks);
$expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
$this->assertEquals($expected, Set::normalize($this->Shell->tasks), 'Normalized results are wrong.');
$this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
}
/** /**
* testInitialize method * testInitialize method
* *