Fixing more strict errors and usage errors in BehaviorCollection test.

Making modParams only work if the result is an array.  This is for compatibility with previous behaviour.
This commit is contained in:
mark_story 2010-12-12 17:41:57 -05:00
parent ce2e6053e4
commit bf22af6b7f
3 changed files with 30 additions and 38 deletions

View file

@ -107,18 +107,17 @@ abstract class ObjectCollection {
if ($options['modParams'] !== false && !isset($params[$options['modParams']])) { if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
throw new CakeException(__('Cannot use modParams with indexes that do not exist.')); throw new CakeException(__('Cannot use modParams with indexes that do not exist.'));
} }
foreach ($list as $name) { foreach ($list as $name) {
$result = call_user_func_array(array($this->_loaded[$name], $callback), $params); $result = call_user_func_array(array($this->_loaded[$name], $callback), $params);
if ($options['collectReturn'] === true) { if ($options['collectReturn'] === true) {
$collected[] = $result; $collected[] = $result;
} }
if ( if (
$options['break'] && ($result === $options['breakOn'] || $options['break'] && ($result === $options['breakOn'] ||
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true))) (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
) { ) {
break; return $result;
} elseif ($options['modParams'] !== false && $result !== null) { } elseif ($options['modParams'] !== false && is_array($result)) {
$params[$options['modParams']] = $result; $params[$options['modParams']] = $result;
} }
} }

View file

