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

This commit is contained in:
Koji Tanaka 2017-12-31 14:25:00 +09:00
parent 51206d7358
commit 74a8611eef
3 changed files with 67 additions and 3 deletions

View file

@ -79,6 +79,12 @@ if (!class_exists('PostsController')) {
public $components = array(
'RequestHandler',
'Email',
'AliasedEmail' => array(
'className' => 'Email',
),
'AliasedPluginEmail' => array(
'className' => 'TestPlugin.TestPluginEmail',
),
'Auth'
);
}
@ -270,6 +276,46 @@ class ControllerTestCaseTest extends CakeTestCase {
$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
*

View file

@ -0,0 +1,10 @@
<?php
App::uses('EmailComponent', 'Controller/Component');
/**
* TestPluginEmailComponent
*
* @package Cake.Test.TestApp.Plugin.TestPlugin.Controller.Component
*/
class TestPluginEmailComponent extends EmailComponent {
}

View file

@ -388,7 +388,15 @@ abstract class ControllerTestCase extends CakeTestCase {
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)) {
@ -396,11 +404,11 @@ abstract class ControllerTestCase extends CakeTestCase {
'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();