Skip to content

Commit

Permalink
[2.x]Fix can't load aliased component on ControllerTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
tenkoma committed Dec 31, 2017
1 parent 51206d7 commit 74a8611
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
46 changes: 46 additions & 0 deletions lib/Cake/Test/Case/TestSuite/ControllerTestCaseTest.php
Expand Up @@ -79,6 +79,12 @@ class PostsController extends AppController {
public $components = array(
'RequestHandler',
'Email',
'AliasedEmail' => array(
'className' => 'Email',
),
'AliasedPluginEmail' => array(
'className' => 'TestPlugin.TestPluginEmail',
),
'Auth'
);
}
Expand Down Expand Up @@ -270,6 +276,46 @@ public function testGenerateWithPlugin() {
$this->assertFalse($Tests->TestPluginComment->save(array()));
}

/**
* Tests ControllerTestCase::generate() using aliased component
*
* @return void
*/
public function testGenerateWithMockedAliasedComponent()
{
$Posts = $this->Case->generate('Posts', array(
'components' => array(
'AliasedEmail' => array('send')
)
));
$Posts->AliasedEmail->expects($this->once())
->method('send')
->will($this->returnValue(true));

$this->assertInstanceOf('EmailComponent', $Posts->AliasedEmail);
$this->assertTrue($Posts->AliasedEmail->send());
}

/**
* Tests ControllerTestCase::generate() using aliased plugin component
*
* @return void
*/
public function testGenerateWithMockedAliasedPluginComponent()
{
$Posts = $this->Case->generate('Posts', array(
'components' => array(
'AliasedPluginEmail' => array('send')
)
));
$Posts->AliasedPluginEmail->expects($this->once())
->method('send')
->will($this->returnValue(true));

$this->assertInstanceOf('TestPluginEmailComponent', $Posts->AliasedPluginEmail);
$this->assertTrue($Posts->AliasedPluginEmail->send());
}

/**
* Tests testAction
*
Expand Down
@@ -0,0 +1,10 @@
<?php
App::uses('EmailComponent', 'Controller/Component');

/**
* TestPluginEmailComponent
*
* @package Cake.Test.TestApp.Plugin.TestPlugin.Controller.Component
*/
class TestPluginEmailComponent extends EmailComponent {
}
14 changes: 11 additions & 3 deletions lib/Cake/TestSuite/ControllerTestCase.php
Expand Up @@ -388,19 +388,27 @@ public function generate($controller, $mocks = array()) {
if ($methods === true) {
$methods = array();
}
$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
if (isset($config['className'])) {
$alias = $component;
$component = $config['className'];
}
list($plugin, $name) = pluginSplit($component, true);
if (!isset($alias)) {
$alias = $name;
}
$componentClass = $name . 'Component';
App::uses($componentClass, $plugin . 'Controller/Component');
if (!class_exists($componentClass)) {
throw new MissingComponentException(array(
'class' => $componentClass
));
}
$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
/** @var Component|PHPUnit_Framework_MockObject_MockObject $componentObj */
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
$controllerObj->Components->set($name, $componentObj);
$controllerObj->Components->enable($name);
$controllerObj->Components->set($alias, $componentObj);
$controllerObj->Components->enable($alias);
unset($alias);
}

$controllerObj->constructClasses();
Expand Down

0 comments on commit 74a8611

Please sign in to comment.