@ -46,7 +46,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function setup(&$model, $config = array()) { function setup($model, $config = array()) {
parent::setup($model, $config); parent::setup($model, $config);
if (isset($config['mangle'])) { if (isset($config['mangle'])) {
$config['mangle'] .= ' mangled'; $config['mangle'] .= ' mangled';
@ -62,7 +62,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function beforeFind(&$model, $query) { function beforeFind($model, $query) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['beforeFind']) || $settings['beforeFind'] == 'off') { if (!isset($settings['beforeFind']) || $settings['beforeFind'] == 'off') {
return parent::beforeFind($model, $query); return parent::beforeFind($model, $query);
@ -91,7 +91,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function afterFind(&$model, $results, $primary) { function afterFind($model, $results, $primary) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['afterFind']) || $settings['afterFind'] == 'off') { if (!isset($settings['afterFind']) || $settings['afterFind'] == 'off') {
return parent::afterFind($model, $results, $primary); return parent::afterFind($model, $results, $primary);
@ -119,7 +119,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function beforeSave(&$model) { function beforeSave($model) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['beforeSave']) || $settings['beforeSave'] == 'off') { if (!isset($settings['beforeSave']) || $settings['beforeSave'] == 'off') {
return parent::beforeSave($model); return parent::beforeSave($model);
@ -129,7 +129,7 @@ class TestBehavior extends ModelBehavior {
return false; return false;
break; break;
case 'test': case 'test':
return null; return true;
break; break;
case 'modify': case 'modify':
$model->data[$model->alias]['name'] .= ' modified before'; $model->data[$model->alias]['name'] .= ' modified before';
@ -146,7 +146,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function afterSave(&$model, $created) { function afterSave($model, $created) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['afterSave']) || $settings['afterSave'] == 'off') { if (!isset($settings['afterSave']) || $settings['afterSave'] == 'off') {
return parent::afterSave($model, $created); return parent::afterSave($model, $created);
@ -178,7 +178,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function beforeValidate(&$model) { function beforeValidate($model) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['validate']) || $settings['validate'] == 'off') { if (!isset($settings['validate']) || $settings['validate'] == 'off') {
return parent::beforeValidate($model); return parent::beforeValidate($model);
@ -210,7 +210,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function beforeDelete(&$model, $cascade = true) { function beforeDelete($model, $cascade = true) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['beforeDelete']) || $settings['beforeDelete'] == 'off') { if (!isset($settings['beforeDelete']) || $settings['beforeDelete'] == 'off') {
return parent::beforeDelete($model, $cascade); return parent::beforeDelete($model, $cascade);
@ -227,6 +227,7 @@ class TestBehavior extends ModelBehavior {
if ($cascade) { if ($cascade) {
echo ' (cascading) '; echo ' (cascading) ';
} }
return true;
break; break;
} }
} }
@ -238,7 +239,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function afterDelete(&$model) { function afterDelete($model) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['afterDelete']) || $settings['afterDelete'] == 'off') { if (!isset($settings['afterDelete']) || $settings['afterDelete'] == 'off') {
return parent::afterDelete($model); return parent::afterDelete($model);
@ -257,10 +258,10 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function onError(&$model) { function onError($model, $error) {
$settings = $this->settings[$model->alias]; $settings = $this->settings[$model->alias];
if (!isset($settings['onError']) || $settings['onError'] == 'off') { if (!isset($settings['onError']) || $settings['onError'] == 'off') {
return parent::onError($model, $cascade); return parent::onError($model, $error);
} }
echo "onError trigger success"; echo "onError trigger success";
} }
@ -271,7 +272,7 @@ class TestBehavior extends ModelBehavior {
* @access public * @access public
* @return void * @return void
*/ */
function beforeTest(&$model) { function beforeTest($model) {
if (!isset($model->beforeTestResult)) { if (!isset($model->beforeTestResult)) {
$model->beforeTestResult = array(); $model->beforeTestResult = array();
} }
@ -361,7 +362,7 @@ class Test3Behavior extends TestBehavior{
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Test4Behavior extends ModelBehavior{ class Test4Behavior extends ModelBehavior{
function setup(&$model, $config = null) { function setup($model, $config = null) {
$model->bindModel( $model->bindModel(
array('hasMany' => array('Comment')) array('hasMany' => array('Comment'))
); );
@ -375,7 +376,7 @@ class Test4Behavior extends ModelBehavior{
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Test5Behavior extends ModelBehavior{ class Test5Behavior extends ModelBehavior{
function setup(&$model, $config = null) { function setup($model, $config = null) {
$model->bindModel( $model->bindModel(
array('belongsTo' => array('User')) array('belongsTo' => array('User'))
); );
@ -389,7 +390,7 @@ class Test5Behavior extends ModelBehavior{
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Test6Behavior extends ModelBehavior{ class Test6Behavior extends ModelBehavior{
function setup(&$model, $config = null) { function setup($model, $config = null) {
$model->bindModel( $model->bindModel(
array('hasAndBelongsToMany' => array('Tag')) array('hasAndBelongsToMany' => array('Tag'))
); );
@ -403,7 +404,7 @@ class Test6Behavior extends ModelBehavior{
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Test7Behavior extends ModelBehavior{ class Test7Behavior extends ModelBehavior{
function setup(&$model, $config = null) { function setup($model, $config = null) {
$model->bindModel( $model->bindModel(
array('hasOne' => array('Attachment')) array('hasOne' => array('Attachment'))
); );
@ -429,16 +430,6 @@ class BehaviorCollectionTest extends CakeTestCase {
'core.attachment', 'core.tag', 'core.articles_tag' 'core.attachment', 'core.tag', 'core.articles_tag'
); );
/**
* tearDown method
*
* @access public
* @return void
*/
function endTest() {
ClassRegistry::flush();
}
/** /**
* testBehaviorBinding method * testBehaviorBinding method
* *
@ -937,7 +928,8 @@ class BehaviorCollectionTest extends CakeTestCase {
$this->assertIdentical(trim(ob_get_clean()), 'afterDelete success'); $this->assertIdentical(trim(ob_get_clean()), 'afterDelete success');
$this->assertIdentical($results, true); $this->assertIdentical($results, true);
} }
/**
/**
* testBehaviorOnErrorCallback method * testBehaviorOnErrorCallback method
* *
* @access public * @access public
@ -948,10 +940,11 @@ class BehaviorCollectionTest extends CakeTestCase {
$Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on')); $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
ob_start(); ob_start();
$Apple->Behaviors->Test->onError($Apple); $Apple->Behaviors->Test->onError($Apple, '');
$this->assertIdentical(trim(ob_get_clean()), 'onError trigger success'); $this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
} }
/**
/**
* testBehaviorValidateCallback method * testBehaviorValidateCallback method
* *
* @access public * @access public
@ -1055,17 +1048,17 @@ class BehaviorCollectionTest extends CakeTestCase {
$Apple->Behaviors->attach('Test3'); $Apple->Behaviors->attach('Test3');
$Apple->beforeTestResult = array(); $Apple->beforeTestResult = array();
$Apple->Behaviors->trigger($Apple, 'beforeTest'); $Apple->Behaviors->trigger('beforeTest', array(&$Apple));
$expected = array('testbehavior', 'test2behavior', 'test3behavior'); $expected = array('testbehavior', 'test2behavior', 'test3behavior');
$this->assertIdentical($Apple->beforeTestResult, $expected); $this->assertIdentical($Apple->beforeTestResult, $expected);
$Apple->beforeTestResult = array(); $Apple->beforeTestResult = array();
$Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => 'test2behavior')); $Apple->Behaviors->trigger('beforeTest', array(&$Apple), array('break' => true, 'breakOn' => 'test2behavior'));
$expected = array('testbehavior', 'test2behavior'); $expected = array('testbehavior', 'test2behavior');
$this->assertIdentical($Apple->beforeTestResult, $expected); $this->assertIdentical($Apple->beforeTestResult, $expected);
$Apple->beforeTestResult = array(); $Apple->beforeTestResult = array();
$Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => array('test2behavior', 'test3behavior'))); $Apple->Behaviors->trigger('beforeTest', array($Apple), array('break' => true, 'breakOn' => array('test2behavior', 'test3behavior')));
$expected = array('testbehavior', 'test2behavior'); $expected = array('testbehavior', 'test2behavior');
$this->assertIdentical($Apple->beforeTestResult, $expected); $this->assertIdentical($Apple->beforeTestResult, $expected);
} }
@ -1130,6 +1123,6 @@ class BehaviorCollectionTest extends CakeTestCase {
$Sample->Behaviors->attach('Test2'); $Sample->Behaviors->attach('Test2');
$Sample->Behaviors->detach('Test3'); $Sample->Behaviors->detach('Test3');
$Sample->Behaviors->trigger($Sample, 'beforeTest'); $Sample->Behaviors->trigger('beforeTest', array(&$Sample));
} }
} }

View file

@ -334,7 +334,7 @@ class ObjectCollectionTest extends CakeTestCase {
$this->Objects->TriggerMockSecond->expects($this->once()) $this->Objects->TriggerMockSecond->expects($this->once())
->method('callback') ->method('callback')
->with(array('value')) ->with(array('value'))
->will($this->returnValue('new value')); ->will($this->returnValue(array('new value')));
$result = $this->Objects->trigger( $result = $this->Objects->trigger(
'callback', 'callback',