Permalink
Browse files

optmizing CakeEmail, and improving tests, transport class config() no…

…w returns configs array if no array is passed
  • Loading branch information...
1 parent 9300f09 commit 2efa3b0377300ae103981533529a654d3a399afd @ceeram ceeram committed Aug 29, 2011
View
6 app/Config/email.php.default
@@ -85,11 +85,5 @@ class EmailConfig {
'client' => null
);
- public $test = array(
- 'from' => array('some@example.com' => 'My website'),
- 'to' => array('test@example.com' => 'Testname'),
- 'subject' => 'Test mail subject',
- 'transport' => 'Debug',
- );
}
View
6 lib/Cake/Console/Templates/skel/Config/email.php.default
@@ -85,11 +85,5 @@ class EmailConfig {
'client' => null
);
- public $test = array(
- 'from' => array('some@example.com' => 'My website'),
- 'to' => array('test@example.com' => 'Testname'),
- 'subject' => 'Test mail subject',
- 'transport' => 'Debug',
- );
}
View
9 lib/Cake/Network/Email/AbstractTransport.php
@@ -42,13 +42,14 @@
/**
* Set the config
*
- * @param array $config
- * @return object $this
+ * @param mixed $config
+ * @return array Returns configs
*/
- public function config($config = array()) {
- if (!empty($config)) {
+ public function config($config = null) {
+ if (is_array($config)) {
$this->_config = $config;
}
+ return $this->_config;
}
/**
View
4 lib/Cake/Network/Email/CakeEmail.php
@@ -942,7 +942,7 @@ public function send($content = null) {
$this->_message[] = '';
}
$contents = $this->transportClass()->send($this);
- if (isset($this->_config['log']) && $this->_config['log']) {
+ if (!empty($this->_config['log'])) {
$level = LOG_DEBUG;
if ($this->_config['log'] !== true) {
$level = $this->_config['log'];
@@ -995,7 +995,7 @@ public static function deliver($to = null, $subject = null, $message = null, $tr
*/
protected function _applyConfig($config) {
if (is_string($config)) {
- if (!config('email')) {
+ if (!class_exists('EmailConfig') && !config('email')) {
throw new SocketException(__d('cake', '%s not found.', APP . 'Config' . DS . 'email.php'));
}
$configs = new EmailConfig();
View
2 lib/Cake/Network/Email/DebugTransport.php
@@ -34,7 +34,7 @@ class DebugTransport extends AbstractTransport {
public function send(CakeEmail $email) {
$headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'));
$headers = $this->_headersToString($headers);
- $message = implode((array)$email->message(), "\n");
+ $message = implode("\n", (array)$email->message());
return array('headers' => $headers, 'message' => $message);
}
View
188 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -57,65 +57,23 @@ public function getBoundary() {
}
-/**
- * Debug transport email
- *
- */
-class DebugTransport extends AbstractTransport {
-
-/**
- * Last email body
+/*
+ * EmailConfig class
*
- * @var string
*/
- public static $lastEmail = '';
+class EmailConfig {
/**
- * Last email header
+ * test config
*
* @var string
*/
- public static $lastHeader = '';
-
-/**
- * Include addresses in header
- *
- * @var boolean
- */
- public static $includeAddresses = false;
-
-/**
- * Config
- *
- * @var array
- */
- public static $config = array();
-
-/**
- * Config
- *
- * @param mixed $config
- * @return mixed
- */
- public function config($config) {
- self::$config = $config;
- }
-
-/**
- * Send
- *
- * @param object $email CakeEmail
- * @return boolean
- */
- public function send(CakeEmail $email) {
- self::$lastEmail = implode("\r\n", $email->message());
- $options = array();
- if (self::$includeAddresses) {
- $options = array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true);
- }
- self::$lastHeader = $this->_headersToString($email->getHeaders($options));
- return true;
- }
+ public $test = array(
+ 'from' => array('some@example.com' => 'My website'),
+ 'to' => array('test@example.com' => 'Testname'),
+ 'subject' => 'Test mail subject',
+ 'transport' => 'Debug',
+ );
}
@@ -517,9 +475,9 @@ public function testAttachments() {
* @return void
*/
public function testTransport() {
- $result = $this->CakeEmail->transport('debug');
+ $result = $this->CakeEmail->transport('Debug');
$this->assertIdentical($this->CakeEmail, $result);
- $this->assertIdentical($this->CakeEmail->transport(), 'debug');
+ $this->assertIdentical($this->CakeEmail->transport(), 'Debug');
$result = $this->CakeEmail->transportClass();
$this->assertIsA($result, 'DebugTransport');
@@ -531,16 +489,15 @@ public function testTransport() {
* @return void
*/
public function testConfig() {
- $this->CakeEmail->transport('debug')->transportClass();
- DebugTransport::$config = array();
+ $transportClass = $this->CakeEmail->transport('debug')->transportClass();
$config = array('test' => 'ok', 'test2' => true);
$this->CakeEmail->config($config);
- $this->assertIdentical(DebugTransport::$config, $config);
+ $this->assertIdentical($transportClass->config(), $config);
$this->assertIdentical($this->CakeEmail->config(), $config);
$this->CakeEmail->config(array());
- $this->assertIdentical(DebugTransport::$config, array());
+ $this->assertIdentical($transportClass->config(), array());
}
@@ -550,12 +507,7 @@ public function testConfig() {
* @return void
*/
public function testConfigString() {
- $shouldSkip = true;
- if (config('email')) {
- $configs = new EmailConfig();
- $shouldSkip = !isset($configs->test);
- }
- $this->skipIf($shouldSkip, 'Create email.php with $test config as defined in email.php.default');
+ $configs = new EmailConfig();
$this->CakeEmail->config('test');
$result = $this->CakeEmail->to();
@@ -571,7 +523,7 @@ public function testConfigString() {
$this->assertEquals($configs->test['transport'], $result);
$result = $this->CakeEmail->transportClass();
- $this->assertTrue($result instanceof DebugTransport);
+ $this->assertIsA($result, 'DebugTransport');
}
/**
* testSendWithContent method
@@ -580,27 +532,28 @@ public function testConfigString() {
*/
public function testSendWithContent() {
$this->CakeEmail->reset();
- $this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = false;
+ $this->CakeEmail->transport('Debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
+
$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
+ $expected = array('headers', 'message');
+ $this->assertEquals($expected, array_keys($result));
+ $expected = "Here is my body, with multi lines.\nThis is the second line.\n\nAnd the last.\n\n";
- $this->assertTrue($result);
- $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
- $this->assertIdentical(DebugTransport::$lastEmail, $expected);
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Date: '));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
- $this->assertFalse(strpos(DebugTransport::$lastHeader, 'To: '));
+ $this->assertEquals($expected, $result['message']);
+ $this->assertTrue((bool)strpos($result['headers'], 'Date: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'To: '));
- DebugTransport::$includeAddresses = true;
- $this->CakeEmail->send("Other body");
- $this->assertIdentical(DebugTransport::$lastEmail, "Other body\r\n\r\n");
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
+ $result = $this->CakeEmail->send("Other body");
+ $expected = "Other body\n\n";
+ $this->assertIdentical($result['message'], $expected);
+ $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
/**
@@ -611,7 +564,6 @@ public function testSendWithContent() {
public function testSendRender() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
@@ -620,9 +572,9 @@ public function testSendRender() {
$this->CakeEmail->template('default', 'default');
$result = $this->CakeEmail->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the CakePHP Framework'));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
+ $this->assertTrue((bool)strpos($result['message'], 'This email was sent using the CakePHP Framework'));
+ $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
/**
@@ -633,7 +585,6 @@ public function testSendRender() {
public function testSendRenderWithVars() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
@@ -643,7 +594,7 @@ public function testSendRenderWithVars() {
$this->CakeEmail->viewVars(array('value' => 12345));
$result = $this->CakeEmail->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
+ $this->assertTrue((bool)strpos($result['message'], 'Here is your value: 12345'));
}
/**
@@ -654,7 +605,6 @@ public function testSendRenderWithVars() {
public function testSendRenderWithHelpers() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
$timestamp = time();
$this->CakeEmail->from('cake@cakephp.org');
@@ -666,7 +616,7 @@ public function testSendRenderWithHelpers() {
$this->CakeEmail->helpers(array('Time'));
$result = $this->CakeEmail->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
+ $this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
}
/**
@@ -682,29 +632,27 @@ public function testSendRenderPlugin() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
- $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the CakePHP Framework'));
+ $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
+ $this->assertTrue((bool)strpos($result['message'], 'Into TestPlugin.'));
+ $this->assertTrue((bool)strpos($result['message'], 'This email was sent using the CakePHP Framework'));
- $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));
+ $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
+ $this->assertTrue((bool)strpos($result['message'], 'Into TestPlugin.'));
+ $this->assertTrue((bool)strpos($result['message'], 'This email was sent using the TestPlugin.'));
- DebugTransport::$lastEmail = '';
- $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));
+ $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
+ $this->assertTrue((bool)strpos($result['message'], 'Into TestPlugin.'));
+ $this->assertTrue((bool)strpos($result['message'], 'This email was sent using the TestPlugin.'));
$this->CakeEmail->viewVars(array('value' => 12345));
- $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));
+ $result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
+ $this->assertTrue((bool)strpos($result['message'], 'Here is your value: 12345'));
+ $this->assertTrue((bool)strpos($result['message'], 'This email was sent using the TestPlugin.'));
$this->expectException();
$this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
@@ -718,7 +666,6 @@ public function testSendRenderPlugin() {
public function testSendMultipleMIME() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
@@ -757,27 +704,29 @@ public function testSendMultipleMIME() {
public function testSendAttachment() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = false;
-
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array());
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
- $this->CakeEmail->send('body');
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\""));
+ $result = $this->CakeEmail->send('body');
+
+ $wrap = $this->CakeEmail->wrap($result['message']);
+ $this->assertTrue(in_array("Content-Type: application/octet-stream", $wrap));
+ $this->assertTrue(in_array("Content-Transfer-Encoding: base64", $wrap));
+ $this->assertTrue(in_array("Content-Disposition: attachment; filename=\"basics.php\"", $wrap));
$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
- $this->CakeEmail->send('body');
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\""));
+ $result = $this->CakeEmail->send('body');
+ $this->assertTrue((bool)strpos($result['message'], "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\""));
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
- $this->CakeEmail->send('body');
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\""));
+ $result = $this->CakeEmail->send('body');
+ $this->assertTrue((bool)strpos($result['message'], "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\""));
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
$this->CakeEmail->send('body');
- $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file.txt\""));
+ $this->assertTrue((bool)strpos($result['message'], "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file.txt\""));
}
/**
@@ -818,8 +767,6 @@ public function testDeliver() {
public function testMessage() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
- DebugTransport::$includeAddresses = true;
-
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
@@ -949,8 +896,8 @@ public function testConstructWithConfigArray() {
$result = $this->CakeEmail->send('This is the message');
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
/**
@@ -959,12 +906,7 @@ public function testConstructWithConfigArray() {
* @return void
*/
public function testConstructWithConfigString() {
- $shouldSkip = true;
- if (config('email')) {
- $configs = new EmailConfig();
- $shouldSkip = !isset($configs->test);
- }
- $this->skipIf($shouldSkip, 'Create email.php with $test config as defined in email.php.default');
+ $configs = new EmailConfig();
$this->CakeEmail = new CakeEmail('test');
$result = $this->CakeEmail->to();
@@ -982,10 +924,10 @@ public function testConstructWithConfigString() {
$result = $this->CakeEmail->transportClass();
$this->assertTrue($result instanceof DebugTransport);
- $this->CakeEmail->send('This is the message');
+ $result = $this->CakeEmail->send('This is the message');
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
- $this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
+ $this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
}

0 comments on commit 2efa3b0

Please sign in to comment.