BaseMailerTest.php 8.55 KB
Newer Older
1 2
<?php

Paul Klimov committed
3
namespace yiiunit\framework\mail;
4 5 6

use Yii;
use yii\base\View;
Paul Klimov committed
7 8
use yii\mail\BaseMailer;
use yii\mail\BaseMessage;
9
use yii\helpers\FileHelper;
10 11 12
use yiiunit\TestCase;

/**
13
 * @group mail
14 15 16 17 18
 */
class BaseMailerTest extends TestCase
{
	public function setUp()
	{
19 20
		$this->mockApplication([
			'components' => [
21
				'mail' => $this->createTestMailComponent(),
22 23
			]
		]);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
		$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();
44 45 46 47 48
	}

	/**
	 * @return Mailer test email component instance.
	 */
49
	protected function createTestMailComponent()
50 51
	{
		$component = new Mailer();
52
		$component->viewPath = $this->getTestFilePath();
53 54 55
		return $component;
	}

56 57 58 59 60 61 62 63
	/**
	 * @return Mailer mailer instance
	 */
	protected function getTestMailComponent()
	{
		return Yii::$app->getComponent('mail');
	}

64 65 66 67 68 69 70 71 72
	// Tests :

	public function testSetupView()
	{
		$mailer = new Mailer();

		$view = new View();
		$mailer->setView($view);
		$this->assertEquals($view, $mailer->getView(), 'Unable to setup view!');
73 74 75 76 77 78 79 80 81 82 83

		$viewConfig = [
			'params' => [
				'param1' => 'value1',
				'param2' => 'value2',
			]
		];
		$mailer->setView($viewConfig);
		$view = $mailer->getView();
		$this->assertTrue(is_object($view), 'Unable to setup view via config!');
		$this->assertEquals($viewConfig['params'], $view->params, 'Unable to configure view via config array!');
84 85
	}

86 87 88
	/**
	 * @depends testSetupView
	 */
89 90 91 92 93 94 95
	public function testGetDefaultView()
	{
		$mailer = new Mailer();
		$view = $mailer->getView();
		$this->assertTrue(is_object($view), 'Unable to get default view!');
	}

96
	public function testCreateMessage()
97 98
	{
		$mailer = new Mailer();
99
		$message = $mailer->compose();
100 101 102 103 104
		$this->assertTrue(is_object($message), 'Unable to create message instance!');
		$this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!');
	}

	/**
105
	 * @depends testCreateMessage
106
	 */
107 108
	public function testDefaultMessageConfig()
	{
109 110
		$mailer = new Mailer();

111
		$notPropertyConfig = [
112
			'charset' => 'utf-16',
113 114 115 116 117
			'from' => 'from@domain.com',
			'to' => 'to@domain.com',
			'cc' => 'cc@domain.com',
			'bcc' => 'bcc@domain.com',
			'subject' => 'Test subject',
118 119
			'textBody' => 'Test text body',
			'htmlBody' => 'Test HTML body',
120 121
		];
		$propertyConfig = [
122 123
			'id' => 'test-id',
			'encoding' => 'test-encoding',
124 125
		];
		$messageConfig = array_merge($notPropertyConfig, $propertyConfig);
126
		$mailer->messageConfig = $messageConfig;
127

128
		$message = $mailer->compose();
129

130 131 132 133
		foreach ($notPropertyConfig as $name => $value) {
			$this->assertEquals($value, $message->{'_' . $name});
		}
		foreach ($propertyConfig as $name => $value) {
134 135 136
			$this->assertEquals($value, $message->$name);
		}
	}
137 138 139 140 141 142

	/**
	 * @depends testGetDefaultView
	 */
	public function testRender()
	{
143
		$mailer = $this->getTestMailComponent();
144 145

		$viewName = 'test_view';
146
		$viewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $viewName . '.php';
147 148
		$viewFileContent = '<?php echo $testParam; ?>';
		file_put_contents($viewFileName, $viewFileContent);
149 150 151 152 153 154 155

		$params = [
			'testParam' => 'test output'
		];
		$renderResult = $mailer->render($viewName, $params);
		$this->assertEquals($params['testParam'], $renderResult);
	}
156 157 158 159 160 161

