Skip to content

Commit

Permalink
Docker Compose 構成でも PHPUnit のメール関連のテストを実行する
Browse files Browse the repository at this point in the history
  • Loading branch information
seasoftjapan committed Sep 6, 2024
1 parent f00fa65 commit 49e834e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ services:
SMTP_PORT: 1025
SMTP_USER: ~
SMTP_PASSWORD: ~
TEST_MAILCATCHER_URL: "http://mailcatcher:1080"
networks:
- backend

Expand Down
1 change: 1 addition & 0 deletions eccube_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ defined('SMTP_HOST') or define('SMTP_HOST', '${SMTP_HOST}');
defined('SMTP_PORT') or define('SMTP_PORT', '${SMTP_PORT}');
defined('SMTP_USER') or define('SMTP_USER', '${SMTP_USER}');
defined('SMTP_PASSWORD') or define('SMTP_PASSWORD', '${SMTP_PASSWORD}');
defined('TEST_MAILCATCHER_URL') or define('TEST_MAILCATCHER_URL', '${TEST_MAILCATCHER_URL-"http://127.0.0.1:1080"}');
__EOF__

Expand Down
22 changes: 13 additions & 9 deletions tests/class/Common_TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
class Common_TestCase extends PHPUnit_Framework_TestCase
{
/** MailCatcher の URL. */
const MAILCATCHER_URL = 'http://127.0.0.1:1080';

/**
* MDB2 をグローバル変数のバックアップ対象から除外する。
*
Expand Down Expand Up @@ -49,6 +46,9 @@ protected function setUp()
$this->objQuery = SC_Query_Ex::getSingletonInstance('', true);
$this->objQuery->begin();
$this->objGenerator = new \Eccube2\Tests\Fixture\Generator($this->objQuery);
if (!defined('TEST_MAILCATCHER_URL')) {
$this->markTestSkipped('TEST_MAILCATCHER_URL is not defined.');
}
}

protected function tearDown()
Expand Down Expand Up @@ -77,14 +77,18 @@ protected function checkMailCatcherStatus()
$context = stream_context_create(array(
'http' => array('ignore_errors' => true)
));
$response = file_get_contents(self::MAILCATCHER_URL.'/messages', false, $context);
$response = file_get_contents(TEST_MAILCATCHER_URL.'/messages', false, $context);

$http_status = strpos($http_response_header[0], '200');
if ($http_status === false) {
$this->markTestSkipped('MailCatcher is not available');
throw new Exception('Response code is not 200: $http_response_header[0] = ' . var_export($http_response_header[0], true));
}

if ($response === false) {
throw new Exception('file_get_contents response is false.');
}
} catch (Exception $e) {
$this->markTestSkipped('MailCatcher is not available');
$this->markTestSkipped('MailCatcher is not available: ' . $e->getMessage());
}
}

Expand All @@ -102,7 +106,7 @@ protected function resetEmails()
)
);

file_get_contents(self::MAILCATCHER_URL.'/messages', false, $context);
file_get_contents(TEST_MAILCATCHER_URL.'/messages', false, $context);

} catch (\Exception $e) {
// quiet
Expand All @@ -116,7 +120,7 @@ protected function resetEmails()
*/
protected function getMailCatcherMessages()
{
return json_decode(file_get_contents(self::MAILCATCHER_URL. '/messages'), true);
return json_decode(file_get_contents(TEST_MAILCATCHER_URL. '/messages'), true);
}

/**
Expand All @@ -127,7 +131,7 @@ protected function getMailCatcherMessages()
*/
protected function getMailCatcherMessage($message)
{
$source = file_get_contents(self::MAILCATCHER_URL. '/messages/'.$message['id'].'.source');
$source = file_get_contents(TEST_MAILCATCHER_URL. '/messages/'.$message['id'].'.source');

$message['source'] = quoted_printable_decode($source);
$message['source'] = mb_convert_encoding($message['source'], 'UTF-8', 'JIS');
Expand Down
4 changes: 2 additions & 2 deletions tests/class/SC_SendMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public function testGetBackendParams()
$this->verify();

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

0 comments on commit 49e834e

Please sign in to comment.