Navigation Menu

Skip to content

Commit

Permalink
Pass Controller:$components config to mocked components
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AD7six committed Aug 19, 2013
1 parent 0f2d59d commit 09cf69c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/Cake/Test/Case/TestSuite/ControllerTestCaseTest.php
Expand Up @@ -210,6 +210,28 @@ public function testGenerate() {
$this->assertEquals('written!', $Posts->Auth->Session->write('something'));
}

/**
* testGenerateWithComponentConfig
*/
public function testGenerateWithComponentConfig() {
$Tests = $this->Case->generate('TestConfigs', array(
));

$expected = array('some' => 'config');
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');

$Tests = $this->Case->generate('TestConfigs', array(
'components' => array(
'RequestHandler' => array('isPut')
)
));

$expected = array('some' => 'config');
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
}

/**
* Tests ControllerTestCase::generate() using classes from plugins
*/
Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/Test/test_app/Controller/TestConfigsController.php
@@ -0,0 +1,13 @@
<?php

App::uses('CakeErrorController', 'Controller');

class TestConfigsController extends CakeErrorController {

public $components = array(
'RequestHandler' => array(
'some' => 'config'
)
);

}
3 changes: 2 additions & 1 deletion lib/Cake/TestSuite/ControllerTestCase.php
Expand Up @@ -364,7 +364,8 @@ public function generate($controller, $mocks = array()) {
'class' => $componentClass
));
}
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components));
$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
$controllerObj->Components->set($name, $componentObj);
$controllerObj->Components->enable($name);
}
Expand Down

0 comments on commit 09cf69c

Please sign in to comment.