	/**
	 * @depends testRender
	 */
	public function testRenderLayout()
	{
162
		$mailer = $this->getTestMailComponent();
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

		$filePath = $this->getTestFilePath();

		$viewName = 'test_view';
		$viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
		$viewFileContent = 'view file content';
		file_put_contents($viewFileName, $viewFileContent);

		$layoutName = 'test_layout';
		$layoutFileName = $filePath . DIRECTORY_SEPARATOR . $layoutName . '.php';
		$layoutFileContent = 'Begin Layout <?php echo $content; ?> End Layout';
		file_put_contents($layoutFileName, $layoutFileContent);

		$renderResult = $mailer->render($viewName, [], $layoutName);
		$this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult);
	}
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	/**
	 * @depends testCreateMessage
	 * @depends testRender
	 */
	public function testCompose()
	{
		$mailer = $this->getTestMailComponent();
		$mailer->htmlLayout = false;
		$mailer->textLayout = false;

		$htmlViewName = 'test_html_view';
		$htmlViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $htmlViewName . '.php';
		$htmlViewFileContent = 'HTML <b>view file</b> content';
		file_put_contents($htmlViewFileName, $htmlViewFileContent);
		
		$textViewName = 'test_text_view';
		$textViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $textViewName . '.php';
		$textViewFileContent = 'Plain text view file content';
		file_put_contents($textViewFileName, $textViewFileContent);

		$message = $mailer->compose([
			'html' => $htmlViewName,
			'text' => $textViewName,
		]);
204 205
		$this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html!');
		$this->assertEquals($textViewFileContent, $message->_textBody, 'Unable to render text!');
206 207

		$message = $mailer->compose($htmlViewName);
208 209
		$this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html by direct view!');
		$this->assertEquals(strip_tags($htmlViewFileContent), $message->_textBody, 'Unable to render text by direct view!');
210
	}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	public function testUseFileTransport()
	{
		$mailer = new Mailer();
		$this->assertFalse($mailer->useFileTransport);
		$this->assertEquals('@runtime/mail', $mailer->fileTransportPath);

		$mailer->fileTransportPath = '@yiiunit/runtime/mail';
		$mailer->useFileTransport = true;
		$mailer->fileTransportCallback = function () {
			return 'message.txt';
		};
		$message = $mailer->compose()
			->setTo('to@example.com')
			->setFrom('from@example.com')
			->setSubject('test subject')
			->setTextBody('text body' . microtime(true));
		$this->assertTrue($mailer->send($message));
		$file = Yii::getAlias($mailer->fileTransportPath) . '/message.txt';
		$this->assertTrue(is_file($file));
		$this->assertEquals($message->toString(), file_get_contents($file));
	}
Mark committed
233 234 235 236 237 238 239 240 241 242 243

	public function testBeforeSendEvent()
	{
		$message = new Message();

		$mailerMock = $this->getMockBuilder('yiiunit\framework\mail\Mailer')->setMethods(['beforeSend','afterSend'])->getMock();
		$mailerMock->expects($this->once())->method('beforeSend')->with($message)->will($this->returnValue(true));
		$mailerMock->expects($this->once())->method('afterSend')->with($message,true);
		$mailerMock->send($message);
	}

244 245 246 247 248 249 250
}

/**
 * Test Mailer class
 */
class Mailer extends BaseMailer
{
251
	public $messageClass = 'yiiunit\framework\mail\Message';
252
	public $sentMessages = [];
253

254
	protected function sendMessage($message)
255 256
	{
		$this->sentMessages[] = $message;
Mark committed
257
		return true;
258 259 260 261 262 263 264 265 266 267
	}
}

/**
 * Test Message class
 */
class Message extends BaseMessage
{
	public $id;
	public $encoding;
268
	public $_charset;
269
	public $_from;
Qiang Xue committed
270
	public $_replyTo;
271 272 273 274
	public $_to;
	public $_cc;
	public $_bcc;
	public $_subject;
275 276
	public $_textBody;
	public $_htmlBody;
277

Qiang Xue committed
278 279 280 281 282
	public function getCharset()
	{
		return $this->_charset;
	}

Qiang Xue committed
283
	public function setCharset($charset)
284 285 286 287
	{
		$this->_charset = $charset;
		return $this;
	}
288

Qiang Xue committed
289 290 291 292 293
	public function getFrom()
	{
		return $this->_from;
	}

Qiang Xue committed
294
	public function setFrom($from)
295
	{
296 297 298
		$this->_from = $from;
		return $this;
	}
299

Qiang Xue committed
300 301 302 303 304
	public function getTo()
	{
		return $this->_to;
	}

Qiang Xue committed
305
	public function setTo($to)
306
	{
307 308 309
		$this->_to = $to;
		return $this;
	}
310

Qiang Xue committed
311 312 313 314 315
	public function getCc()
	{
		return $this->_cc;
	}

Qiang Xue committed
316
	public function setCc($cc)
317
	{
318 319 320
		$this->_cc = $cc;
		return $this;
	}
321

Qiang Xue committed
322 323 324 325 326
	public function getBcc()
	{
		return $this->_bcc;
	}

Qiang Xue committed
327
	public function setBcc($bcc)
328
	{
329 330 331
		$this->_bcc = $bcc;
		return $this;
	}
332

Qiang Xue committed
333 334 335 336 337
	public function getSubject()
	{
		return $this->_subject;
	}

Qiang Xue committed
338
	public function setSubject($subject)
339
	{
340 341 342
		$this->_subject = $subject;
		return $this;
	}
343

Qiang Xue committed
344 345 346 347 348 349 350 351 352 353 354
	public function getReplyTo()
	{
		return $this->_replyTo;
	}

	public function setReplyTo($replyTo)
	{
		$this->_replyTo = $replyTo;
		return $this;
	}

Qiang Xue committed
355
	public function setTextBody($text)
356
	{
357
		$this->_textBody = $text;
358 359
		return $this;
	}
360

Qiang Xue committed
361
	public function setHtmlBody($html)
362
	{
363
		$this->_htmlBody = $html;
364 365
		return $this;
	}
366

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

369
	public function attach($fileName, array $options = []) {}
370

371
	public function embed($fileName, array $options = []) {}
372 373

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

375
	public function toString()
376
	{
377
		return var_export($this, true);
378
	}
Qiang Xue committed
379
}