MessageTest.php 10.4 KB
Newer Older
1 2
<?php

Paul Klimov committed
3
namespace yiiunit\extensions\swiftmailer;
4 5

use Yii;
6
use yii\helpers\FileHelper;
Paul Klimov committed
7 8
use yii\swiftmailer\Mailer;
use yii\swiftmailer\Message;
9
use yiiunit\VendorTestCase;
10

Qiang Xue committed
11
Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer');
12

13
/**
14
 * @group vendor
15
 * @group mail
16 17
 * @group swiftmailer
 */
18
class MessageTest extends VendorTestCase
19
{
20 21 22 23 24
	/**
	 * @var string test email address, which will be used as receiver for the messages.
	 */
	protected $testEmailReceiver = 'someuser@somedomain.com';

25 26
	public function setUp()
	{
27 28
		$this->mockApplication([
			'components' => [
29
				'mail' => $this->createTestEmailComponent()
30 31
			]
		]);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		$filePath = $this->getTestFilePath();
		if (!file_exists($filePath)) {
			FileHelper::createDirectory($filePath);
		}
	}

	public function tearDown()
	{
		$filePath = $this->getTestFilePath();
		if (file_exists($filePath)) {
			FileHelper::removeDirectory($filePath);
		}
	}

	/**
	 * @return string test file path.
	 */
	protected function getTestFilePath()
	{
		return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
52 53 54 55 56 57 58
	}

	/**
	 * @return Mailer test email component instance.
	 */
	protected function createTestEmailComponent()
	{
59 60 61
		$component = new Mailer([
			'useFileTransport' => true,
		]);
62 63 64
		return $component;
	}

65 66 67 68 69
	/**
	 * @return Message test message instance.
	 */
	protected function createTestMessage()
	{
70
		return Yii::$app->getComponent('mail')->compose();
71 72
	}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	/**
	 * Creates image file with given text.
	 * @param string $fileName file name.
	 * @param string $text text to be applied on image.
	 * @return string image file full name.
	 */
	protected function createImageFile($fileName = 'test.jpg', $text = 'Test Image')
	{
		if (!function_exists('imagecreatetruecolor')) {
			$this->markTestSkipped('GD lib required.');
		}
		$fileFullName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $fileName;
		$image = imagecreatetruecolor(120, 20);
		$textColor = imagecolorallocate($image, 233, 14, 91);
		imagestring($image, 1, 5, 5, $text, $textColor);
		imagejpeg($image, $fileFullName);
		imagedestroy($image);
		return $fileFullName;
	}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	/**
	 * Finds the attachment object in the message.
	 * @param Message $message message instance
	 * @return null|\Swift_Mime_Attachment attachment instance.
	 */
	protected function getAttachment(Message $message)
	{
		$messageParts = $message->getSwiftMessage()->getChildren();
		$attachment = null;
		foreach ($messageParts as $part) {
			if ($part instanceof \Swift_Mime_Attachment) {
				$attachment = $part;
				break;
			}
		}
		return $attachment;
	}

111 112 113 114 115 116 117
	// Tests :

	public function testGetSwiftMessage()
	{
		$message = new Message();
		$this->assertTrue(is_object($message->getSwiftMessage()), 'Unable to get Swift message!');
	}
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	/**
	 * @depends testGetSwiftMessage
	 */
	public function testSetGet()
	{
		$message = new Message();

		$charset = 'utf-16';
		$message->setCharset($charset);
		$this->assertEquals($charset, $message->getCharset(), 'Unable to set charset!');

		$subject = 'Test Subject';
		$message->setSubject($subject);
		$this->assertEquals($subject, $message->getSubject(), 'Unable to set subject!');

		$from = 'from@somedomain.com';
		$message->setFrom($from);
		$this->assertContains($from, array_keys($message->getFrom()), 'Unable to set from!');

		$replyTo = 'reply-to@somedomain.com';
		$message->setReplyTo($replyTo);
		$this->assertContains($replyTo, array_keys($message->getReplyTo()), 'Unable to set replyTo!');

		$to = 'someuser@somedomain.com';
		$message->setTo($to);
		$this->assertContains($to, array_keys($message->getTo()), 'Unable to set to!');

		$cc = 'ccuser@somedomain.com';
		$message->setCc($cc);
		$this->assertContains($cc, array_keys($message->getCc()), 'Unable to set cc!');

		$bcc = 'bccuser@somedomain.com';
		$message->setBcc($bcc);
		$this->assertContains($bcc, array_keys($message->getBcc()), 'Unable to set bcc!');
	}

155 156 157 158 159 160 161
	/**
	 * @depends testGetSwiftMessage
	 */
	public function testSetupHeaders()
	{
		$charset = 'utf-16';
		$subject = 'Test Subject';
162 163
		$from = 'from@somedomain.com';
		$replyTo = 'reply-to@somedomain.com';
164
		$to = 'someuser@somedomain.com';
165 166
		$cc = 'ccuser@somedomain.com';
		$bcc = 'bccuser@somedomain.com';
167 168

		$messageString = $this->createTestMessage()
Qiang Xue committed
169 170
			->setCharset($charset)
			->setSubject($subject)
171 172
			->setFrom($from)
			->setReplyTo($replyTo)
Qiang Xue committed
173 174 175
			->setTo($to)
			->setCc($cc)
			->setBcc($bcc)
176
			->toString();
177 178 179

		$this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!');
		$this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" header!');
180 181
		$this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!');
		$this->assertContains('Reply-To: ' . $replyTo, $messageString, 'Incorrect "Reply-To" header!');
182
		$this->assertContains('To: ' . $to, $messageString, 'Incorrect "To" header!');
183 184
		$this->assertContains('Cc: ' . $cc, $messageString, 'Incorrect "Cc" header!');
		$this->assertContains('Bcc: ' . $bcc, $messageString, 'Incorrect "Bcc" header!');
185 186
	}

187 188 189 190 191
	/**
	 * @depends testGetSwiftMessage
	 */
	public function testSend()
	{
192
		$message = $this->createTestMessage();
Qiang Xue committed
193 194 195 196
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Test');
		$message->setTextBody('Yii Swift Test body');
197 198 199 200 201 202 203 204
		$this->assertTrue($message->send());
	}

	/**
	 * @depends testSend
	 */
	public function testAttachFile()
	{
205
		$message = $this->createTestMessage();
206

Qiang Xue committed
207 208 209 210
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Attach File Test');
		$message->setTextBody('Yii Swift Attach File Test body');
211
		$fileName = __FILE__;
212
		$message->attach($fileName);
213

214
		$this->assertTrue($message->send());
215 216 217 218

		$attachment = $this->getAttachment($message);
		$this->assertTrue(is_object($attachment), 'No attachment found!');
		$this->assertContains($attachment->getFilename(), $fileName, 'Invalid file name!');
219 220 221 222 223
	}

	/**
	 * @depends testSend
	 */
224
	public function testAttachContent()
225
	{
226
		$message = $this->createTestMessage();
227

Qiang Xue committed
228 229 230 231
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Create Attachment Test');
		$message->setTextBody('Yii Swift Create Attachment Test body');
232 233 234 235
		$fileName = 'test.txt';
		$fileContent = 'Test attachment content';
		$message->attachContent($fileContent, ['fileName' => $fileName]);

236
		$this->assertTrue($message->send());
237 238 239 240

		$attachment = $this->getAttachment($message);
		$this->assertTrue(is_object($attachment), 'No attachment found!');
		$this->assertEquals($fileName, $attachment->getFilename(), 'Invalid file name!');
241 242 243 244 245 246 247 248 249 250 251
	}

	/**
	 * @depends testSend
	 */
	public function testEmbedFile()
	{
		$fileName = $this->createImageFile('embed_file.jpg', 'Embed Image File');

		$message = $this->createTestMessage();

252
		$cid = $message->embed($fileName);
253

Qiang Xue committed
254 255 256 257
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Embed File Test');
		$message->setHtmlBody('Embed image: <img src="' . $cid. '" alt="pic">');
258 259

		$this->assertTrue($message->send());
260 261 262 263

		$attachment = $this->getAttachment($message);
		$this->assertTrue(is_object($attachment), 'No attachment found!');
		$this->assertContains($attachment->getFilename(), $fileName, 'Invalid file name!');
264 265 266 267 268 269 270
	}

	/**
	 * @depends testSend
	 */
	public function testEmbedContent()
	{
271
		$fileFullName = $this->createImageFile('embed_file.jpg', 'Embed Image File');
272 273
		$message = $this->createTestMessage();

274 275 276 277 278
		$fileName = basename($fileFullName);
		$contentType = 'image/jpeg';
		$fileContent = file_get_contents($fileFullName);

		$cid = $message->embedContent($fileContent, ['fileName' => $fileName, 'contentType' => $contentType]);
279

Qiang Xue committed
280 281 282 283
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Embed File Test');
		$message->setHtmlBody('Embed image: <img src="' . $cid. '" alt="pic">');
284

285
		$this->assertTrue($message->send());
286 287 288 289 290

		$attachment = $this->getAttachment($message);
		$this->assertTrue(is_object($attachment), 'No attachment found!');
		$this->assertEquals($fileName, $attachment->getFilename(), 'Invalid file name!');
		$this->assertEquals($contentType, $attachment->getContentType(), 'Invalid content type!');
291 292 293 294 295 296 297
	}

	/**
	 * @depends testSend
	 */
	public function testSendAlternativeBody()
	{
298
		$message = $this->createTestMessage();
299

Qiang Xue committed
300 301 302 303 304
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Alternative Body Test');
		$message->setHtmlBody('<b>Yii Swift</b> test HTML body');
		$message->setTextBody('Yii Swift test plain text body');
305

306
		$this->assertTrue($message->send());
307 308 309 310 311 312

		$messageParts = $message->getSwiftMessage()->getChildren();
		$textPresent = false;
		$htmlPresent = false;
		foreach ($messageParts as $part) {
			if (!($part instanceof \Swift_Mime_Attachment)) {
slavcodev committed
313
				/* @var \Swift_Mime_MimePart $part */
314 315 316 317 318 319 320 321 322 323
				if ($part->getContentType() == 'text/plain') {
					$textPresent = true;
				}
				if ($part->getContentType() == 'text/html') {
					$htmlPresent = true;
				}
			}
		}
		$this->assertTrue($textPresent, 'No text!');
		$this->assertTrue($htmlPresent, 'No HTML!');
324
	}
325 326 327 328 329 330 331 332

	/**
	 * @depends testGetSwiftMessage
	 */
	public function testSerialize()
	{
		$message = $this->createTestMessage();

Qiang Xue committed
333 334 335 336
		$message->setTo($this->testEmailReceiver);
		$message->setFrom('someuser@somedomain.com');
		$message->setSubject('Yii Swift Alternative Body Test');
		$message->setTextBody('Yii Swift test plain text body');
337 338 339 340 341 342 343

		$serializedMessage = serialize($message);
		$this->assertNotEmpty($serializedMessage, 'Unable to serialize message!');

		$unserializedMessaage = unserialize($serializedMessage);
		$this->assertEquals($message, $unserializedMessaage, 'Unable to unserialize message!');
	}
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

	/**
	 * @depends testSendAlternativeBody
	 */
	public function testAlternativeBodyCharset()
	{
		$message = $this->createTestMessage();
		$charset = 'windows-1251';
		$message->setCharset($charset);

		$message->setTextBody('some text');
		$message->setHtmlBody('some html');
		$content = $message->toString();
		$this->assertEquals(2, substr_count($content, $charset), 'Wrong charset for alternative body.');

		$message->setTextBody('some text override');
		$content = $message->toString();
		$this->assertEquals(2, substr_count($content, $charset), 'Wrong charset for alternative body override.');
	}
Qiang Xue committed
363
}