【发布时间】:2022-01-17 23:08:13
【问题描述】:
我在 php8 上的 symfony5 项目中有一个服务类,它按照某些规则发送电子邮件。通过我的测试,想检查是否发送了正确的邮件。这个任务存在于几个项目中,所以我真的很想为此找到一个解决方案。
收集邮件接收者的方法目前是私有的,你不应该暴露你的私有。
一个想法是为每个潜在的邮件接收者编写一个日志条目。但是如何检查我的 test.log 文件中的某些值?
我找到了一个包,它扩展了 phpunit 只是为了这个目的 (https://github.com/phoenixrvd/phpunit-assert-log-entry),但它看起来并没有被维护。
你知道两者的解决方案吗
- 检查日志中的消息 或
- 有不同的方法来测试是否通知了正确的接收者?
目前我使用 Trait 向我的服务添加日志记录功能,该服务有一个 setter 方法,该方法通过 @required 注释设置 Logger。
namespace App\Logger;
use Psr\Log\LoggerInterface;
trait LoggerTrait
{
private $logger;
/**
* @required
*/
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
protected function logInfo(string $message, array $context = [])
{
if (null !== $this->logger) {
$this->logger->info($message, $context);
}
}
【问题讨论】:
标签: php phpunit symfony5 monolog