Skip to content

Commit 4c52ded

Browse files
committed
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.
1 parent 6f25e4a commit 4c52ded

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/Controller/Controller.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,12 @@ class Controller implements EventListener {
230230
* conventions CakePHP uses you can define properties in your class declaration.
231231
*
232232
* @param \Cake\Network\Request $request Request object for this controller. Can be null for testing,
233-
* but expect that features that use the request parameters will not work.
233+
* but expect that features that use the request parameters will not work.
234234
* @param \Cake\Network\Response $response Response object for this controller.
235235
* @param string $name Override the name useful in testing when using mocks.
236+
* @param \Cake\Event\EventManager $eventManager The event manager. Defaults to a new instance.
236237
*/
237-
public function __construct(Request $request = null, Response $response = null, $name = null) {
238+
public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null) {
238239
if ($this->name === null && $name === null) {
239240
list(, $name) = namespaceSplit(get_class($this));
240241
$name = substr($name, 0, -10);
@@ -261,10 +262,27 @@ public function __construct(Request $request = null, Response $response = null,
261262
if ($response instanceof Response) {
262263
$this->response = $response;
263264
}
265+
if ($eventManager) {
266+
$this->eventManager($eventManager);
267+
}
264268

265269
$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
266270
$modelClass = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
267271
$this->_setModelClass($modelClass);
272+
273+
$this->constructClasses();
274+
$this->initialize();
275+
}
276+
277+
/**
278+
* Initialization hook method.
279+
*
280+
* Implement this method to avoid having to overwrite
281+
* the constructor and call parent.
282+
*
283+
* @return void
284+
*/
285+
public function initialize() {
268286
}
269287

270288
/**

src/Routing/Dispatcher.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public function dispatch(Request $request, Response $response) {
104104
* instance of Response
105105
*/
106106
protected function _invoke(Controller $controller) {
107-
$controller->constructClasses();
108107
$result = $controller->startupProcess();
109108
if ($result instanceof Response) {
110109
return $result;

tests/TestCase/Controller/Component/AuthComponentTest.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public function setUp() {
7272
$response = $this->getMock('Cake\Network\Response', array('stop'));
7373

7474
$this->Controller = new AuthTestController($request, $response);
75-
$this->Controller->constructClasses();
76-
7775
$this->Auth = new TestAuthComponent($this->Controller->components());
7876

7977
$Users = TableRegistry::get('AuthUsers');
@@ -982,31 +980,22 @@ public function testStatelessAuthWorksWithUser() {
982980
* @return void
983981
*/
984982
public function testComponentSettings() {
985-
Router::connect('/:controller');
986-
987-
$request = new Request();
988-
$this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
989-
990-
$this->Controller->components = array(
991-
'Auth' => array(
992-
'loginAction' => array('controller' => 'people', 'action' => 'login'),
993-
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
994-
),
995-
'Session'
996-
);
997-
$this->Controller->constructClasses();
983+
$this->Auth->config([
984+
'loginAction' => array('controller' => 'people', 'action' => 'login'),
985+
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
986+
]);
998987

999988
$expected = array(
1000989
'loginAction' => array('controller' => 'people', 'action' => 'login'),
1001990
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
1002991
);
1003992
$this->assertEquals(
1004993
$expected['loginAction'],
1005-
$this->Controller->Auth->config('loginAction')
994+
$this->Auth->config('loginAction')
1006995
);
1007996
$this->assertEquals(
1008997
$expected['logoutRedirect'],
1009-
$this->Controller->Auth->config('logoutRedirect')
998+
$this->Auth->config('logoutRedirect')
1010999
);
10111000
}
10121001

tests/TestCase/Controller/ControllerTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,15 @@ public function testSetAction() {
625625
* @return void
626626
*/
627627
public function testStartupProcess() {
628-
$Controller = $this->getMock('Cake\Controller\Controller', array('eventManager'));
629-
630628
$eventManager = $this->getMock('Cake\Event\EventManager');
629+
$controller = new Controller(null, null, null, $eventManager);
630+
631631
$eventManager->expects($this->at(0))->method('dispatch')
632632
->with(
633633
$this->logicalAnd(
634634
$this->isInstanceOf('Cake\Event\Event'),
635635
$this->attributeEqualTo('_name', 'Controller.initialize'),
636-
$this->attributeEqualTo('_subject', $Controller)
636+
$this->attributeEqualTo('_subject', $controller)
637637
)
638638
)
639639
->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
@@ -643,15 +643,12 @@ public function testStartupProcess() {
643643
$this->logicalAnd(
644644
$this->isInstanceOf('Cake\Event\Event'),
645645
$this->attributeEqualTo('_name', 'Controller.startup'),
646-
$this->attributeEqualTo('_subject', $Controller)
646+
$this->attributeEqualTo('_subject', $controller)
647647
)
648648
)
649649
->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
650650

651-
$Controller->expects($this->exactly(2))->method('eventManager')
652-
->will($this->returnValue($eventManager));
653-
654-
$Controller->startupProcess();
651+
$controller->startupProcess();
655652
}
656653

657654
/**
@@ -660,23 +657,20 @@ public function testStartupProcess() {
660657
* @return void
661658
*/
662659
public function testShutdownProcess() {
663-
$Controller = $this->getMock('Cake\Controller\Controller', array('eventManager'));
664-
665660
$eventManager = $this->getMock('Cake\Event\EventManager');
661+
$controller = new Controller(null, null, null, $eventManager);
662+
666663
$eventManager->expects($this->once())->method('dispatch')
667664
->with(
668665
$this->logicalAnd(
669666
$this->isInstanceOf('Cake\Event\Event'),
670667
$this->attributeEqualTo('_name', 'Controller.shutdown'),
671-
$this->attributeEqualTo('_subject', $Controller)
668+
$this->attributeEqualTo('_subject', $controller)
672669
)
673670
)
674671
->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
675672

676-
$Controller->expects($this->once())->method('eventManager')
677-
->will($this->returnValue($eventManager));
678-
679-
$Controller->shutdownProcess();
673+
$controller->shutdownProcess();
680674
}
681675

682676
/**

0 commit comments

Comments
 (0)