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

42 lines
1.6 KiB
PHP

<?php
use App\Actions\Schools\AddSchoolEmailDomain;
use App\Exceptions\AuditionAdminException;
use App\Models\AuditLogEntry;
use App\Models\School;
use App\Models\SchoolEmailDomain;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
actAsAdmin();
$this->secretary = app(AddSchoolEmailDomain::class);
$this->school = School::factory()->create();
$this->domain = fake()->domainName();
});
it('adds a domain to a school', function () {
($this->secretary)($this->school, $this->domain);
expect(SchoolEmailDomain::where('school_id', $this->school->id)
->where('domain', $this->domain)->exists())->toBeTrue();
});
it('logs the addition of the domain', function () {
($this->secretary)($this->school, $this->domain);
$logEntry = AuditLogEntry::orderBy('id', 'desc')->first();
expect($logEntry->message)->toEqual('Added the email domain '.$this->domain.' to school '.$this->school->name);
});
it('throws an exception if the school does not exist', function () {
$newSchool = School::factory()->make();
$this->secretary->addDomain($newSchool, $this->domain);
})->throws(AuditionAdminException::class, 'School does not exist');
it('silently continues if the domain is already added to the school', function () {
SchoolEmailDomain::create(['school_id' => $this->school->id, 'domain' => $this->domain]);
($this->secretary)($this->school, $this->domain);
expect(SchoolEmailDomain::where('school_id', $this->school->id)
->where('domain', $this->domain)->exists())->toBeTrue();
});