Permalink
Browse files

Avoid reloading config file and recreating config instance.

  • Loading branch information...
ADmad committed Feb 19, 2015
1 parent e3b5306 commit 2a57d9b65f5f899a28638241b1d44c24801f7140
Showing with 53 additions and 30 deletions.
  1. +17 −7 lib/Cake/Network/Email/CakeEmail.php
  2. +36 −23 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -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 @@ public function __construct($config = null) {
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 @@ public static function deliver($to = null, $subject = null, $message = null, $tr
*/
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'])) {
@@ -17,6 +17,7 @@
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('File', 'Utility');
/**
* Help to test CakeEmail
@@ -92,6 +93,15 @@ public function render($content) {
*/
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 @@ public function setUp() {
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 @@ public function testConfig() {
$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 @@ public function assertLineLengths($message) {
}
}
/**
* 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();
}
}
}

0 comments on commit 2a57d9b

Please sign in to comment.