You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/MailDeliveryLogTest.php

214 lines
6.0 KiB
PHTML

<?php
namespace App\Models {
#[\AllowDynamicProperties]
class EmailLog
{
public static array $rows = [];
public ?int $id = null;
public function save(): bool
{
if ($this->id === null) {
$this->id = count(self::$rows) + 1;
}
self::$rows[$this->id] = get_object_vars($this);
return true;
}
}
}
namespace App\Services {
class Config
{
public static array $values = [
'mailDriver' => 'smtp',
'smtp_host' => 'smtp.example.test',
'smtp_username' => 'test-user',
'smtp_port' => '587',
'smtp_sender' => 'sender@example.test',
'smtp_name' => 'Test sender',
'smtp_passsword' => 'test-password',
'smtp_ssl' => 'true',
];
public static function get($key, $default = null)
{
return self::$values[$key] ?? $default;
}
public static function getPublicConfig(): array
{
return ['appName' => 'Test site'];
}
}
class RedisClient
{
public static string $blacklist = '';
public function get($key, $ttl, $callback): string
{
return self::$blacklist;
}
}
}
namespace PHPMailer\PHPMailer {
class PHPMailer
{
public static bool $sendResult = true;
public static bool $throwOnSend = false;
public static int $sendCalls = 0;
public string $Host = '';
public bool $SMTPAuth = false;
public string $Username = '';
public string $Password = '';
public string $SMTPSecure = '';
public array $SMTPOptions = [];
public string $Port = '';
public int $Timeout = 0;
public int $Timelimit = 0;
public string $CharSet = '';
public string $Subject = '';
public string $Body = '';
public function isSMTP(): void
{
}
public function setFrom($address, $name): void
{
}
public function addAddress($address): void
{
}
public function isHTML($enabled): void
{
}
public function addAttachment($file): void
{
}
public function send(): bool
{
self::$sendCalls++;
if (self::$throwOnSend) {
throw new \RuntimeException('Mock SMTP transport exception');
}
return self::$sendResult;
}
}
}
namespace {
use App\Models\EmailLog;
use App\Services\Config;
use App\Services\Mail;
use App\Services\RedisClient;
use PHPMailer\PHPMailer\PHPMailer;
class Smarty
{
public static bool $throwOnFetch = false;
public function settemplatedir($directory): void
{
}
public function setcompiledir($directory): void
{
}
public function setcachedir($directory): void
{
}
public function assign($key, $value): void
{
}
public function fetch($template): string
{
if (self::$throwOnFetch) {
throw new \RuntimeException('Mock template rendering exception');
}
return 'Rendered ' . $template;
}
}
define('BASE_PATH', dirname(__DIR__));
$_SERVER['REMOTE_ADDR'] = '192.0.2.10';
$_SERVER['HTTP_HOST'] = 'site.example.test';
require dirname(__DIR__) . '/app/Services/Mail/Base.php';
require dirname(__DIR__) . '/app/Services/Mail/Smtp.php';
require dirname(__DIR__) . '/app/Services/Mail/NullMail.php';
require dirname(__DIR__) . '/app/Services/Mail.php';
function expectMailDeliveryLog($condition, string $message): void
{
if (!$condition) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
}
function checkMailDelivery(
string $case,
string $to,
bool $expectedResult,
int $expectedStatus,
bool $transportCalled,
?string $emailType = null
): void
{
$beforeRows = count(EmailLog::$rows);
$beforeCalls = PHPMailer::$sendCalls;
$actual = Mail::send($to, $case, 'news/warn.tpl', ['text' => $case], [], $emailType);
expectMailDeliveryLog($actual === $expectedResult, $case . ': wrong send result.');
expectMailDeliveryLog(count(EmailLog::$rows) === $beforeRows + 1, $case . ': expected exactly one email log row.');
expectMailDeliveryLog(
PHPMailer::$sendCalls === $beforeCalls + ($transportCalled ? 1 : 0),
$case . ': wrong number of SMTP transport calls.'
);
$row = EmailLog::$rows[$beforeRows + 1];
expectMailDeliveryLog(($row['email'] ?? null) === $to, $case . ': wrong log recipient.');
expectMailDeliveryLog(
($row['email_type'] ?? null) === ($emailType ?? $case),
$case . ': wrong log mail type.'
);
expectMailDeliveryLog(array_key_exists('status', $row), $case . ': missing log status.');
expectMailDeliveryLog($row['status'] === $expectedStatus, $case . ': wrong log status.');
}
checkMailDelivery('SMTP success', 'success@example.test', true, 1, true, '验证邮件');
PHPMailer::$sendResult = false;
checkMailDelivery('SMTP returned false', 'failure@example.test', false, 0, true);
PHPMailer::$sendResult = true;
PHPMailer::$throwOnSend = true;
checkMailDelivery('SMTP exception', 'exception@example.test', false, 0, true);
PHPMailer::$throwOnSend = false;
Smarty::$throwOnFetch = true;
checkMailDelivery('Template exception', 'template@example.test', false, 0, false);
Smarty::$throwOnFetch = false;
Config::$values['mailDriver'] = 'none';
checkMailDelivery('No mail driver', 'none@example.test', false, 0, false);
Config::$values['mailDriver'] = 'smtp';
RedisClient::$blacklist = 'blocked@example.test';
checkMailDelivery('Blacklisted recipient', 'blocked@example.test', false, 0, false);
echo 'Mail delivery log checks passed.' . PHP_EOL;
}