Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Nov 18, 2010
1 parent a8554df commit b80535573e10fcb4249b34c9676fbb62aa3c69f7
Showing with 60 additions and 0 deletions.
  1. +30 −0 cake/console/shells/shell.php
  2. +30 −0 cake/tests/cases/console/shells/shell.test.php
@@ -164,6 +164,36 @@ function __construct($stdout = null, $stderr = null, $stdin = null) {
if ($this->stdin == null) {
$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});
}
}
}
/**
@@ -69,6 +69,20 @@ public function _secret() {
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 @@ public function testConstruct() {
$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
*

0 comments on commit b805355

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