FileMailerTest.php 1.29 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<?php

namespace yiiunit\extensions\swiftmailer;

require __DIR__ . '/../../../../extensions/swiftmailer/FileMailer.php';

use yii\swiftmailer\FileMailer;
use yiiunit\VendorTestCase;

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

	/**
	 * @return FileMailer test email component instance.
	 */
	protected function createTestEmailComponent()
	{
		$component = new FileMailer([
			'callback' => function () {
				return 'Message_test.txt';
			}
		]);

		return $component;
	}

	public function testConfigurePath()
	{
		$mailer = new FileMailer();
		$this->assertEquals(\Yii::getAlias('@app/runtime/mail'), $mailer->getPath());
		$mailer->setPath('@yiiunit/runtime/');
		$this->assertEquals(\Yii::getAlias('@yiiunit/runtime'), $mailer->getPath());
	}

	public function testSend()
	{
		$message = \Yii::$app->mail->compose()
			->setTo('tester@example.com')
			->setFrom('admin@example.com')
			->setSubject('Just a test')
			->setHtmlBody('This is html body');
		$message->send();
		$this->assertEquals(
			$message->toString(),
			file_get_contents(\Yii::$app->getRuntimePath() . '/mail/Message_test.txt')
		);
	}
}