Adding _mergeVars to Controller (this duplicated method will be re-factored away).

Updating tests in ControllerMergeVarsTest to use PHPUnit assertions.
This commit is contained in:
mark_story 2010-11-20 13:11:26 -05:00
parent e2aeae3825
commit 8f82156a51
2 changed files with 50 additions and 44 deletions

View file

@ -409,30 +409,53 @@ class Controller extends Object {
}
}
/**
* 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} != $classProperties[$var]
) {
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
}
}
}
/**
* Merge components, helpers, and uses vars from AppController and PluginAppController.
*
* @return void
*/
protected function __mergeVars() {
$pluginName = Inflector::camelize($this->plugin);
$pluginController = $pluginName . 'AppController';
if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
$appVars = get_class_vars('AppController');
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
$plugin = null;
$pluginName = $pluginController = null;
if (!empty($this->plugin)) {
$plugin = $pluginName . '.';
$pluginName = Inflector::camelize($this->plugin);
$pluginController = $pluginName . 'AppController';
if (!is_subclass_of($this, $pluginController)) {
$pluginController = null;
}
} else {
$pluginController = null;
$plugin = $pluginName . '.';
}
if (is_subclass_of($this, 'AppController') || !empty($pluginController)) {
$appVars = get_class_vars('AppController');
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
if ($uses == $this->uses && !empty($this->uses)) {
if (!in_array($plugin . $this->modelClass, $this->uses)) {
array_unshift($this->uses, $plugin . $this->modelClass);
@ -446,19 +469,10 @@ class Controller extends Object {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (!empty($appVars[$var]) && is_array($this->{$var})) {
if ($var !== 'uses') {
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
if ($app !== $normal) {
$this->{$var} = Set::merge($app, $normal);
}
} else {
$this->{$var} = Set::merge(
array_diff($appVars[$var], $this->{$var}), $this->{$var}
);
}
$this->_mergeVars($merge, 'AppController');
foreach ($merge as $prop) {
if ($prop !== 'components') {
$this->{$prop} = array_unique($this->{$prop});
}
}
}
@ -471,20 +485,10 @@ class Controller extends Object {
if ($this->uses !== null || $this->uses !== false) {
$merge[] = 'uses';
}
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
if ($var !== 'uses') {
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
if ($app !== $normal) {
$this->{$var} = Set::merge($app, $normal);
}
} else {
$this->{$var} = Set::merge(
array_diff($appVars[$var], $this->{$var}), $this->{$var}
);
}
$this->_mergeVars($merge, $pluginController);
foreach ($merge as $prop) {
if ($prop !== 'components') {
$this->{$prop} = array_unique($this->{$prop});
}
}
}

View file

@ -19,6 +19,8 @@
* @since CakePHP(tm) v 1.2.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', 'Controller');
if (!class_exists('AppController')) {
/**
@ -220,13 +222,13 @@ class ControllerMergeVarsTest extends CakeTestCase {
'Auth' => array('setting' => 'val', 'otherVal'),
'Email' => array('ports' => 'open')
);
$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
$this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
$expected = array(
'MergeVar' => array('format' => 'html', 'terse'),
'Javascript' => null
);
$this->assertEqual($Controller->helpers, $expected, 'Helpers are unexpected %s');
$this->assertEquals($expected, $Controller->helpers, 'Helpers are unexpected.');
$Controller = new MergePostsController();
$Controller->components = array();
@ -237,7 +239,7 @@ class ControllerMergeVarsTest extends CakeTestCase {
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
'Auth' => array('setting' => 'val', 'otherVal'),
);
$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
$this->assertEqual($expected, $Controller->components, 'Components are unexpected.');
}
/**