Avoid reloading config file and recreating config instance.

This commit is contained in:
ADmad 2015-02-19 12:25:17 +05:30
parent e3b5306521
commit 2a57d9b65f
2 changed files with 53 additions and 30 deletions

View file

@ -336,6 +336,13 @@ class CakeEmail {
*/
protected $_configClass = 'EmailConfig';
/**
* An instance of the EmailConfig class can be set here
*
* @var string
*/
protected $_configInstance;
/**
* Constructor
*
@ -353,8 +360,9 @@ class CakeEmail {
if ($config) {
$this->config($config);
} elseif (config('email')) {
if (property_exists($this->_configClass, 'default')) {
} elseif (class_exists($this->_configClass) && config('email')) {
$this->_configInstance = new $this->_configClass();
if (isset($this->_configInstance->default)) {
$this->config('default');
}
}
@ -1227,14 +1235,16 @@ class CakeEmail {
*/
protected function _applyConfig($config) {
if (is_string($config)) {
if (!class_exists($this->_configClass) && !config('email')) {
throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
if (!$this->_configInstance) {
if (!class_exists($this->_configClass) && !config('email')) {
throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
}
$this->_configInstance = new $this->_configClass();
}
$configs = new $this->_configClass();
if (!isset($configs->{$config})) {
if (!isset($this->_configInstance->{$config})) {
throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
}
$config = $configs->{$config};
$config = $this->_configInstance->{$config};
}
$this->_config = $config + $this->_config;
if (!empty($config['charset'])) {

View file

@ -17,6 +17,7 @@
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('File', 'Utility');
/**
* Help to test CakeEmail
@ -92,6 +93,15 @@ class TestCakeEmail extends CakeEmail {
*/
class TestEmailConfig {
/**
* default config
*
* @var array
*/
public $default = array(
'subject' => 'Default Subject',
);
/**
* test config
*
@ -145,6 +155,14 @@ class CakeEmailTest extends CakeTestCase {
*/
public function setUp() {
parent::setUp();
$this->_configFileExists = true;
$emailConfig = new File(APP . 'Config' . DS . 'email.php');
if (!$emailConfig->exists()) {
$this->_configFileExists = false;
$emailConfig->create();
}
$this->CakeEmail = new TestCakeEmail();
App::build(array(
@ -160,6 +178,19 @@ class CakeEmailTest extends CakeTestCase {
public function tearDown() {
parent::tearDown();
App::build();
if (!$this->_configFileExists) {
unlink(APP . 'Config' . DS . 'email.php');
}
}
/**
* Test if the EmailConfig::$default configuration is read when present
*
* @return void
*/
public function testDefaultConfig() {
$this->assertEquals('Default Subject', $this->CakeEmail->subject());
}
/**
@ -860,16 +891,17 @@ class CakeEmailTest extends CakeTestCase {
$config = array('test' => 'ok', 'test2' => true);
$this->CakeEmail->config($config);
$this->assertSame($config, $transportClass->config());
$this->assertSame($config, $this->CakeEmail->config());
$expected = $config + array('subject' => 'Default Subject');
$this->assertSame($expected, $this->CakeEmail->config());
$this->CakeEmail->config(array());
$this->assertSame($config, $transportClass->config());
$config = array('test' => 'test@example.com');
$config = array('test' => 'test@example.com', 'subject' => 'my test subject');
$this->CakeEmail->config($config);
$expected = array('test' => 'test@example.com', 'test2' => true);
$expected = array('test' => 'test@example.com', 'subject' => 'my test subject', 'test2' => true);
$this->assertSame($expected, $this->CakeEmail->config());
$this->assertSame($expected, $transportClass->config());
$this->assertSame(array('test' => 'test@example.com', 'test2' => true), $transportClass->config());
}
/**
@ -2449,23 +2481,4 @@ HTML;
}
}
/**
* Test if the EmailConfig::$default configuration is read when present
*
* @return void
*/
public function testDefaultConfig() {
$defaultConfig = new File(APP . 'Config' . DS . 'email.php.default');
$emailConfig = new File(APP . 'Config' . DS . 'email.php');
$hasConfig = $emailConfig->exists();
$this->skipIf(!$defaultConfig->copy(APP . 'Config' . DS . 'email.php', false));
$Email = new CakeEmail();
$this->skipIf(!property_exists('EmailConfig', 'default'));
$this->assertEquals('you@localhost', current($Email->from()));
if (!$hasConfig) {
$emailConfig->delete();
}
}
}