test: improve coverage

This commit is contained in:
Benjamin Nussbaum 2025-09-23 23:56:11 +02:00
parent 4f251bf37e
commit e9fc6b2335
7 changed files with 630 additions and 0 deletions

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
use Database\Seeders\ExampleRecipesSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('example recipes seeder command calls seeder with correct user id', function () {
$seeder = Mockery::mock(ExampleRecipesSeeder::class);
$seeder->shouldReceive('run')
->once()
->with('123');
$this->app->instance(ExampleRecipesSeeder::class, $seeder);
$this->artisan('recipes:seed', ['user_id' => '123'])
->assertExitCode(0);
});
test('example recipes seeder command has correct signature', function () {
$command = $this->app->make(\App\Console\Commands\ExampleRecipesSeederCommand::class);
expect($command->getName())->toBe('recipes:seed');
expect($command->getDescription())->toBe('Seed example recipes');
});
test('example recipes seeder command prompts for missing input', function () {
$seeder = Mockery::mock(ExampleRecipesSeeder::class);
$seeder->shouldReceive('run')
->once()
->with('456');
$this->app->instance(ExampleRecipesSeeder::class, $seeder);
$this->artisan('recipes:seed')
->expectsQuestion('What is the user_id?', '456')
->assertExitCode(0);
});

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
use App\Jobs\FirmwarePollJob;
use App\Models\Firmware;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('firmware check command has correct signature', function () {
$command = $this->app->make(\App\Console\Commands\FirmwareCheckCommand::class);
expect($command->getName())->toBe('trmnl:firmware:check');
expect($command->getDescription())->toBe('Checks for the latest firmware and downloads it if flag --download is passed.');
});
test('firmware check command runs without errors', function () {
$this->artisan('trmnl:firmware:check')
->assertExitCode(0);
});
test('firmware check command runs with download flag', function () {
$this->artisan('trmnl:firmware:check', ['--download' => true])
->assertExitCode(0);
});
test('firmware check command can run successfully', function () {
$this->artisan('trmnl:firmware:check')
->assertExitCode(0);
});

View file

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
use App\Jobs\NotifyDeviceBatteryLowJob;
use App\Models\Device;
use App\Models\User;
use App\Notifications\BatteryLow;
use Illuminate\Support\Facades\Notification;
test('it sends battery low notification when battery is below threshold', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'last_battery_voltage' => 3.0, // This should result in low battery percentage
'battery_notification_sent' => false,
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertSentTo($user, BatteryLow::class);
$device->refresh();
expect($device->battery_notification_sent)->toBeTrue();
});
test('it does not send notification when battery is above threshold', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'last_battery_voltage' => 4.0, // This should result in high battery percentage
'battery_notification_sent' => false,
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertNotSentTo($user, BatteryLow::class);
$device->refresh();
expect($device->battery_notification_sent)->toBeFalse();
});
test('it does not send notification when already sent', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'last_battery_voltage' => 3.0, // Low battery
'battery_notification_sent' => true, // Already sent
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertNotSentTo($user, BatteryLow::class);
});
test('it resets notification flag when battery is above threshold', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'last_battery_voltage' => 4.0, // High battery
'battery_notification_sent' => true, // Was previously sent
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertNotSentTo($user, BatteryLow::class);
$device->refresh();
expect($device->battery_notification_sent)->toBeFalse();
});
test('it skips devices without associated user', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$device = Device::factory()->create([
'user_id' => null,
'last_battery_voltage' => 3.0, // Low battery
'battery_notification_sent' => false,
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertNothingSent();
});
test('it processes multiple devices correctly', function () {
Notification::fake();
config(['app.notifications.battery_low.warn_at_percent' => 20]);
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$device1 = Device::factory()->create([
'user_id' => $user1->id,
'last_battery_voltage' => 3.0, // Low battery
'battery_notification_sent' => false,
]);
$device2 = Device::factory()->create([
'user_id' => $user2->id,
'last_battery_voltage' => 4.0, // High battery
'battery_notification_sent' => false,
]);
$job = new NotifyDeviceBatteryLowJob();
$job->handle();
Notification::assertSentTo($user1, BatteryLow::class);
Notification::assertNotSentTo($user2, BatteryLow::class);
$device1->refresh();
$device2->refresh();
expect($device1->battery_notification_sent)->toBeTrue();
expect($device2->battery_notification_sent)->toBeFalse();
});