presseportale/tests/Feature/OperationalAlertTest.php
Kevin Adametz 6984846233 Monitoring (R5.3): Betriebs-Alerts fuer Scheduler-/Queue-Ausfaelle
OperationalAlert loggt critical + Best-Effort-Mail an MONITORING_ALERT_EMAIL
(Fallback LEGAL_CONTACT_EMAIL, feature-flagged). Alle 6 Scheduler-Tasks mit
onFailure, Queue::failing-Listener; .env.example: LOG_STACK=daily als Prod-Soll.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 15:34:40 +00:00

74 lines
2.7 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;
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();
Log::spy();
OperationalAlert::send('Geplanter Task fehlgeschlagen: billing:generate-manual-invoices', ['exit_code' => 1]);
Log::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');
});
});