presseportale/tests/Feature/OperationalAlertTest.php
Kevin Adametz 85d7c2b0d5 betrieb: Logs je Thema in Tagesdateien (billing, ki, monitoring, market); Doku: Duplicate-Content-Decision durch Anhang 2 abgelöst
- config/logging.php: Themen-Channels billing, ki, monitoring, market als
  daily-Dateien mit gemeinsamer Aufbewahrung (LOG_DAILY_DAYS), Stack-Default
  auf daily; Channel `testing` für die Suite.
- Fachliche Log-Aufrufe in Billing, KI-Jobs, OperationalAlert und
  Kursdaten auf Log::channel() umgestellt.
- phpunit.xml + TestCase: Suite schreibt ausschließlich nach
  testing-YYYY-MM-DD.log; LogChannelsTest sichert die Konfiguration.
- docs/weiteres: Decision-Update Duplicate-Content entfernt (überholt durch
  Roadmap-Anhang 2), Decision-Update Verlinkung & Backlinks als Referenz
  wieder abgelegt, README-Index nachgezogen.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 13:35:42 +00:00

76 lines
2.8 KiB
PHP

<?php
use App\Mail\OperationalAlertMail;
use App\Support\OperationalAlert;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Psr\Log\LoggerInterface;
test('an alert always logs critical and mails the configured recipient', function () {
config()->set('monitoring.alerts.enabled', true);
config()->set('monitoring.alerts.mail_to', 'ops@example.com');
Mail::fake();
$monitoringLog = Mockery::spy(LoggerInterface::class);
Log::shouldReceive('channel')->with('monitoring')->andReturn($monitoringLog);
OperationalAlert::send('Geplanter Task fehlgeschlagen: billing:generate-manual-invoices', ['exit_code' => 1]);
$monitoringLog->shouldHaveReceived('critical')->once();
Mail::assertSent(OperationalAlertMail::class, function (OperationalAlertMail $mail) {
return $mail->hasTo('ops@example.com')
&& str_contains($mail->summary, 'billing:generate-manual-invoices')
&& $mail->context === ['exit_code' => 1];
});
});
test('no mail is sent when alerts are disabled', function () {
config()->set('monitoring.alerts.enabled', false);
config()->set('monitoring.alerts.mail_to', 'ops@example.com');
Mail::fake();
OperationalAlert::send('Etwas ist kaputt');
Mail::assertNothingSent();
});
test('no mail is sent when no recipient is configured', function () {
config()->set('monitoring.alerts.enabled', true);
config()->set('monitoring.alerts.mail_to', null);
Mail::fake();
OperationalAlert::send('Etwas ist kaputt');
Mail::assertNothingSent();
});
test('a mail failure never bubbles up to the caller', function () {
config()->set('monitoring.alerts.enabled', true);
config()->set('monitoring.alerts.mail_to', 'ops@example.com');
// Mailserver down: der Versand wirft — der Alert muss das schlucken,
// damit der auslösende Scheduler-/Queue-Prozess weiterläuft.
Mail::shouldReceive('to')->andReturnSelf();
Mail::shouldReceive('send')->andThrow(new RuntimeException('mail down'));
OperationalAlert::send('Etwas ist kaputt');
expect(true)->toBeTrue();
});
test('a failing queue job triggers an operational alert via the registered listener', function () {
config()->set('monitoring.alerts.enabled', true);
config()->set('monitoring.alerts.mail_to', 'ops@example.com');
Mail::fake();
$job = Mockery::mock(Job::class);
$job->shouldReceive('resolveName')->andReturn('App\\Jobs\\ClassifyPressRelease');
$job->shouldReceive('getQueue')->andReturn('classification');
event(new JobFailed('database', $job, new RuntimeException('job boom')));
Mail::assertSent(OperationalAlertMail::class, function (OperationalAlertMail $mail) {
return str_contains($mail->summary, 'ClassifyPressRelease');
});
});