Skip to content
Permalink
Browse files

[2.x]Fix can't load aliased component on ControllerTestCase

  • Loading branch information...
tenkoma committed Dec 31, 2017
1 parent 51206d7 commit 74a8611eef339b12de5f0d4344338ee179c90629
@@ -79,6 +79,12 @@ class PostsController extends AppController {
public $components = array(
'RequestHandler',
'Email',
'AliasedEmail' => array(
'className' => 'Email',
),
'AliasedPluginEmail' => array(
'className' => 'TestPlugin.TestPluginEmail',
),
'Auth'
);
}
@@ -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
*
@@ -0,0 +1,10 @@
<?php
App::uses('EmailComponent', 'Controller/Component');
/**
* TestPluginEmailComponent
*
* @package Cake.Test.TestApp.Plugin.TestPlugin.Controller.Component
*/
class TestPluginEmailComponent extends EmailComponent {
}
@@ -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();

0 comments on commit 74a8611

Please sign in to comment.
You can’t perform that action at this time.