Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Sep 13, 2014
1 parent 6f25e4a commit 4c52dede4f788d74da8b5aec5dbafe8f2f43ee70
@@ -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 @@ public function __construct(Request $request = null, Response $response = null,
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() {
}
/**
@@ -104,7 +104,6 @@ public function dispatch(Request $request, Response $response) {
* instance of Response
*/
protected function _invoke(Controller $controller) {
$controller->constructClasses();
$result = $controller->startupProcess();
if ($result instanceof Response) {
return $result;
@@ -72,8 +72,6 @@ public function setUp() {
$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,31 +980,22 @@ public function testStatelessAuthWorksWithUser() {
* @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'),
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
);
$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')
);
}
@@ -625,15 +625,15 @@ public function testSetAction() {
* @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 @@ public function testStartupProcess() {
$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 @@ public function testStartupProcess() {
* @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();
}
/**

0 comments on commit 4c52ded

Please sign in to comment.
You can’t perform that action at this time.