BaseMessageTest.php 2.35 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;

AlexGx committed
81 82 83 84
	public function getCharset()
	{
		return '';
	}
Qiang Xue committed
85

Qiang Xue committed
86
	public function setCharset($charset) {}
87

AlexGx committed
88 89 90 91
	public function getFrom()
	{
		return '';
	}
Qiang Xue committed
92

Qiang Xue committed
93
	public function setFrom($from) {}
94

AlexGx committed
95 96 97 98
	public function getReplyTo()
	{
		return '';
	}
Qiang Xue committed
99 100 101

	public function setReplyTo($replyTo) {}

AlexGx committed
102 103 104 105
	public function getTo()
	{
		return '';
	}
Qiang Xue committed
106

Qiang Xue committed
107
	public function setTo($to) {}
108

AlexGx committed
109 110 111 112
	public function getCc()
	{
		return '';
	}
Qiang Xue committed
113

Qiang Xue committed
114
	public function setCc($cc) {}
115

AlexGx committed
116 117 118 119
	public function getBcc()
	{
		return '';
	}
Qiang Xue committed
120

AlexGx committed
121
	public function setBcc($bcc){}
122

AlexGx committed
123 124 125 126
	public function getSubject()
	{
		return '';
	}
Qiang Xue committed
127

Qiang Xue committed
128
	public function setSubject($subject) {}
129

AlexGx committed
130 131
	public function setTextBody($text)
	{
132 133 134
		$this->text = $text;
	}

AlexGx committed
135 136
	public function setHtmlBody($html)
	{
137 138 139 140 141
		$this->html = $html;
	}

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

142
	public function attach($fileName, array $options = []) {}
143

144
	public function embed($fileName, array $options = []) {}
145 146 147

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

148
	public function toString()
149 150 151
	{
		return get_class($this);
	}
Qiang Xue committed
152
}