BaseMessageTest.php 2.46 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 testGetMailer()
44 45
	{
		$mailer = $this->getMailer();
46 47
		$message = $mailer->compose();
		$this->assertEquals($mailer, $message->getMailer());
48 49
	}

50
	public function testSend()
51 52
	{
		$mailer = $this->getMailer();
53 54 55
		$message = $mailer->compose();
		$message->send();
		$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
56 57
	}

58
	public function testToString()
59 60
	{
		$mailer = $this->getMailer();
61 62
		$message = $mailer->compose();
		$this->assertEquals($message->toString(), '' . $message);
63 64 65 66 67 68 69 70 71 72 73
	}
}

/**
 * Test Mailer class
 */
class TestMailer extends BaseMailer
{
	public $messageClass = 'yiiunit\framework\mail\TestMessage';
	public $sentMessages = array();

74
	protected function sendMessage($message)
75 76 77 78 79 80 81 82 83 84 85 86 87
	{
		$this->sentMessages[] = $message;
	}
}

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

Qiang Xue committed
88 89
	public function getCharset() {return '';}

Qiang Xue committed
90
	public function setCharset($charset) {}
91

Qiang Xue committed
92 93
	public function getFrom() {return '';}

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

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

	public function setReplyTo($replyTo) {}

	public function getTo() {return '';}

Qiang Xue committed
102
	public function setTo($to) {}
103

Qiang Xue committed
104 105
	public function getCc() {return '';}

Qiang Xue committed
106
	public function setCc($cc) {}
107

Qiang Xue committed
108 109
	public function getBcc() {return '';}

Qiang Xue committed
110
	public function setBcc($bcc) {}
111

Qiang Xue committed
112 113
	public function getSubject() {return '';}

Qiang Xue committed
114
	public function setSubject($subject) {}
115

Qiang Xue committed
116
	public function setTextBody($text) {
117 118 119
		$this->text = $text;
	}

Qiang Xue committed
120
	public function setHtmlBody($html) {
121 122 123 124 125
		$this->html = $html;
	}

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

126
	public function attach($fileName, array $options = []) {}
127

128
	public function embed($fileName, array $options = []) {}
129 130 131

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

132
	public function toString()
133 134 135
	{
		return get_class($this);
	}
Qiang Xue committed
136
}