Skip to content

Commit 09cf69c

Browse files
committed
Pass Controller:$components config to mocked components
Otherwise there can be significant differences in behavior between using an unmodifiedcomponent in testAction and using a mock as the config will not be propogated from the controller
1 parent 0f2d59d commit 09cf69c

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/Cake/Test/Case/TestSuite/ControllerTestCaseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ public function testGenerate() {
210210
$this->assertEquals('written!', $Posts->Auth->Session->write('something'));
211211
}
212212

213+
/**
214+
* testGenerateWithComponentConfig
215+
*/
216+
public function testGenerateWithComponentConfig() {
217+
$Tests = $this->Case->generate('TestConfigs', array(
218+
));
219+
220+
$expected = array('some' => 'config');
221+
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
222+
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
223+
224+
$Tests = $this->Case->generate('TestConfigs', array(
225+
'components' => array(
226+
'RequestHandler' => array('isPut')
227+
)
228+
));
229+
230+
$expected = array('some' => 'config');
231+
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
232+
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
233+
}
234+
213235
/**
214236
* Tests ControllerTestCase::generate() using classes from plugins
215237
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
App::uses('CakeErrorController', 'Controller');
4+
5+
class TestConfigsController extends CakeErrorController {
6+
7+
public $components = array(
8+
'RequestHandler' => array(
9+
'some' => 'config'
10+
)
11+
);
12+
13+
}

lib/Cake/TestSuite/ControllerTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ public function generate($controller, $mocks = array()) {
364364
'class' => $componentClass
365365
));
366366
}
367-
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components));
367+
$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
368+
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
368369
$controllerObj->Components->set($name, $componentObj);
369370
$controllerObj->Components->enable($name);
370371
}

0 commit comments

Comments
 (0)