Skip to content

Commit

Permalink
#465 - feat: added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Jul 17, 2024
1 parent 20dfb29 commit f958997
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Toby\Models\User;
use Toby\Notifications\UpcomingOhsTrainingForEmployeeNotification;

class SendNotificationAboutUpcomingOhsTrainingForEmployees extends Command
class SendNotificationAboutUpcomingOhsTrainingsForEmployees extends Command
{
protected $signature = "toby:send-notification-about-ohs-training-for-employees";
protected $signature = "toby:send-notification-about-ohs-trainings-for-employees";
protected $description = "Send notifications about upcoming ohs trainings.";

public function handle(): void
Expand Down
4 changes: 2 additions & 2 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Toby\Console\Commands\SendNotificationAboutUpcomingAndOverdueMedicalExams;
use Toby\Console\Commands\SendNotificationAboutUpcomingAndOverdueOhsTraining;
use Toby\Console\Commands\SendNotificationAboutUpcomingMedicalExamsForEmployees;
use Toby\Console\Commands\SendNotificationAboutUpcomingOhsTrainingForEmployees;
use Toby\Console\Commands\SendNotificationAboutUpcomingOhsTrainingsForEmployees;
use Toby\Console\Commands\SendOvertimeRequestSummariesToApprovers;
use Toby\Console\Commands\SendVacationRequestSummariesToApprovers;
use Toby\Jobs\CheckYearPeriod;
Expand Down Expand Up @@ -55,7 +55,7 @@
->weeklyOn(1, "08:30")
->onOneServer();

Schedule::command(SendNotificationAboutUpcomingOhsTrainingForEmployees::class)
Schedule::command(SendNotificationAboutUpcomingOhsTrainingsForEmployees::class)
->weeklyOn(1, "08:30")
->onOneServer();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Console\Commands\SendNotificationAboutUpcomingMedicalExamsForEmployees;
use Toby\Models\User;
use Toby\Notifications\UpcomingMedicalExamForEmployeeNotification;

class SendNotificationAboutUpcomingMedicalExamsToSlackTest extends TestCase
{
use RefreshDatabase;
use InteractsWithYearPeriods;

protected User $user;

protected function setUp(): void
{
parent::setUp();

Notification::fake();
$this->createCurrentYearPeriod();
$this->user = User::factory()->employee()->create();
}

public function testNotificationIsSentToUserWithUpcomingMedicalExams(): void
{
$this->user->histories()->create([
"from" => Carbon::createFromDate(2022, 1, 1),
"to" => Carbon::now()->addDays(14),
"type" => "medical_exam",
]);

$this->artisan(SendNotificationAboutUpcomingMedicalExamsForEmployees::class)
->execute();

Notification::assertSentTo($this->user, UpcomingMedicalExamForEmployeeNotification::class);
}

public function testNotificationIsNotSentToUserWithoutUpcomingMedicalExams(): void
{
$this->user->histories()->create([
"from" => Carbon::createFromDate(2022, 1, 1),
"to" => Carbon::now()->addYear(),
"type" => "medical_exam",
]);

$this->artisan(SendNotificationAboutUpcomingMedicalExamsForEmployees::class)
->execute();

Notification::assertNothingSent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Console\Commands\SendNotificationAboutUpcomingOhsTrainingsForEmployees;
use Toby\Models\User;
use Toby\Notifications\UpcomingOhsTrainingForEmployeeNotification;

class SendNotificationAboutUpcomingOhsTrainingsToSlackTest extends TestCase
{
use RefreshDatabase;
use InteractsWithYearPeriods;

protected User $user;

protected function setUp(): void
{
parent::setUp();

Notification::fake();
$this->createCurrentYearPeriod();
Http::fake(fn(): array => [
"channel" => Str::random(8),
"message" => ["ts" => Carbon::now()->toDateTimeString()],
]);

$this->user = User::factory()->employee()->create();
}

public function testNotificationIsSentToUserWithUpcomingMedicalExams(): void
{
$this->user->histories()->create([
"from" => Carbon::createFromDate(2022, 1, 1),
"to" => Carbon::now()->addDays(14),
"type" => "ohs_training",
]);

$this->artisan(SendNotificationAboutUpcomingOhsTrainingsForEmployees::class)
->execute();

Notification::assertSentTo($this->user, UpcomingOhsTrainingForEmployeeNotification::class);
}

public function testNotificationIsNotSentToUserWithoutUpcomingMedicalExams(): void
{
$this->user->histories()->create([
"from" => Carbon::createFromDate(2022, 1, 1),
"to" => Carbon::now()->addYear(),
"type" => "ohs_training",
]);

$this->artisan(SendNotificationAboutUpcomingOhsTrainingsForEmployees::class)
->execute();

Notification::assertNothingSent();
}
}

0 comments on commit f958997

Please sign in to comment.