Commit b0c5981d by Paul Klimov

Method names "yii\mail\MessageInterface" simplified.

parent 28b03273
......@@ -42,21 +42,13 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function setCharset($charset)
public function charset($charset)
{
$this->getSwiftMessage()->setCharset($charset);
return $this;
}
/**
* @return string the character set of this message.
*/
public function getCharset()
{
return $this->getSwiftMessage()->getCharset();
}
/**
* @inheritdoc
*/
public function from($from)
......@@ -167,7 +159,7 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function attachFile($fileName, array $options = [])
public function attach($fileName, array $options = [])
{
$attachment = \Swift_Attachment::fromPath($fileName);
if (!empty($options['fileName'])) {
......@@ -199,7 +191,7 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function embedFile($fileName, array $options = [])
public function embed($fileName, array $options = [])
{
$embedFile = \Swift_EmbeddedFile::fromPath($fileName);
if (!empty($options['fileName'])) {
......@@ -229,7 +221,7 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function __toString()
public function toString()
{
return $this->getSwiftMessage()->toString();
}
......
......@@ -46,6 +46,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* @var array configuration, which should be applied by default to any new created
* email message instance.
* In addition to normal [[Yii::createObject()]] behavior extra config keys are available:
* - 'charset' argument for [[MessageInterface::charset()]]
* - 'from' argument for [[MessageInterface::from()]]
* - 'to' argument for [[MessageInterface::to()]]
* - 'cc' argument for [[MessageInterface::cc()]]
......@@ -123,6 +124,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
$config['class'] = $this->messageClass;
}
$directSetterNames = [
'charset',
'from',
'to',
'cc',
......
......@@ -21,7 +21,6 @@ use Yii;
* @see BaseMailer
*
* @property \yii\mail\BaseMailer $mailer mailer component instance. This property is read-only.
* @property string $charset the character set of this message.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
......@@ -77,4 +76,20 @@ abstract class BaseMessage extends Object implements MessageInterface
}
return $this;
}
/**
* PHP magic method that returns the string representation of this object.
* @return string the string representation of this object.
*/
public function __toString()
{
// __toString cannot throw exception
// use trigger_error to bypass this limitation
try {
return $this->toString();
} catch (\Exception $e) {
trigger_error($e->getMessage());
return '';
}
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@ interface MessageInterface
* @param string $charset character set name.
* @return static self reference.
*/
public function setCharset($charset);
public function charset($charset);
/**
* Sets message sender.
......@@ -97,24 +97,24 @@ interface MessageInterface
public function html($html);
/**
* Attach specified content as file for the email message.
* @param string $content attachment file content.
* Attaches existing file to the email message.
* @param string $fileName full file name
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return static self reference.
*/
public function attachContent($content, array $options = []);
public function attach($fileName, array $options = []);
/**
* Attaches existing file to the email message.
* @param string $fileName full file name
* Attach specified content as file for the email message.
* @param string $content attachment file content.
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return static self reference.
*/
public function attachFile($fileName, array $options = []);
public function attachContent($content, array $options = []);
/**
* Attach a file and return it's CID source.
......@@ -125,7 +125,7 @@ interface MessageInterface
* - contentType: attached file MIME type.
* @return string attachment CID.
*/
public function embedFile($fileName, array $options = []);
public function embed($fileName, array $options = []);
/**
* Attach a content as file and return it's CID source.
......@@ -178,9 +178,8 @@ interface MessageInterface
public function body($view, $params = []);
/**
* String output.
* This is PHP magic method that returns string representation of an object.
* @return string the string representation of the object
* Returns string representation of this message.
* @return string the string representation of this message.
*/
public function __toString();
public function toString();
}
\ No newline at end of file
......@@ -124,12 +124,12 @@ class MessageTest extends VendorTestCase
$bcc = 'bccuser@somedomain.com';
$messageString = $this->createTestMessage()
->setCharset($charset)
->charset($charset)
->subject($subject)
->to($to)
->cc($cc)
->bcc($bcc)
->__toString();
->toString();
$this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!');
$this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" header!');
......@@ -146,7 +146,7 @@ class MessageTest extends VendorTestCase
$from = 'someuser@somedomain.com';
$messageString = $this->createTestMessage()
->from($from)
->__toString();
->toString();
$this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!');
$this->assertContains('Reply-To: ' . $from, $messageString, 'Incorrect "Reply-To" header!');
}
......@@ -176,7 +176,7 @@ class MessageTest extends VendorTestCase
$message->subject('Yii Swift Attach File Test');
$message->text('Yii Swift Attach File Test body');
$fileName = __FILE__;
$message->attachFile($fileName);
$message->attach($fileName);
$this->assertTrue($message->send());
......@@ -216,7 +216,7 @@ class MessageTest extends VendorTestCase
$message = $this->createTestMessage();
$cid = $message->embedFile($fileName);
$cid = $message->embed($fileName);
$message->to($this->testEmailReceiver);
$message->from('someuser@somedomain.com');
......
......@@ -119,6 +119,7 @@ class BaseMailerTest extends TestCase
$mailer = new Mailer();
$notPropertyConfig = [
'charset' => 'utf-16',
'from' => 'from@domain.com',
'to' => 'to@domain.com',
'cc' => 'cc@domain.com',
......@@ -144,8 +145,6 @@ class BaseMailerTest extends TestCase
}
}
/**
* @depends testGetDefaultView
*/
......@@ -217,7 +216,7 @@ class BaseMailerTest extends TestCase
class Mailer extends BaseMailer
{
public $messageClass = 'yiiunit\framework\mail\Message';
public $sentMessages = array();
public $sentMessages = [];
public function send($message)
{
......@@ -232,6 +231,7 @@ class Message extends BaseMessage
{
public $id;
public $encoding;
public $_charset;
public $_from;
public $_to;
public $_cc;
......@@ -240,52 +240,63 @@ class Message extends BaseMessage
public $_text;
public $_html;
public function setCharset($charset) {}
public function charset($charset)
{
$this->_charset = $charset;
return $this;
}
public function from($from) {
public function from($from)
{
$this->_from = $from;
return $this;
}
public function to($to) {
public function to($to)
{
$this->_to = $to;
return $this;
}
public function cc($cc) {
public function cc($cc)
{
$this->_cc = $cc;
return $this;
}
public function bcc($bcc) {
public function bcc($bcc)
{
$this->_bcc = $bcc;
return $this;
}
public function subject($subject) {
public function subject($subject)
{
$this->_subject = $subject;
return $this;
}
public function text($text) {
public function text($text)
{
$this->_text = $text;
return $this;
}
public function html($html) {
public function html($html)
{
$this->_html = $html;
return $this;
}
public function attachContent($content, array $options = []) {}
public function attachFile($fileName, array $options = []) {}
public function attach($fileName, array $options = []) {}
public function embedFile($fileName, array $options = []) {}
public function embed($fileName, array $options = []) {}
public function embedContent($content, array $options = []) {}
public function __toString()
public function toString()
{
return get_class($this);
}
......
......@@ -116,7 +116,7 @@ class TestMessage extends BaseMessage
public $text;
public $html;
public function setCharset($charset) {}
public function charset($charset) {}
public function from($from) {}
......@@ -138,13 +138,13 @@ class TestMessage extends BaseMessage
public function attachContent($content, array $options = []) {}
public function attachFile($fileName, array $options = []) {}
public function attach($fileName, array $options = []) {}
public function embedFile($fileName, array $options = []) {}
public function embed($fileName, array $options = []) {}
public function embedContent($content, array $options = []) {}
public function __toString()
public function toString()
{
return get_class($this);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment