2010-11-27 20:09:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* ObjectCollectionTest file
|
2010-11-27 20:09:43 +00:00
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-27 20:09:43 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2010-11-27 20:09:43 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('ObjectCollection', 'Utility');
|
2011-12-26 03:47:08 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
2010-11-27 20:09:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A generic object class
|
|
|
|
*/
|
|
|
|
class GenericObject {
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param GenericObjectCollection $collection
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
|
|
|
public function __construct(GenericObjectCollection $collection, $settings = array()) {
|
|
|
|
$this->_Collection = $collection;
|
|
|
|
$this->settings = $settings;
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* First Extension of Generic Object
|
|
|
|
*/
|
|
|
|
class FirstGenericObject extends GenericObject {
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
/**
|
|
|
|
* A generic callback
|
|
|
|
*/
|
|
|
|
public function callback() {
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Second Extension of Generic Object
|
|
|
|
*/
|
|
|
|
class SecondGenericObject extends GenericObject {
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
public function callback() {
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* Third Extension of Generic Object
|
|
|
|
*/
|
|
|
|
class ThirdGenericObject extends GenericObject {
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
public function callback() {
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
/**
|
|
|
|
* A collection of Generic objects
|
|
|
|
*/
|
|
|
|
class GenericObjectCollection extends ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a generic object
|
|
|
|
*
|
|
|
|
* @param string $object Object name
|
|
|
|
* @param array $settings Settings array
|
|
|
|
* @return array List of loaded objects
|
|
|
|
*/
|
2011-01-19 00:33:05 +00:00
|
|
|
public function load($object, $settings = array()) {
|
2010-11-27 20:09:43 +00:00
|
|
|
list($plugin, $name) = pluginSplit($object);
|
|
|
|
if (isset($this->_loaded[$name])) {
|
|
|
|
return $this->_loaded[$name];
|
|
|
|
}
|
|
|
|
$objectClass = $name . 'GenericObject';
|
|
|
|
$this->_loaded[$name] = new $objectClass($this, $settings);
|
2011-01-19 00:33:05 +00:00
|
|
|
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
|
2010-11-27 20:09:43 +00:00
|
|
|
if ($enable === true) {
|
2011-11-06 22:00:17 +00:00
|
|
|
$this->enable($name);
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
return $this->_loaded[$name];
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ObjectCollectionTest extends CakeTestCase {
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* setUp
|
2010-11-27 20:09:43 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->Objects = new GenericObjectCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-11-27 20:09:43 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2012-04-16 02:20:34 +00:00
|
|
|
unset($this->Objects);
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test triggering callbacks on loaded helpers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoad() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$result = $this->Objects->load('First');
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInstanceOf('FirstGenericObject', $result);
|
|
|
|
$this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
|
2010-11-27 20:09:43 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnload() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Tests set()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSet() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Objects->load('First');
|
|
|
|
|
|
|
|
$result = $this->Objects->attached();
|
|
|
|
$this->assertEquals(array('First'), $result, 'loaded objects are wrong');
|
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
$result = $this->Objects->set('First', new SecondGenericObject($this->Objects));
|
2011-11-16 17:56:34 +00:00
|
|
|
$this->assertInstanceOf('SecondGenericObject', $result['First'], 'set failed');
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
$result = $this->Objects->set('Second', new SecondGenericObject($this->Objects));
|
2011-11-16 17:56:34 +00:00
|
|
|
$this->assertInstanceOf('SecondGenericObject', $result['Second'], 'set failed');
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(2, count($result));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2011-11-06 22:00:17 +00:00
|
|
|
if (!class_exists('TriggerMockThirdGenericObject')) {
|
|
|
|
$this->getMock('ThirdGenericObject', array(), array(), 'TriggerMockThirdGenericObject', false);
|
|
|
|
}
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test triggering callbacks.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTrigger() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
2010-12-12 19:10:22 +00:00
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue(true));
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->Objects->TriggerMockSecond->expects($this->once())
|
2010-12-12 19:10:22 +00:00
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue(true));
|
2010-11-27 20:09:43 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->Objects->trigger('callback'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-15 04:26:24 +00:00
|
|
|
* test trigger and disabled objects
|
2010-11-27 20:09:43 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerWithDisabledObjects() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
2010-12-12 19:10:22 +00:00
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue(true));
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->Objects->TriggerMockSecond->expects($this->never())
|
2010-12-12 19:10:22 +00:00
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue(true));
|
2010-11-27 20:09:43 +00:00
|
|
|
|
|
|
|
$this->Objects->disable('TriggerMockSecond');
|
|
|
|
|
|
|
|
$this->assertTrue($this->Objects->trigger('callback', array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that the collectReturn option works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerWithCollectReturn() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
$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')));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
$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
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerWithBreak() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
$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',
|
2010-12-12 19:10:22 +00:00
|
|
|
array(),
|
2010-11-27 20:09:43 +00:00
|
|
|
array('break' => true, 'breakOn' => false)
|
|
|
|
);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2010-12-12 19:10:22 +00:00
|
|
|
/**
|
|
|
|
* test that trigger with modParams works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerWithModParams() {
|
2010-12-12 19:10:22 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-12-12 19:10:22 +00:00
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with(array('value'))
|
|
|
|
->will($this->returnValue(array('new value')));
|
|
|
|
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with(array('new value'))
|
|
|
|
->will($this->returnValue(array('newer value')));
|
|
|
|
|
|
|
|
$result = $this->Objects->trigger(
|
|
|
|
'callback',
|
|
|
|
array(array('value')),
|
|
|
|
array('modParams' => 0)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('newer value'), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that setting modParams to an index that doesn't exist doesn't cause errors.
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerModParamsInvalidIndex() {
|
2010-12-12 19:10:22 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
2010-12-12 19:10:22 +00:00
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->Objects->TriggerMockFirst->expects($this->never())
|
|
|
|
->method('callback');
|
|
|
|
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->never())
|
|
|
|
->method('callback');
|
2010-12-12 19:10:22 +00:00
|
|
|
|
|
|
|
$result = $this->Objects->trigger(
|
|
|
|
'callback',
|
|
|
|
array(array('value')),
|
|
|
|
array('modParams' => 2)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-12-12 20:33:24 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test that returning null doesn't modify parameters.
|
2010-12-12 20:33:24 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTriggerModParamsNullIgnored() {
|
2010-12-12 20:33:24 +00:00
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
2010-12-12 20:33:24 +00:00
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with(array('value'))
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with(array('value'))
|
2010-12-12 22:41:57 +00:00
|
|
|
->will($this->returnValue(array('new value')));
|
2010-12-12 20:33:24 +00:00
|
|
|
|
|
|
|
$result = $this->Objects->trigger(
|
|
|
|
'callback',
|
|
|
|
array(array('value')),
|
2011-01-03 21:07:29 +00:00
|
|
|
array('modParams' => 0)
|
2010-12-12 20:33:24 +00:00
|
|
|
);
|
2011-01-03 21:07:29 +00:00
|
|
|
$this->assertEquals(array('new value'), $result);
|
2010-12-12 20:33:24 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test order of callbacks triggering based on priority.
|
2011-11-06 22:00:17 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTriggerPriority() {
|
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond', array('priority' => 5));
|
|
|
|
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
|
|
|
$this->Objects->TriggerMockFirst->expects($this->any())
|
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue('1st'));
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->any())
|
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue('2nd'));
|
|
|
|
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->load('TriggerMockThird', array('priority' => 7));
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockThird;
|
|
|
|
$this->Objects->TriggerMockThird->expects($this->any())
|
|
|
|
->method('callback')
|
|
|
|
->will($this->returnValue('3rd'));
|
|
|
|
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'3rd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->disable('TriggerMockFirst');
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'3rd'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->enable('TriggerMockFirst');
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'3rd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->disable('TriggerMockThird');
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->enable('TriggerMockThird', false);
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st',
|
|
|
|
'3rd'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->setPriority('TriggerMockThird', 1);
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'3rd',
|
|
|
|
'2nd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->disable('TriggerMockThird');
|
|
|
|
$this->Objects->setPriority('TriggerMockThird', 11);
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->enable('TriggerMockThird');
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st',
|
|
|
|
'3rd'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->Objects->setPriority('TriggerMockThird');
|
|
|
|
$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
|
|
|
|
$expected = array(
|
|
|
|
'2nd',
|
|
|
|
'1st',
|
|
|
|
'3rd'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2010-11-27 20:09:43 +00:00
|
|
|
/**
|
|
|
|
* test normalizeObjectArray
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testnormalizeObjectArray() {
|
2010-11-27 20:09:43 +00:00
|
|
|
$components = array(
|
|
|
|
'Html',
|
|
|
|
'Foo.Bar' => array('one', 'two'),
|
|
|
|
'Something',
|
|
|
|
'Banana.Apple' => array('foo' => 'bar')
|
|
|
|
);
|
2010-12-12 19:10:22 +00:00
|
|
|
$result = ObjectCollection::normalizeObjectArray($components);
|
2010-11-27 20:09:43 +00:00
|
|
|
$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);
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2011-10-15 18:52:41 +00:00
|
|
|
// This is the result after Controller::_mergeVars
|
|
|
|
$components = array(
|
|
|
|
'Html' => null,
|
|
|
|
'Foo.Bar' => array('one', 'two'),
|
|
|
|
'Something' => null,
|
|
|
|
'Banana.Apple' => array('foo' => 'bar')
|
|
|
|
);
|
|
|
|
$result = ObjectCollection::normalizeObjectArray($components);
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-11-27 20:09:43 +00:00
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
2011-12-26 03:47:08 +00:00
|
|
|
/**
|
|
|
|
* tests that passing an instance of CakeEvent to trigger will prepend the subject to the list of arguments
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDispatchEventWithSubject() {
|
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
|
|
|
$subjectClass = new Object();
|
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with($subjectClass, 'first argument')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with($subjectClass, 'first argument')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$event = new CakeEvent('callback', $subjectClass, array('first argument'));
|
|
|
|
$this->assertTrue($this->Objects->trigger($event));
|
|
|
|
}
|
2010-11-27 20:09:43 +00:00
|
|
|
|
2011-12-26 03:47:08 +00:00
|
|
|
/**
|
|
|
|
* tests that passing an instance of CakeEvent to trigger with omitSubject property
|
|
|
|
* will NOT prepend the subject to the list of arguments
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDispatchEventNoSubject() {
|
|
|
|
$this->_makeMockClasses();
|
|
|
|
$this->Objects->load('TriggerMockFirst');
|
|
|
|
$this->Objects->load('TriggerMockSecond');
|
|
|
|
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockFirst;
|
|
|
|
$this->mockObjects[] = $this->Objects->TriggerMockSecond;
|
|
|
|
|
|
|
|
$subjectClass = new Object();
|
|
|
|
$this->Objects->TriggerMockFirst->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with('first argument')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->Objects->TriggerMockSecond->expects($this->once())
|
|
|
|
->method('callback')
|
|
|
|
->with('first argument')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$event = new CakeEvent('callback', $subjectClass, array('first argument'));
|
|
|
|
$event->omitSubject = true;
|
|
|
|
$this->assertTrue($this->Objects->trigger($event));
|
|
|
|
}
|
2012-03-14 02:59:20 +00:00
|
|
|
|
|
|
|
}
|