I have a service class in my symfony5 project on php8, which is sending emails following certain rules. With my test want to check if the correct mails are sent. This task exists for several projects, so I really want to find a solution for this.
The method, which collects the receiver of the mails is currently private and you should not expose your privates.
One idea was to write a log entry for each potential mail receiver. But how can I check for certain values in my test.log file?
I found a package, which extends phpunit just for this purpose ( https://github.com/phoenixrvd/phpunit-assert-log-entry ), but it doesn't look like it is maintained.
Do you know a solution for either
- check the logs for messages or
- have a different method to test if the right receivers are notified?
Currently I use a Trait to add logging functionality to my service, which has a setter method, which sets the Logger via the @required
annotation.
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);
}
}