Skip to content

Commit

Permalink
Merge pull request #256 from nanasess/add-sendmailtest
Browse files Browse the repository at this point in the history
SC_SendMail のテストを追加
  • Loading branch information
Chihiro Adachi committed Apr 23, 2019
2 parents 86bb67b + 12713ff commit b472a43
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/class/Common_TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
class Common_TestCase extends PHPUnit_Framework_TestCase
{
/** MailCatcher の URL. */
const MAILCATCHER_URL = 'http://127.0.0.1:1080';

/**
* MDB2 をグローバル変数のバックアップ対象から除外する。
*
Expand Down Expand Up @@ -59,6 +62,85 @@ protected function verify($message = null)
$this->assertEquals($this->expected, $this->actual, $message);
}

/**
* MailCatcher の起動状態をチェックする.
*
* MailCatcher が起動していない場合は, テストをスキップする.
*/
protected function checkMailCatcherStatus()
{
try {
$client = new \GuzzleHttp\Client(['base_url' => self::MAILCATCHER_URL]);
$response = $client->get('/messages');
if ($response->getStatusCode() !== 200) {
$this->markTestSkipped('MailCatcher is not available');
}
} catch (Exception $e) {
$this->markTestSkipped('MailCatcher is not available');
}
}

/**
* MailCatcher のメッセージをすべて削除する.
*/
protected function resetEmails()
{
try {
$client = new \GuzzleHttp\Client(['base_url' => self::MAILCATCHER_URL]);
$client->delete('/messages');
} catch (\Exception $e) {
// quiet
}
}

/**
* MailCatcher のメッセージをすべて取得する.
*
* @return array MailCatcher のメッセージの配列
*/
protected function getMailCatcherMessages()
{
$client = new \GuzzleHttp\Client(['base_url' => self::MAILCATCHER_URL]);
$response = $client->get('/messages');

return json_decode($response->getBody(true), true);
}

/**
* MailCatcher のメッセージを ID を指定して取得する.
*
* @param int $id メッセージの ID
* @return array MailCatcher のメッセージ
*/
protected function getMailCatcherMessage($id)
{
$client = new \GuzzleHttp\Client(['base_url' => self::MAILCATCHER_URL]);
$response = $client->get('/messages/'.$id.'.json');

$message = json_decode($response->getBody(true), true);

$message['source'] = quoted_printable_decode($message['source']);
$message['source'] = mb_convert_encoding($message['source'], 'UTF-8', 'JIS');
return $message;
}

/**
* MailCatcher の最後のメッセージ取得する.
*
* @return array MailCatcher のメッセージ
*/
protected function getLastMailCatcherMessage()
{
$messages = $this->getMailCatcherMessages();
if (empty($messages)) {
$this->fail("No messages received");
}

$last = array_shift($messages);
return $this->getMailCatcherMessage($last['id']);
}


//////////////////////////////////////////////////////////////////
// 以下はテスト用のユーティリティを使うためのサンプルです。
// 実際に動作させる場合にはコメントアウトを外して下さい。
Expand Down
172 changes: 172 additions & 0 deletions tests/class/SC_SendMailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

class SC_SendMailTest extends Common_TestCase
{
/**
* @var SC_SendMail
*/
protected $objSendMail;

protected function setUp()
{
parent::setUp();
$this->checkMailCatcherStatus();
$this->objSendMail = new SC_SendMail_Ex();
}

public function testGetInstance()
{
$this->assertInstanceOf('SC_SendMail', $this->objSendMail);
}

public function testSendMail()
{
$this->resetEmails();

$this->objSendMail->setItem('to@example.com', '件名', '本文', 'from@example.com', '差出人名', 'reply-to@example.com', 'return-path@example.com', 'error-to@example.com', 'bcc@example.com', 'cc@example.com');
$result = $this->objSendMail->sendMail();
$this->assertTrue($result);

$messages = $this->getMailCatcherMessages();
$this->assertCount(1, $messages);

$message = $this->getLastMailCatcherMessage();
$this->assertEquals('件名', $message['subject']);
$this->assertContains('本文', $message['source']);

$this->assertContains('text/plain', $message['source']);

$this->assertContains('Return-Path: error-to@example.com', $message['source']);
}

public function testSendHtmlMail()
{
$this->resetEmails();

$this->objSendMail->setItemHtml('to@example.com', '件名', '<p>本文</p>', 'from@example.com', '差出人名', 'reply-to@example.com', 'return-path@example.com', 'error-to@example.com', 'bcc@example.com', 'cc@example.com');
$result = $this->objSendMail->sendHtmlMail();
$this->assertTrue($result);

$messages = $this->getMailCatcherMessages();
$this->assertCount(1, $messages);

$message = $this->getLastMailCatcherMessage();
$this->assertEquals('件名', $message['subject']);
$this->assertContains('<p>本文</p>', $message['source']);

$this->assertContains('text/html', $message['source']);
}

public function testSetReturnPathToSendMail()
{
$this->resetEmails();

$this->objSendMail->setBase('to@example.com', '件名', '本文', 'from@example.com', '差出人名', 'reply-to@example.com');

$this->objSendMail->setReturnPath('return-path@example.com');
$result = $this->objSendMail->sendMail();
$this->assertTrue($result);

$messages = $this->getMailCatcherMessages();
$this->assertCount(1, $messages);

$message = $this->getLastMailCatcherMessage();
$this->assertEquals('件名', $message['subject']);
$this->assertContains('本文', $message['source']);
$this->assertContains('text/plain', $message['source']);

$this->assertContains('Return-Path: return-path@example.com', $message['source']);
}

public function testUnsetErrorToSendMail()
{
$this->resetEmails();

$this->objSendMail->setItem('to@example.com', '件名', '本文', 'from@example.com', '差出人名', 'reply-to@example.com', 'return-path@example.com');
$result = $this->objSendMail->sendMail();
$this->assertTrue($result);

$messages = $this->getMailCatcherMessages();
$this->assertCount(1, $messages);

$message = $this->getLastMailCatcherMessage();
$this->assertEquals('件名', $message['subject']);
$this->assertContains('本文', $message['source']);

$this->assertContains('text/plain', $message['source']);

$this->assertContains('Return-Path: return-path@example.com', $message['source']);
}

public function testUnsetReturnPathToSendMail()
{
$this->resetEmails();

$this->objSendMail->setItem('to@example.com', '件名', '本文', 'from@example.com', '差出人名', 'reply-to@example.com');
$result = $this->objSendMail->sendMail();
$this->assertTrue($result);

$messages = $this->getMailCatcherMessages();
$this->assertCount(1, $messages);

$message = $this->getLastMailCatcherMessage();
$this->assertEquals('件名', $message['subject']);
$this->assertContains('本文', $message['source']);

$this->assertContains('text/plain', $message['source']);

$this->assertContains('Return-Path: from@example.com', $message['source']);
}

public function testGetRecip()
{
$this->objSendMail->setItem('to@example.com', '件名', '本文', 'from@example.com', '差出人名', 'reply-to@example.com');

$this->objSendMail->backend = 'mail';
$this->expected = 'to@example.com';
$this->actual = $this->objSendMail->getRecip();
$this->verify();

$this->objSendMail->backend = 'smtp';
$this->expected = [
'To' => 'to@example.com'
];
$this->actual = $this->objSendMail->getRecip();
$this->verify();

$this->objSendMail->backend = 'sendmail';
$this->expected = [
'To' => 'to@example.com'
];
$this->actual = $this->objSendMail->getRecip();
$this->verify();
}

public function testGetBackendParams()
{
$this->expected = [];
$this->actual = $this->objSendMail->getBackendParams('mail');
$this->verify();

$this->expected = [
'sendmail_path' => '/usr/bin/sendmail',
'sendmail_args' => '-i',
];
$this->actual = $this->objSendMail->getBackendParams('sendmail');
$this->verify();

$this->expected = [
'host' => '127.0.0.1',
'port' => '1025'
];
$this->actual = $this->objSendMail->getBackendParams('smtp');
$this->verify();

$this->expected = [
'host' => '127.0.0.1',
'port' => '1025'
];
$this->actual = $this->objSendMail->getBackendParams('smtp');
$this->verify();
}
}

0 comments on commit b472a43

Please sign in to comment.