SetHeadDirector testing

This commit is contained in:
Matt Young 2025-07-01 20:25:43 -05:00
parent d7134a948b
commit d994e906e1
2 changed files with 67 additions and 3 deletions

View File

@ -3,7 +3,6 @@
namespace App\Actions\Schools; namespace App\Actions\Schools;
use App\Exceptions\AuditionAdminException; use App\Exceptions\AuditionAdminException;
use App\Models\School;
use App\Models\User; use App\Models\User;
use function auditionLog; use function auditionLog;
@ -15,9 +14,9 @@ class SetHeadDirector
{ {
} }
public function __invoke(User $user, School $school): void public function __invoke(User $user): void
{ {
$this->setHeadDirector($user, $school); $this->setHeadDirector($user);
} }
/** /**
@ -25,6 +24,10 @@ class SetHeadDirector
*/ */
public function setHeadDirector(User $user): void public function setHeadDirector(User $user): void
{ {
if (! User::where('id', $user->id)->exists()) {
throw new AuditionAdminException('User does not exist');
}
if (is_null($user->school_id)) { if (is_null($user->school_id)) {
throw new AuditionAdminException('User is not associated with a school'); throw new AuditionAdminException('User is not associated with a school');
} }

View File

@ -0,0 +1,61 @@
<?php
use App\Actions\Schools\SetHeadDirector;
use App\Exceptions\AuditionAdminException;
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->school = School::factory()->create();
$this->user = User::factory()->create();
});
it('makes a user the head director at a school', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$promoter = app(SetHeadDirector::class);
$promoter($this->user);
expect($this->user->hasFlag('head_director'))->toBeTrue();
});
it('throws an exception if the user does not exist', function () {
$newUser = User::factory()->make();
$promoter = app(SetHeadDirector::class);
$promoter($newUser);
})->throws(AuditionAdminException::class, 'User does not exist');
it('throws an exception if the user is not associated with a school', function () {
$promoter = app(SetHeadDirector::class);
$promoter($this->user);
})->throws(AuditionAdminException::class, 'User is not associated with a school');
it('removes any other head directors at their school', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$otherUser = User::factory()->create();
$otherUser->school_id = $this->school->id;
$otherUser->save();
$promoter = app(SetHeadDirector::class);
$promoter($this->user);
$this->user->refresh();
$otherUser->refresh();
expect($otherUser->hasFlag('head_director'))->toBeFalse()
->and($this->user->hasFlag('head_director'))->toBeTrue();
$promoter($otherUser);
$this->user->refresh();
$otherUser->refresh();
expect($otherUser->hasFlag('head_director'))->toBeTrue()
->and($this->user->hasFlag('head_director'))->toBeFalse();
});
it('logs the head director promotion', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$promoter = app(SetHeadDirector::class);
$promoter($this->user);
$logEntry = \App\Models\AuditLogEntry::latest()->first();
expect($logEntry->message)->toEqual('Set '.$this->user->full_name().' as head director at '.$this->school->name);
});