test: improve coverage
Some checks are pending
tests / ci (push) Waiting to run

This commit is contained in:
Benjamin Nussbaum 2025-09-24 00:45:50 +02:00
parent e9fc6b2335
commit 5d3a512203
20 changed files with 1651 additions and 101 deletions

View file

@ -20,7 +20,7 @@ test('battery low notification creates correct mail message', function () {
'name' => 'Test Device',
'last_battery_voltage' => 3.0,
]);
$notification = new BatteryLow($device);
$mailMessage = $notification->toMail(new User());
@ -34,12 +34,12 @@ test('battery low notification creates correct webhook message', function () {
'services.webhook.notifications.topic' => 'battery.low',
'app.name' => 'Test App',
]);
$device = Device::factory()->create([
'name' => 'Test Device',
'last_battery_voltage' => 3.0,
]);
$notification = new BatteryLow($device);
$webhookMessage = $notification->toWebhook(new User());
@ -65,7 +65,7 @@ test('battery low notification creates correct array representation', function (
'name' => 'Test Device',
'last_battery_voltage' => 3.0,
]);
$notification = new BatteryLow($device);
$array = $notification->toArray(new User());
@ -74,4 +74,3 @@ test('battery low notification creates correct array representation', function (
'battery_percent' => $device->battery_percent,
]);
});

View file

@ -2,11 +2,10 @@
declare(strict_types=1);
use App\Notifications\BatteryLow;
use App\Notifications\Channels\WebhookChannel;
use App\Notifications\Messages\WebhookMessage;
use App\Models\Device;
use App\Models\User;
use App\Notifications\BatteryLow;
use App\Notifications\Channels\WebhookChannel;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
@ -15,59 +14,63 @@ use Illuminate\Notifications\Notification;
test('webhook channel returns null when no webhook url is configured', function () {
$client = Mockery::mock(Client::class);
$channel = new WebhookChannel($client);
$user = new class extends User {
$user = new class extends User
{
public function routeNotificationFor($driver, $notification = null)
{
return null; // No webhook URL configured
}
};
$notification = new BatteryLow(Device::factory()->create());
$result = $channel->send($user, $notification);
expect($result)->toBeNull();
});
test('webhook channel throws exception when notification does not implement toWebhook', function () {
$client = Mockery::mock(Client::class);
$channel = new WebhookChannel($client);
$user = new class extends User {
$user = new class extends User
{
public function routeNotificationFor($driver, $notification = null)
{
return 'https://example.com/webhook';
}
};
$notification = new class extends Notification {
$notification = new class extends Notification
{
public function via($notifiable)
{
return [];
}
};
expect(fn() => $channel->send($user, $notification))
->toThrow(\Exception::class, 'Notification does not implement toWebhook method.');
expect(fn () => $channel->send($user, $notification))
->toThrow(Exception::class, 'Notification does not implement toWebhook method.');
});
test('webhook channel sends successful webhook request', function () {
$client = Mockery::mock(Client::class);
$channel = new WebhookChannel($client);
$user = new class extends User {
$user = new class extends User
{
public function routeNotificationFor($driver, $notification = null)
{
return 'https://example.com/webhook';
}
};
$device = Device::factory()->create();
$notification = new BatteryLow($device);
$expectedResponse = new Response(200, [], 'OK');
$client->shouldReceive('post')
->once()
->with('https://example.com/webhook', [
@ -77,55 +80,56 @@ test('webhook channel sends successful webhook request', function () {
'headers' => $notification->toWebhook($user)->toArray()['headers'],
])
->andReturn($expectedResponse);
$result = $channel->send($user, $notification);
expect($result)->toBe($expectedResponse);
});
test('webhook channel throws exception when response status is not successful', function () {
$client = Mockery::mock(Client::class);
$channel = new WebhookChannel($client);
$user = new class extends User {
$user = new class extends User
{
public function routeNotificationFor($driver, $notification = null)
{
return 'https://example.com/webhook';
}
};
$device = Device::factory()->create();
$notification = new BatteryLow($device);
$errorResponse = new Response(400, [], 'Bad Request');
$client->shouldReceive('post')
->once()
->andReturn($errorResponse);
expect(fn() => $channel->send($user, $notification))
->toThrow(\Exception::class, 'Webhook request failed with status code: 400');
expect(fn () => $channel->send($user, $notification))
->toThrow(Exception::class, 'Webhook request failed with status code: 400');
});
test('webhook channel handles guzzle exceptions', function () {
$client = Mockery::mock(Client::class);
$channel = new WebhookChannel($client);
$user = new class extends User {
$user = new class extends User
{
public function routeNotificationFor($driver, $notification = null)
{
return 'https://example.com/webhook';
}
};
$device = Device::factory()->create();
$notification = new BatteryLow($device);
$client->shouldReceive('post')
->once()
->andThrow(new class extends \Exception implements GuzzleException {});
expect(fn() => $channel->send($user, $notification))
->toThrow(\Exception::class);
->andThrow(new class extends Exception implements GuzzleException {});
expect(fn () => $channel->send($user, $notification))
->toThrow(Exception::class);
});

View file

@ -71,7 +71,7 @@ test('webhook message can chain methods', function () {
->verify(true);
$array = $message->toArray();
expect($array['query'])->toBe(['param' => 'value']);
expect($array['data'])->toBe(['updated' => 'data']);
expect($array['headers']['X-Test'])->toBe('header');
@ -83,7 +83,7 @@ test('webhook message toArray returns correct structure', function () {
$message = WebhookMessage::create(['test' => 'data']);
$array = $message->toArray();
expect($array)->toHaveKeys(['query', 'data', 'headers', 'verify']);
expect($array['query'])->toBeNull();
expect($array['data'])->toBe(['test' => 'data']);