From 2a57d9b65f5f899a28638241b1d44c24801f7140 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 19 Feb 2015 12:25:17 +0530 Subject: [PATCH] Avoid reloading config file and recreating config instance. --- lib/Cake/Network/Email/CakeEmail.php | 24 +++++--- .../Test/Case/Network/Email/CakeEmailTest.php | 59 +++++++++++-------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 7150bb417..4f860db61 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.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 @@ 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'])) { diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 7668baad7..66b36dcd6 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -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(); - } - } - }