BaseMessageTest.php 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php

namespace yiiunit\framework\mail;

use Yii;
use yii\mail\BaseMailer;
use yii\mail\BaseMessage;
use yiiunit\TestCase;

/**
 * @group mail
 */
class BaseMessageTest extends TestCase
{
	public function setUp()
	{
		$this->mockApplication([
			'components' => [
				'mail' => $this->createTestEmailComponent()
			]
		]);
	}

	/**
	 * @return Mailer test email component instance.
	 */
	protected function createTestEmailComponent()
	{
		$component = new TestMailer();
		return $component;
	}

	/**
	 * @return TestMailer mailer instance.
	 */
	protected function getMailer()
	{
		return Yii::$app->getComponent('mail');
	}

	// Tests :

43
	public function testSend()
44 45
	{
		$mailer = $this->getMailer();
46
		$message = $mailer->compose();
47
		$message->send($mailer);
48
		$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
49 50
	}

51
	public function testToString()
52 53
	{
		$mailer = $this->getMailer();
54 55
		$message = $mailer->compose();
		$this->assertEquals($message->toString(), '' . $message);
56 57 58 59 60 61 62 63 64
	}
}

/**
 * Test Mailer class
 */
class TestMailer extends BaseMailer
{
	public $messageClass = 'yiiunit\framework\mail\TestMessage';
Qiang Xue committed
65
	public $sentMessages = [];
66

67
	protected function sendMessage($message)
68 69 70 71 72 73 74 75 76 77 78 79 80
	{
		$this->sentMessages[] = $message;
	}
}

/**
 * Test Message class
 */
class TestMessage extends BaseMessage
{
	public $text;
	public $html;

Qiang Xue committed
81 82
	public function getCharset() {return '';}

Qiang Xue committed
83
	public function setCharset($charset) {}
84

Qiang Xue committed
85 86
	public function getFrom() {return '';}

Qiang Xue committed
87
	public function setFrom($from) {}
88

Qiang Xue committed
89 90 91 92 93 94
	public function getReplyTo() {return '';}

	public function setReplyTo($replyTo) {}

	public function getTo() {return '';}

Qiang Xue committed
95
	public function setTo($to) {}
96

Qiang Xue committed
97 98
	public function getCc() {return '';}

Qiang Xue committed
99
	public function setCc($cc) {}
100

Qiang Xue committed
101 102
	public function getBcc() {return '';}

Qiang Xue committed
103
	public function setBcc($bcc) {}
104

Qiang Xue committed
105 106
	public function getSubject() {return '';}

Qiang Xue committed
107
	public function setSubject($subject) {}
108

Qiang Xue committed
109
	public function setTextBody($text) {
110 111 112
		$this->text = $text;
	}

Qiang Xue committed
113
	public function setHtmlBody($html) {
114 115 116 117 118
		$this->html = $html;
	}

	public function attachContent($content, array $options = []) {}

119
	public function attach($fileName, array $options = []) {}
120

121
	public function embed($fileName, array $options = []) {}
122 123 124

	public function embedContent($content, array $options = []) {}

125
	public function toString()
126 127 128
	{
		return get_class($this);
	}
Qiang Xue committed
129
}