2008-05-30 11:40:08 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* Short description for file .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) Tests < https :// trac . cakephp . org / wiki / Developement / TestSuite >
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc .
* @ link https :// trac . cakephp . org / wiki / Developement / TestSuite CakePHP ( tm ) Tests
* @ package cake . tests
* @ subpackage cake . tests . cases . libs . controller
* @ since CakePHP ( tm ) v 1.2 . 0.5436
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / opengroup . php The Open Group Test Suite License
*/
2008-06-04 02:39:05 +00:00
App :: import ( 'Core' , array ( 'Component' , 'Controller' ));
if ( ! class_exists ( 'AppController' )) {
2008-06-05 15:20:45 +00:00
/**
* AppController class
2008-06-06 16:18:14 +00:00
*
2008-06-10 22:38:05 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-05 15:20:45 +00:00
*/
2008-06-04 02:39:05 +00:00
class AppController extends Controller {
2008-06-05 15:20:45 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var string 'App'
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $name = 'App' ;
2008-06-05 15:20:45 +00:00
/**
* uses property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $uses = array ();
2008-06-05 15:20:45 +00:00
/**
* helpers property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $helpers = array ();
2008-06-05 15:20:45 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $components = array ( 'Orange' => array ( 'colour' => 'blood orange' ));
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
}
} else {
define ( 'AppControllerExists' , true );
}
/**
* ParamTestComponent
*
* @ package cake . tests . cases . libs . controller
*/
class ParamTestComponent extends Object {
2008-06-05 15:20:45 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var string 'ParamTest'
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $name = 'ParamTest' ;
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $components = array ( 'Banana' => array ( 'config' => 'value' ));
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* initialize method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
* @ param mixed $settings
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-06-04 02:39:05 +00:00
function initialize ( & $controller , $settings ) {
foreach ( $settings as $key => $value ) {
if ( is_numeric ( $key )) {
$this -> { $value } = true ;
} else {
$this -> { $key } = $value ;
}
}
}
}
2008-05-30 11:40:08 +00:00
/**
* Short description for class .
*
2008-06-10 22:38:05 +00:00
* @ package cake . tests
* @ subpackage cake . tests . cases . libs . controller
2008-05-30 11:40:08 +00:00
*/
class ComponentTestController extends AppController {
2008-06-02 19:22:55 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var string 'ComponentTest'
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $name = 'ComponentTest' ;
2008-06-02 19:22:55 +00:00
/**
* uses property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-30 11:40:08 +00:00
var $uses = array ();
}
2008-06-02 19:22:55 +00:00
/**
* AppleComponent class
2008-06-06 16:18:14 +00:00
*
2008-06-10 22:38:05 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class AppleComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $components = array ( 'Orange' );
2008-06-02 19:22:55 +00:00
/**
* testName property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var mixed null
* @ access public
*/
2008-06-02 04:57:06 +00:00
var $testName = null ;
2008-06-02 19:22:55 +00:00
/**
* startup method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-31 03:54:22 +00:00
function startup ( & $controller ) {
2008-06-02 04:57:06 +00:00
$this -> testName = $controller -> name ;
2008-05-31 03:54:22 +00:00
}
}
2008-06-02 19:22:55 +00:00
/**
* OrangeComponent class
2008-06-06 16:18:14 +00:00
*
2008-06-10 22:38:05 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class OrangeComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $components = array ( 'Banana' );
2008-06-02 19:22:55 +00:00
/**
* initialize method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-06-04 02:39:05 +00:00
function initialize ( & $controller , $settings ) {
2008-06-02 04:57:06 +00:00
$this -> Banana -> testField = 'OrangeField' ;
2008-06-04 02:39:05 +00:00
$this -> settings = $settings ;
2008-06-02 04:57:06 +00:00
}
2008-05-31 03:54:22 +00:00
}
2008-06-02 19:22:55 +00:00
/**
* BananaComponent class
2008-06-06 16:18:14 +00:00
*
2008-06-10 22:38:05 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class BananaComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* testField property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var string 'BananaField'
* @ access public
*/
2008-06-02 04:57:06 +00:00
var $testField = 'BananaField' ;
2008-05-31 03:54:22 +00:00
}
2008-06-06 16:18:14 +00:00
/**
* MutuallyReferencingOneComponent class
*
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
*/
class MutuallyReferencingOneComponent extends Object {
var $components = array ( 'MutuallyReferencingTwo' );
}
/**
* MutuallyReferencingTwoComponent class
*
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
*/
class MutuallyReferencingTwoComponent extends Object {
var $components = array ( 'MutuallyReferencingOne' );
}
2008-06-02 19:22:55 +00:00
/**
* ComponentTest class
2008-06-06 16:18:14 +00:00
*
2008-06-10 22:38:05 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-30 11:40:08 +00:00
class ComponentTest extends CakeTestCase {
2008-06-02 19:22:55 +00:00
/**
* setUp method
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function setUp () {
Configure :: write ( 'pluginPaths' , array ( TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS ));
}
2008-06-02 19:22:55 +00:00
/**
* testLoadComponents method
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testLoadComponents () {
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'RequestHandler' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> plugin = 'test_plugin' ;
$Controller -> components = array ( 'RequestHandler' , 'TestPluginComponent' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> TestPluginComponent , 'TestPluginComponentComponent' ));
$this -> assertTrue ( is_a ( $Controller -> TestPluginComponent -> TestPluginOtherComponent , 'TestPluginOtherComponentComponent' ));
$this -> assertFalse ( isset ( $Controller -> TestPluginOtherComponent ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'Security' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> Security , 'SecurityComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Security -> Session , 'SessionComponent' ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'Security' , 'Cookie' , 'RequestHandler' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> Security , 'SecurityComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Security -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Cookie , 'CookieComponent' ));
}
2008-06-04 02:39:05 +00:00
/**
* test component loading
*
* @ return void
*/
2008-05-31 03:54:22 +00:00
function testNestedComponentLoading () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Apple' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-05-31 03:54:22 +00:00
$this -> assertTrue ( is_a ( $Controller -> Apple , 'AppleComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Apple -> Orange , 'OrangeComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Apple -> Orange -> Banana , 'BananaComponent' ));
}
2008-06-04 02:39:05 +00:00
/**
* test component :: startup and running all built components startup ()
*
* @ return void
*/
2008-05-31 03:54:22 +00:00
function testComponentStartup () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Apple' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
$Controller -> beforeFilter ();
$Controller -> Component -> startup ( $Controller );
2008-05-31 03:54:22 +00:00
$this -> assertTrue ( is_a ( $Controller -> Apple , 'AppleComponent' ));
2008-06-02 04:57:06 +00:00
$this -> assertEqual ( $Controller -> Apple -> testName , 'ComponentTest' );
}
2008-06-04 02:39:05 +00:00
/**
* test a component being used more than once .
2008-06-06 16:18:14 +00:00
*
2008-06-04 02:39:05 +00:00
* @ return void
*/
2008-06-02 04:57:06 +00:00
function testMultipleComponentInitialize () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Orange' , 'Banana' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-02 04:57:06 +00:00
$this -> assertEqual ( $Controller -> Banana -> testField , 'OrangeField' );
$this -> assertEqual ( $Controller -> Orange -> Banana -> testField , 'OrangeField' );
2008-05-31 03:54:22 +00:00
}
2008-06-04 02:39:05 +00:00
/**
* Test Component declarations with Parameters
* tests merging of component parameters and merging / construction of components .
*
* @ return void
2008-06-06 16:18:14 +00:00
*/
2008-06-04 02:39:05 +00:00
function testComponentsWithParams () {
$this -> skipIf ( defined ( 'AppControllerExists' ), 'Components with Params test will be skipped as it needs a non-existent AppController. As the an AppController class exists, this cannot be run.' );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'ParamTest' => array ( 'test' => 'value' , 'flag' ), 'Apple' );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$this -> assertTrue ( is_a ( $Controller -> ParamTest , 'ParamTestComponent' ));
$this -> assertTrue ( is_a ( $Controller -> ParamTest -> Banana , 'BananaComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Orange , 'OrangeComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Session , 'SessionComponent' ));
$this -> assertEqual ( $Controller -> Orange -> settings , array ( 'colour' => 'blood orange' ));
$this -> assertEqual ( $Controller -> ParamTest -> test , 'value' );
$this -> assertEqual ( $Controller -> ParamTest -> flag , true );
//Settings are merged from app controller and current controller.
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'ParamTest' => array ( 'test' => 'value' ), 'Orange' => array ( 'ripeness' => 'perfect' ));
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$this -> assertEqual ( $Controller -> Orange -> settings , array ( 'colour' => 'blood orange' , 'ripeness' => 'perfect' ));
$this -> assertEqual ( $Controller -> ParamTest -> test , 'value' );
}
2008-06-06 16:18:14 +00:00
/**
* Test mutually referencing components .
*
*
*/
function testMutuallyReferencingComponents () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'MutuallyReferencingOne' );
$Controller -> constructClasses ();
$Controller -> Component -> initialize ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> MutuallyReferencingOne , 'MutuallyReferencingOneComponent' ));
$this -> assertTrue ( is_a ( $Controller -> MutuallyReferencingOne -> MutuallyReferencingTwo , 'MutuallyReferencingTwoComponent' ));
$this -> assertTrue ( is_a ( $Controller -> MutuallyReferencingOne -> MutuallyReferencingTwo -> MutuallyReferencingOne , 'MutuallyReferencingOneComponent' ));
}
2008-05-30 11:40:08 +00:00
}
2008-06-06 16:18:14 +00:00
?>