Browse Source

Start implementing Controller::initialize()

Fix tests and update code to implement a new initialize() hook that
works similar to Table::initialize(). Hopefully, with this we can
deprecate the $components and $helpers properties and replace them with
methods.
mark_story 11 years ago
parent
commit
4c52dede4f

+ 20 - 2
src/Controller/Controller.php

@@ -230,11 +230,12 @@ class Controller implements EventListener {
  * conventions CakePHP uses you can define properties in your class declaration.
  *
  * @param \Cake\Network\Request $request Request object for this controller. Can be null for testing,
- *  but expect that features that use the request parameters will not work.
+ *   but expect that features that use the request parameters will not work.
  * @param \Cake\Network\Response $response Response object for this controller.
  * @param string $name Override the name useful in testing when using mocks.
+ * @param \Cake\Event\EventManager $eventManager The event manager. Defaults to a new instance.
  */
-	public function __construct(Request $request = null, Response $response = null, $name = null) {
+	public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null) {
 		if ($this->name === null && $name === null) {
 			list(, $name) = namespaceSplit(get_class($this));
 			$name = substr($name, 0, -10);
@@ -261,10 +262,27 @@ class Controller implements EventListener {
 		if ($response instanceof Response) {
 			$this->response = $response;
 		}
+		if ($eventManager) {
+			$this->eventManager($eventManager);
+		}
 
 		$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
 		$modelClass = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
 		$this->_setModelClass($modelClass);
+
+		$this->constructClasses();
+		$this->initialize();
+	}
+
+/**
+ * Initialization hook method.
+ *
+ * Implement this method to avoid having to overwrite
+ * the constructor and call parent.
+ *
+ * @return void
+ */
+	public function initialize() {
 	}
 
 /**

+ 0 - 1
src/Routing/Dispatcher.php

@@ -104,7 +104,6 @@ class Dispatcher {
  *   instance of Response
  */
 	protected function _invoke(Controller $controller) {
-		$controller->constructClasses();
 		$result = $controller->startupProcess();
 		if ($result instanceof Response) {
 			return $result;

+ 6 - 17
tests/TestCase/Controller/Component/AuthComponentTest.php

@@ -72,8 +72,6 @@ class AuthComponentTest extends TestCase {
 		$response = $this->getMock('Cake\Network\Response', array('stop'));
 
 		$this->Controller = new AuthTestController($request, $response);
-		$this->Controller->constructClasses();
-
 		$this->Auth = new TestAuthComponent($this->Controller->components());
 
 		$Users = TableRegistry::get('AuthUsers');
@@ -982,19 +980,10 @@ class AuthComponentTest extends TestCase {
  * @return void
  */
 	public function testComponentSettings() {
-		Router::connect('/:controller');
-
-		$request = new Request();
-		$this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
-
-		$this->Controller->components = array(
-			'Auth' => array(
-				'loginAction' => array('controller' => 'people', 'action' => 'login'),
-				'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
-			),
-			'Session'
-		);
-		$this->Controller->constructClasses();
+		$this->Auth->config([
+			'loginAction' => array('controller' => 'people', 'action' => 'login'),
+			'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
+		]);
 
 		$expected = array(
 			'loginAction' => array('controller' => 'people', 'action' => 'login'),
@@ -1002,11 +991,11 @@ class AuthComponentTest extends TestCase {
 		);
 		$this->assertEquals(
 			$expected['loginAction'],
-			$this->Controller->Auth->config('loginAction')
+			$this->Auth->config('loginAction')
 		);
 		$this->assertEquals(
 			$expected['logoutRedirect'],
-			$this->Controller->Auth->config('logoutRedirect')
+			$this->Auth->config('logoutRedirect')
 		);
 	}
 

+ 9 - 15
tests/TestCase/Controller/ControllerTest.php

@@ -625,15 +625,15 @@ class ControllerTest extends TestCase {
  * @return void
  */
 	public function testStartupProcess() {
-		$Controller = $this->getMock('Cake\Controller\Controller', array('eventManager'));
-
 		$eventManager = $this->getMock('Cake\Event\EventManager');
+		$controller = new Controller(null, null, null, $eventManager);
+
 		$eventManager->expects($this->at(0))->method('dispatch')
 			->with(
 				$this->logicalAnd(
 					$this->isInstanceOf('Cake\Event\Event'),
 					$this->attributeEqualTo('_name', 'Controller.initialize'),
-					$this->attributeEqualTo('_subject', $Controller)
+					$this->attributeEqualTo('_subject', $controller)
 				)
 			)
 			->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
@@ -643,15 +643,12 @@ class ControllerTest extends TestCase {
 				$this->logicalAnd(
 					$this->isInstanceOf('Cake\Event\Event'),
 					$this->attributeEqualTo('_name', 'Controller.startup'),
-					$this->attributeEqualTo('_subject', $Controller)
+					$this->attributeEqualTo('_subject', $controller)
 				)
 			)
 			->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
 
-		$Controller->expects($this->exactly(2))->method('eventManager')
-			->will($this->returnValue($eventManager));
-
-		$Controller->startupProcess();
+		$controller->startupProcess();
 	}
 
 /**
@@ -660,23 +657,20 @@ class ControllerTest extends TestCase {
  * @return void
  */
 	public function testShutdownProcess() {
-		$Controller = $this->getMock('Cake\Controller\Controller', array('eventManager'));
-
 		$eventManager = $this->getMock('Cake\Event\EventManager');
+		$controller = new Controller(null, null, null, $eventManager);
+
 		$eventManager->expects($this->once())->method('dispatch')
 			->with(
 				$this->logicalAnd(
 					$this->isInstanceOf('Cake\Event\Event'),
 					$this->attributeEqualTo('_name', 'Controller.shutdown'),
-					$this->attributeEqualTo('_subject', $Controller)
+					$this->attributeEqualTo('_subject', $controller)
 				)
 			)
 			->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
 
-		$Controller->expects($this->once())->method('eventManager')
-			->will($this->returnValue($eventManager));
-
-		$Controller->shutdownProcess();
+		$controller->shutdownProcess();
 	}
 
 /**