From 8492f2055bc6ed31fdc11547db72b74597a938f0 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sat, 27 Nov 2010 12:09:43 -0800 Subject: [PATCH] Added tests for object collection --- .../cases/libs/object_collection.test.php | 278 ++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 cake/tests/cases/libs/object_collection.test.php diff --git a/cake/tests/cases/libs/object_collection.test.php b/cake/tests/cases/libs/object_collection.test.php new file mode 100644 index 000000000..9d0861861 --- /dev/null +++ b/cake/tests/cases/libs/object_collection.test.php @@ -0,0 +1,278 @@ +_loaded[$name])) { + return $this->_loaded[$name]; + } + $objectClass = $name . 'GenericObject'; + $this->_loaded[$name] = new $objectClass($this, $settings); + if ($enable === true) { + $this->_enabled[] = $name; + } + return $this->_loaded[$name]; + } +} + +class ObjectCollectionTest extends CakeTestCase { +/** + * setup + * + * @return void + */ + function setup() { + $this->Objects = new GenericObjectCollection(); + } + +/** + * teardown + * + * @return void + */ + function teardown() { + unset($this->Objects); + } + +/** + * test triggering callbacks on loaded helpers + * + * @return void + */ + function testLoad() { + $result = $this->Objects->load('First'); + $this->assertType('FirstGenericObject', $result); + $this->assertType('FirstGenericObject', $this->Objects->First); + + $result = $this->Objects->attached(); + $this->assertEquals(array('First'), $result, 'attached() results are wrong.'); + + $this->assertTrue($this->Objects->enabled('First')); + + $result = $this->Objects->load('First'); + $this->assertSame($result, $this->Objects->First); + } + +/** + * test unload() + * + * @return void + */ + function testUnload() { + $this->Objects->load('First'); + $this->Objects->load('Second'); + + $result = $this->Objects->attached(); + $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong'); + + $this->Objects->unload('First'); + $this->assertFalse(isset($this->Objects->First)); + $this->assertTrue(isset($this->Objects->Second)); + + $result = $this->Objects->attached(); + $this->assertEquals(array('Second'), $result, 'loaded objects are wrong'); + + $result = $this->Objects->enabled(); + $this->assertEquals(array('Second'), $result, 'enabled objects are wrong'); + } + +/** + * creates mock classes for testing + * + * @return void + */ + protected function _makeMockClasses() { + if (!class_exists('TriggerMockFirstGenericObject')) { + $this->getMock('FirstGenericObject', array(), array(), 'TriggerMockFirstGenericObject', false); + } + if (!class_exists('TriggerMockSecondGenericObject')) { + $this->getMock('SecondGenericObject', array(), array(), 'TriggerMockSecondGenericObject', false); + } + } + +/** + * test triggering callbacks. + * + * @return void + */ + function testTrigger() { + $this->_makeMockClasses(); + $this->Objects->load('TriggerMockFirst'); + $this->Objects->load('TriggerMockSecond'); + + $this->Objects->TriggerMockFirst->expects($this->once()) + ->method('callback'); + $this->Objects->TriggerMockSecond->expects($this->once()) + ->method('callback'); + + $this->assertTrue($this->Objects->trigger('callback')); + } + +/** + * test that the initalize callback is triggered on all components even those that are disabled. + * + * @return void + */ + function testTriggerWithTriggerDisabledObjects() { + $this->_makeMockClasses(); + $this->Objects->load('TriggerMockFirst', array(), false); + $this->Objects->load('TriggerMockSecond'); + + $this->Objects->TriggerMockFirst->expects($this->once()) + ->method('callback'); + $this->Objects->TriggerMockSecond->expects($this->once()) + ->method('callback'); + + $result = $this->Objects->trigger('callback', array(), array('triggerDisabled' => true)); + $this->assertTrue($result); + } + +/** + * test trigger and disabled helpers. + * + * @return void + */ + function testTriggerWithDisabledComponents() { + $this->_makeMockClasses(); + $this->Objects->load('TriggerMockFirst'); + $this->Objects->load('TriggerMockSecond'); + + $this->Objects->TriggerMockFirst->expects($this->once()) + ->method('callback'); + $this->Objects->TriggerMockSecond->expects($this->never()) + ->method('callback'); + + $this->Objects->disable('TriggerMockSecond'); + + $this->assertTrue($this->Objects->trigger('callback', array())); + } + +/** + * test that the collectReturn option works. + * + * @return void + */ + function testTriggerWithCollectReturn() { + $this->_makeMockClasses(); + $this->Objects->load('TriggerMockFirst'); + $this->Objects->load('TriggerMockSecond'); + + $this->Objects->TriggerMockFirst->expects($this->once()) + ->method('callback') + ->will($this->returnValue(array('one', 'two'))); + $this->Objects->TriggerMockSecond->expects($this->once()) + ->method('callback') + ->will($this->returnValue(array('three', 'four'))); + + $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true)); + $expected = array( + array('one', 'two'), + array('three', 'four') + ); + $this->assertEquals($expected, $result); + } + +/** + * test that trigger with break & breakOn works. + * + * @return void + */ + function testTriggerWithBreak() { + $this->_makeMockClasses(); + $this->Objects->load('TriggerMockFirst'); + $this->Objects->load('TriggerMockSecond'); + + $this->Objects->TriggerMockFirst->expects($this->once()) + ->method('callback') + ->will($this->returnValue(false)); + $this->Objects->TriggerMockSecond->expects($this->never()) + ->method('callback'); + + $result = $this->Objects->trigger( + 'callback', + array(&$controller), + array('break' => true, 'breakOn' => false) + ); + $this->assertFalse($result); + } + +/** + * test normalizeObjectArray + * + * @return void + */ + function testnormalizeObjectArray() { + $components = array( + 'Html', + 'Foo.Bar' => array('one', 'two'), + 'Something', + 'Banana.Apple' => array('foo' => 'bar') + ); + $result = ComponentCollection::normalizeObjectArray($components); + $expected = array( + 'Html' => array('class' => 'Html', 'settings' => array()), + 'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')), + 'Something' => array('class' => 'Something', 'settings' => array()), + 'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')), + ); + $this->assertEquals($expected, $result); + } + +} \ No newline at end of file