Commit b0c5981d by Paul Klimov

Method names "yii\mail\MessageInterface" simplified.

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