Skip to content

Commit

Permalink
Start implementing Controller::initialize()
Browse files Browse the repository at this point in the history
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 4c52ded
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
22 changes: 20 additions & 2 deletions src/Controller/Controller.php
Expand Up @@ -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);
Expand All @@ -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() {
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Routing/Dispatcher.php
Expand Up @@ -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;
Expand Down
23 changes: 6 additions & 17 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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')
);
}

Expand Down
24 changes: 9 additions & 15 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -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)));
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit 4c52ded

Please sign in to comment.