Merge pull request #961 from dereuromark/2.3-unify-object-collection

2.3 unify object collection
This commit is contained in:
José Lorenzo Rodríguez 2012-12-22 14:24:26 -08:00
commit a8bd7c6678
10 changed files with 69 additions and 56 deletions

View file

@ -208,7 +208,7 @@ class CakeLog {
if (empty(self::$_Collection)) { if (empty(self::$_Collection)) {
self::_init(); self::_init();
} }
return self::$_Collection->attached(); return self::$_Collection->loaded();
} }
/** /**

View file

@ -54,8 +54,8 @@ class TaskCollectionTest extends CakeTestCase {
$this->assertInstanceOf('DbConfigTask', $result); $this->assertInstanceOf('DbConfigTask', $result);
$this->assertInstanceOf('DbConfigTask', $this->Tasks->DbConfig); $this->assertInstanceOf('DbConfigTask', $this->Tasks->DbConfig);
$result = $this->Tasks->attached(); $result = $this->Tasks->loaded();
$this->assertEquals(array('DbConfig'), $result, 'attached() results are wrong.'); $this->assertEquals(array('DbConfig'), $result, 'loaded() results are wrong.');
} }
/** /**
@ -110,14 +110,14 @@ class TaskCollectionTest extends CakeTestCase {
$this->Tasks->load('Extract'); $this->Tasks->load('Extract');
$this->Tasks->load('DbConfig'); $this->Tasks->load('DbConfig');
$result = $this->Tasks->attached(); $result = $this->Tasks->loaded();
$this->assertEquals(array('Extract', 'DbConfig'), $result, 'loaded tasks is wrong'); $this->assertEquals(array('Extract', 'DbConfig'), $result, 'loaded tasks is wrong');
$this->Tasks->unload('DbConfig'); $this->Tasks->unload('DbConfig');
$this->assertFalse(isset($this->Tasks->DbConfig)); $this->assertFalse(isset($this->Tasks->DbConfig));
$this->assertTrue(isset($this->Tasks->Extract)); $this->assertTrue(isset($this->Tasks->Extract));
$result = $this->Tasks->attached(); $result = $this->Tasks->loaded();
$this->assertEquals(array('Extract'), $result, 'loaded tasks is wrong'); $this->assertEquals(array('Extract'), $result, 'loaded tasks is wrong');
} }

View file

@ -59,8 +59,8 @@ class ComponentCollectionTest extends CakeTestCase {
$this->assertInstanceOf('CookieComponent', $result); $this->assertInstanceOf('CookieComponent', $result);
$this->assertInstanceOf('CookieComponent', $this->Components->Cookie); $this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
$result = $this->Components->attached(); $result = $this->Components->loaded();
$this->assertEquals(array('Cookie'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
$this->assertTrue($this->Components->enabled('Cookie')); $this->assertTrue($this->Components->enabled('Cookie'));
@ -79,8 +79,8 @@ class ComponentCollectionTest extends CakeTestCase {
$this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie); $this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie);
$this->assertTrue($this->Components->Cookie->settings['somesetting']); $this->assertTrue($this->Components->Cookie->settings['somesetting']);
$result = $this->Components->attached(); $result = $this->Components->loaded();
$this->assertEquals(array('Cookie'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
$this->assertTrue($this->Components->enabled('Cookie')); $this->assertTrue($this->Components->enabled('Cookie'));
@ -93,8 +93,8 @@ class ComponentCollectionTest extends CakeTestCase {
$this->assertInstanceOf('OtherComponent', $result); $this->assertInstanceOf('OtherComponent', $result);
$this->assertInstanceOf('OtherComponent', $this->Components->SomeOther); $this->assertInstanceOf('OtherComponent', $this->Components->SomeOther);
$result = $this->Components->attached(); $result = $this->Components->loaded();
$this->assertEquals(array('Cookie', 'SomeOther'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Cookie', 'SomeOther'), $result, 'loaded() results are wrong.');
App::build(); App::build();
CakePlugin::unload(); CakePlugin::unload();
} }
@ -148,14 +148,14 @@ class ComponentCollectionTest extends CakeTestCase {
$this->Components->load('Cookie'); $this->Components->load('Cookie');
$this->Components->load('Security'); $this->Components->load('Security');
$result = $this->Components->attached(); $result = $this->Components->loaded();
$this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong'); $this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong');
$this->Components->unload('Cookie'); $this->Components->unload('Cookie');
$this->assertFalse(isset($this->Components->Cookie)); $this->assertFalse(isset($this->Components->Cookie));
$this->assertTrue(isset($this->Components->Security)); $this->assertTrue(isset($this->Components->Security));
$result = $this->Components->attached(); $result = $this->Components->loaded();
$this->assertEquals(array('Security'), $result, 'loaded components is wrong'); $this->assertEquals(array('Security'), $result, 'loaded components is wrong');
$result = $this->Components->enabled(); $result = $this->Components->enabled();

View file

@ -440,10 +440,10 @@ class BehaviorCollectionTest extends CakeTestCase {
*/ */
public function testLoadDisabled() { public function testLoadDisabled() {
$Apple = new Apple(); $Apple = new Apple();
$this->assertSame(array(), $Apple->Behaviors->attached()); $this->assertSame(array(), $Apple->Behaviors->loaded());
$Apple->Behaviors->load('Translate', array('enabled' => false)); $Apple->Behaviors->load('Translate', array('enabled' => false));
$this->assertTrue($Apple->Behaviors->attached('Translate')); $this->assertTrue($Apple->Behaviors->loaded('Translate'));
$this->assertFalse($Apple->Behaviors->enabled('Translate')); $this->assertFalse($Apple->Behaviors->enabled('Translate'));
} }
@ -452,10 +452,10 @@ class BehaviorCollectionTest extends CakeTestCase {
*/ */
public function testLoadAlias() { public function testLoadAlias() {
$Apple = new Apple(); $Apple = new Apple();
$this->assertSame(array(), $Apple->Behaviors->attached()); $this->assertSame(array(), $Apple->Behaviors->loaded());
$Apple->Behaviors->load('Test', array('className' => 'TestAlias', 'somesetting' => true)); $Apple->Behaviors->load('Test', array('className' => 'TestAlias', 'somesetting' => true));
$this->assertSame(array('Test'), $Apple->Behaviors->attached()); $this->assertSame(array('Test'), $Apple->Behaviors->loaded());
$this->assertInstanceOf('TestAliasBehavior', $Apple->Behaviors->Test); $this->assertInstanceOf('TestAliasBehavior', $Apple->Behaviors->Test);
$this->assertTrue($Apple->Behaviors->Test->settings['Apple']['somesetting']); $this->assertTrue($Apple->Behaviors->Test->settings['Apple']['somesetting']);
@ -468,8 +468,8 @@ class BehaviorCollectionTest extends CakeTestCase {
$this->assertTrue($Apple->Behaviors->load('SomeOther', array('className' => 'TestPlugin.TestPluginPersisterOne'))); $this->assertTrue($Apple->Behaviors->load('SomeOther', array('className' => 'TestPlugin.TestPluginPersisterOne')));
$this->assertInstanceOf('TestPluginPersisterOneBehavior', $Apple->Behaviors->SomeOther); $this->assertInstanceOf('TestPluginPersisterOneBehavior', $Apple->Behaviors->SomeOther);
$result = $Apple->Behaviors->attached(); $result = $Apple->Behaviors->loaded();
$this->assertEquals(array('Test', 'SomeOther'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Test', 'SomeOther'), $result, 'loaded() results are wrong.');
App::build(); App::build();
CakePlugin::unload(); CakePlugin::unload();
} }
@ -481,18 +481,18 @@ class BehaviorCollectionTest extends CakeTestCase {
*/ */
public function testBehaviorBinding() { public function testBehaviorBinding() {
$Apple = new Apple(); $Apple = new Apple();
$this->assertSame(array(), $Apple->Behaviors->attached()); $this->assertSame(array(), $Apple->Behaviors->loaded());
$Apple->Behaviors->attach('Test', array('key' => 'value')); $Apple->Behaviors->attach('Test', array('key' => 'value'));
$this->assertSame(array('Test'), $Apple->Behaviors->attached()); $this->assertSame(array('Test'), $Apple->Behaviors->loaded());
$this->assertEquals('testbehavior', strtolower(get_class($Apple->Behaviors->Test))); $this->assertEquals('testbehavior', strtolower(get_class($Apple->Behaviors->Test)));
$expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value'); $expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value');
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
$this->assertEquals(array('Apple'), array_keys($Apple->Behaviors->Test->settings)); $this->assertEquals(array('Apple'), array_keys($Apple->Behaviors->Test->settings));
$this->assertSame($Apple->Sample->Behaviors->attached(), array()); $this->assertSame($Apple->Sample->Behaviors->loaded(), array());
$Apple->Sample->Behaviors->attach('Test', array('key2' => 'value2')); $Apple->Sample->Behaviors->attach('Test', array('key2' => 'value2'));
$this->assertSame($Apple->Sample->Behaviors->attached(), array('Test')); $this->assertSame($Apple->Sample->Behaviors->loaded(), array('Test'));
$this->assertEquals(array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'), $Apple->Sample->Behaviors->Test->settings['Sample']); $this->assertEquals(array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'), $Apple->Sample->Behaviors->Test->settings['Sample']);
$this->assertEquals(array('Apple', 'Sample'), array_keys($Apple->Behaviors->Test->settings)); $this->assertEquals(array('Apple', 'Sample'), array_keys($Apple->Behaviors->Test->settings));
@ -548,17 +548,17 @@ class BehaviorCollectionTest extends CakeTestCase {
$Apple = new Apple(); $Apple = new Apple();
$Apple->Behaviors->attach('Plugin.Test'); $Apple->Behaviors->attach('Plugin.Test');
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior'); $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
$this->assertEquals(array('Test'), $Apple->Behaviors->attached()); $this->assertEquals(array('Test'), $Apple->Behaviors->loaded());
$Apple->Behaviors->detach('Plugin.Test'); $Apple->Behaviors->detach('Plugin.Test');
$this->assertEquals(array(), $Apple->Behaviors->attached()); $this->assertEquals(array(), $Apple->Behaviors->loaded());
$Apple->Behaviors->attach('Plugin.Test'); $Apple->Behaviors->attach('Plugin.Test');
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior'); $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
$this->assertEquals(array('Test'), $Apple->Behaviors->attached()); $this->assertEquals(array('Test'), $Apple->Behaviors->loaded());
$Apple->Behaviors->detach('Test'); $Apple->Behaviors->detach('Test');
$this->assertEquals(array(), $Apple->Behaviors->attached()); $this->assertEquals(array(), $Apple->Behaviors->loaded());
} }
/** /**
@ -586,7 +586,7 @@ class BehaviorCollectionTest extends CakeTestCase {
$this->assertSame($Apple->Behaviors->enabled(), array('Test')); $this->assertSame($Apple->Behaviors->enabled(), array('Test'));
$Apple->Behaviors->disable('Test'); $Apple->Behaviors->disable('Test');
$this->assertSame(array('Test'), $Apple->Behaviors->attached()); $this->assertSame(array('Test'), $Apple->Behaviors->loaded());
$this->assertSame($Apple->Behaviors->enabled(), array()); $this->assertSame($Apple->Behaviors->enabled(), array());
$Apple->Sample->Behaviors->attach('Test'); $Apple->Sample->Behaviors->attach('Test');
@ -594,7 +594,7 @@ class BehaviorCollectionTest extends CakeTestCase {
$this->assertSame($Apple->Behaviors->enabled(), array()); $this->assertSame($Apple->Behaviors->enabled(), array());
$Apple->Behaviors->enable('Test'); $Apple->Behaviors->enable('Test');
$this->assertSame($Apple->Behaviors->attached('Test'), true); $this->assertSame($Apple->Behaviors->loaded('Test'), true);
$this->assertSame($Apple->Behaviors->enabled(), array('Test')); $this->assertSame($Apple->Behaviors->enabled(), array('Test'));
$Apple->Behaviors->disable('Test'); $Apple->Behaviors->disable('Test');

View file

@ -208,11 +208,11 @@ class ModelIntegrationTest extends BaseModelTest {
public function testDynamicBehaviorAttachment() { public function testDynamicBehaviorAttachment() {
$this->loadFixtures('Apple', 'Sample', 'Author'); $this->loadFixtures('Apple', 'Sample', 'Author');
$TestModel = new Apple(); $TestModel = new Apple();
$this->assertEquals(array(), $TestModel->Behaviors->attached()); $this->assertEquals(array(), $TestModel->Behaviors->loaded());
$TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field')); $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
$this->assertTrue(is_object($TestModel->Behaviors->Tree)); $this->assertTrue(is_object($TestModel->Behaviors->Tree));
$this->assertEquals(array('Tree'), $TestModel->Behaviors->attached()); $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
$expected = array( $expected = array(
'parent' => 'parent_id', 'parent' => 'parent_id',
@ -227,10 +227,10 @@ class ModelIntegrationTest extends BaseModelTest {
$TestModel->Behaviors->attach('Tree', array('enabled' => false)); $TestModel->Behaviors->attach('Tree', array('enabled' => false));
$this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']); $this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']);
$this->assertEquals(array('Tree'), $TestModel->Behaviors->attached()); $this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded());
$TestModel->Behaviors->detach('Tree'); $TestModel->Behaviors->detach('Tree');
$this->assertEquals(array(), $TestModel->Behaviors->attached()); $this->assertEquals(array(), $TestModel->Behaviors->loaded());
$this->assertFalse(isset($TestModel->Behaviors->Tree)); $this->assertFalse(isset($TestModel->Behaviors->Tree));
} }

View file

@ -131,8 +131,8 @@ class ObjectCollectionTest extends CakeTestCase {
$this->assertInstanceOf('FirstGenericObject', $result); $this->assertInstanceOf('FirstGenericObject', $result);
$this->assertInstanceOf('FirstGenericObject', $this->Objects->First); $this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
$result = $this->Objects->attached(); $result = $this->Objects->loaded();
$this->assertEquals(array('First'), $result, 'attached() results are wrong.'); $this->assertEquals(array('First'), $result, 'loaded() results are wrong.');
$this->assertTrue($this->Objects->enabled('First')); $this->assertTrue($this->Objects->enabled('First'));
@ -149,17 +149,17 @@ class ObjectCollectionTest extends CakeTestCase {
$this->Objects->load('First'); $this->Objects->load('First');
$this->Objects->load('Second'); $this->Objects->load('Second');
$result = $this->Objects->attached(); $result = $this->Objects->loaded();
$this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong'); $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
$this->Objects->unload('First'); $this->Objects->unload('First');
$this->assertFalse(isset($this->Objects->First)); $this->assertFalse(isset($this->Objects->First));
$this->assertTrue(isset($this->Objects->Second)); $this->assertTrue(isset($this->Objects->Second));
$result = $this->Objects->attached(); $result = $this->Objects->loaded();
$this->assertEquals(array('Second'), $result, 'loaded objects are wrong'); $this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
$result = $this->Objects->enabled(); $result = $this->Objects->loaded();
$this->assertEquals(array('Second'), $result, 'enabled objects are wrong'); $this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
} }
@ -171,7 +171,7 @@ class ObjectCollectionTest extends CakeTestCase {
public function testSet() { public function testSet() {
$this->Objects->load('First'); $this->Objects->load('First');
$result = $this->Objects->attached(); $result = $this->Objects->loaded();
$this->assertEquals(array('First'), $result, 'loaded objects are wrong'); $this->assertEquals(array('First'), $result, 'loaded objects are wrong');
$result = $this->Objects->set('First', new SecondGenericObject($this->Objects)); $result = $this->Objects->set('First', new SecondGenericObject($this->Objects));

View file

@ -61,8 +61,8 @@ class HelperCollectionTest extends CakeTestCase {
$this->assertInstanceOf('HtmlHelper', $result); $this->assertInstanceOf('HtmlHelper', $result);
$this->assertInstanceOf('HtmlHelper', $this->Helpers->Html); $this->assertInstanceOf('HtmlHelper', $this->Helpers->Html);
$result = $this->Helpers->attached(); $result = $this->Helpers->loaded();
$this->assertEquals(array('Html'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
$this->assertTrue($this->Helpers->enabled('Html')); $this->assertTrue($this->Helpers->enabled('Html'));
} }
@ -106,8 +106,8 @@ class HelperCollectionTest extends CakeTestCase {
$this->assertInstanceOf('HtmlAliasHelper', $result); $this->assertInstanceOf('HtmlAliasHelper', $result);
$this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html); $this->assertInstanceOf('HtmlAliasHelper', $this->Helpers->Html);
$result = $this->Helpers->attached(); $result = $this->Helpers->loaded();
$this->assertEquals(array('Html'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
$this->assertTrue($this->Helpers->enabled('Html')); $this->assertTrue($this->Helpers->enabled('Html'));
@ -120,8 +120,8 @@ class HelperCollectionTest extends CakeTestCase {
$this->assertInstanceOf('OtherHelperHelper', $result); $this->assertInstanceOf('OtherHelperHelper', $result);
$this->assertInstanceOf('OtherHelperHelper', $this->Helpers->SomeOther); $this->assertInstanceOf('OtherHelperHelper', $this->Helpers->SomeOther);
$result = $this->Helpers->attached(); $result = $this->Helpers->loaded();
$this->assertEquals(array('Html', 'SomeOther'), $result, 'attached() results are wrong.'); $this->assertEquals(array('Html', 'SomeOther'), $result, 'loaded() results are wrong.');
App::build(); App::build();
} }
@ -174,14 +174,14 @@ class HelperCollectionTest extends CakeTestCase {
$this->Helpers->load('Form'); $this->Helpers->load('Form');
$this->Helpers->load('Html'); $this->Helpers->load('Html');
$result = $this->Helpers->attached(); $result = $this->Helpers->loaded();
$this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong'); $this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
$this->Helpers->unload('Html'); $this->Helpers->unload('Html');
$this->assertNotContains('Html', $this->Helpers->attached()); $this->assertNotContains('Html', $this->Helpers->loaded());
$this->assertContains('Form', $this->Helpers->attached()); $this->assertContains('Form', $this->Helpers->loaded());
$result = $this->Helpers->attached(); $result = $this->Helpers->loaded();
$this->assertEquals(array('Form'), $result, 'loaded helpers is wrong'); $this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
} }

View file

@ -978,7 +978,7 @@ class ViewTest extends CakeTestCase {
$result = $View->render('index', false); $result = $View->render('index', false);
$this->assertEquals('posts index', $result); $this->assertEquals('posts index', $result);
$attached = $View->Helpers->attached(); $attached = $View->Helpers->loaded();
$this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached); $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
$this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'); $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
@ -987,7 +987,7 @@ class ViewTest extends CakeTestCase {
$result = $View->render('index', false); $result = $View->render('index', false);
$this->assertEquals('posts index', $result); $this->assertEquals('posts index', $result);
$attached = $View->Helpers->attached(); $attached = $View->Helpers->loaded();
$expected = array('Html', 'Form', 'Number', 'PluggedHelper'); $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
$this->assertEquals($expected, $attached, 'Attached helpers are wrong.'); $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
} }

View file

@ -58,7 +58,7 @@ class ControllerTestDispatcher extends Dispatcher {
$this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers); $this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
$this->testController->setRequest($request); $this->testController->setRequest($request);
$this->testController->response = $this->response; $this->testController->response = $this->response;
foreach ($this->testController->Components->attached() as $component) { foreach ($this->testController->Components->loaded() as $component) {
$object = $this->testController->Components->{$component}; $object = $this->testController->Components->{$component};
if (isset($object->response)) { if (isset($object->response)) {
$object->response = $response; $object->response = $response;

View file

@ -267,12 +267,25 @@ abstract class ObjectCollection {
/** /**
* Gets the list of attached objects, or, whether the given object is attached * Gets the list of attached objects, or, whether the given object is attached
* *
* @param string $name Optional. The name of the behavior to check the status of. If omitted, * @param string $name Optional. The name of the object to check the status of. If omitted,
* returns an array of currently-attached behaviors * returns an array of currently-attached objects
* @return mixed If $name is specified, returns the boolean status of the corresponding behavior. * @return mixed If $name is specified, returns the boolean status of the corresponding object.
* Otherwise, returns an array of all attached behaviors. * Otherwise, returns an array of all attached objects.
* @deprecated Use loaded instead.
*/ */
public function attached($name = null) { public function attached($name = null) {
return $this->loaded($name);
}
/**
* Gets the list of loaded objects, or, whether the given object is loaded
*
* @param string $name Optional. The name of the object to check the status of. If omitted,
* returns an array of currently-loaded objects
* @return mixed If $name is specified, returns the boolean status of the corresponding object.
* Otherwise, returns an array of all loaded objects.
*/
public function loaded($name = null) {
if (!empty($name)) { if (!empty($name)) {
return isset($this->_loaded[$name]); return isset($this->_loaded[$name]);
} }