BaseMessageTest.php 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace yiiunit\framework\mail;

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

/**
 * @group mail
 */
class BaseMessageTest extends TestCase
{
15 16 17 18
    public function setUp()
    {
        $this->mockApplication([
            'components' => [
19
                'mailer' => $this->createTestEmailComponent()
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
            ]
        ]);
    }

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

        return $component;
    }

    /**
     * @return TestMailer mailer instance.
     */
    protected function getMailer()
    {
39
        return Yii::$app->get('mailer');
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    }

    // Tests :

    public function testSend()
    {
        $mailer = $this->getMailer();
        $message = $mailer->compose();
        $message->send($mailer);
        $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
    }

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

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

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

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

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

87
    public function setCharset($charset) {}
88

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

94
    public function setFrom($from) {}
95

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

101
    public function setReplyTo($replyTo) {}
Qiang Xue committed
102

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

108
    public function setTo($to) {}
109

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

115
    public function setCc($cc) {}
116

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

122
    public function setBcc($bcc) {}
123

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

129
    public function setSubject($subject) {}
130

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

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

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

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

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

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

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