auditionadmin/tests/Feature/app/Actions/Schools/AssignUserToSchoolTest.php

59 lines
2.1 KiB
PHP

<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Schools\AssignUserToSchool;
use App\Exceptions\AuditionAdminException;
use App\Models\AuditLogEntry;
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->assigner = app(AssignUserToSchool::class);
$this->user = User::factory()->create();
$this->school = School::factory()->create();
});
it('throws an exception if the user does not exist', function () {
$newUser = User::factory()->make();
$this->assigner->assign($newUser, $this->school);
})->throws(AuditionAdminException::class, 'User does not exist');
it('throws an exception if the school does not exist', function () {
$newSchool = School::factory()->make();
$this->assigner->assign($this->user, $newSchool);
})->throws(AuditionAdminException::class, 'School does not exist');
it('assigns a user to a school', function () {
($this->assigner)($this->user, $this->school);
expect($this->user->school_id)->toEqual($this->school->id);
});
it('logs the assignment of the user to the school', function () {
($this->assigner)($this->user, $this->school);
$logEntry = AuditLogEntry::orderBy('id', 'desc')->first();
expect($logEntry->message)->toEqual('Added '.$this->user->full_name().' to '.$this->school->name);
});
it('changes a users school assignment', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$newSchool = School::factory()->create();
($this->assigner)($this->user, $newSchool);
$this->user->refresh();
expect($this->user->school_id)->toEqual($newSchool->id);
});
it('logs a change in school assignment', function () {
$this->user->school_id = $this->school->id;
$this->user->save();
$newSchool = School::factory()->create();
($this->assigner)($this->user, $newSchool);
$this->user->refresh();
$logEntry = AuditLogEntry::orderBy('id', 'desc')->first();
expect($logEntry->message)->toEqual('Changed school for '.$this->user->full_name().' from '.$this->school->name.' to '.$newSchool->name);
});