Create school test and action created

This commit is contained in:
Matt Young 2025-07-01 11:30:31 -05:00
parent b4bf94d9f8
commit bd207f8e4a
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace App\Actions\Schools;
use App\Models\School;
class CreateSchool
{
public function __invoke(
string $name,
?string $address = null,
?string $city = null,
?string $state = null,
?string $zip = null
): School {
return $this->create($name, $address, $city, $state, $zip);
}
public function create(
string $name,
?string $address = null,
?string $city = null,
?string $state = null,
?string $zip = null
): School {
$newSchool = School::create([
'name' => $name,
'address' => $address,
'city' => $city,
'state' => $state,
'zip' => $zip,
]);
if (auth()->user()) {
$message = 'Created school '.$newSchool->name;
$affects = ['schools' => [$newSchool->id]];
auditionLog($message, $affects);
}
return $newSchool;
}
}

View File

@ -0,0 +1,49 @@
<?php
use App\Actions\Schools\CreateSchool;
use App\Models\School;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->creator = app(CreateSchool::class);
});
it('creates a school', function () {
$newSchool = ($this->creator)(
'Longfellow Intermediate',
'210 East 4th Stret',
'Chelsea',
'OK',
'74016',
);
$dbSchool = School::first();
expect(School::where('name', 'Longfellow Intermediate')->exists())->toBeTrue()
->and($dbSchool->name)->toEqual('Longfellow Intermediate')
->and($dbSchool->address)->toEqual('210 East 4th Stret')
->and($dbSchool->city)->toEqual('Chelsea')
->and($dbSchool->state)->toEqual('OK')
->and($dbSchool->zip)->toEqual('74016');
});
it('logs school creation', function () {
actAsAdmin();
$schoolAddress = fake()->streetAddress();
$schoolCity = fake()->city();
$schoolState = 'OK';
$schoolZip = fake()->postcode();
$schoolName = $schoolCity.' High School';
$this->creator->create(
$schoolName,
$schoolAddress,
$schoolCity,
$schoolState,
$schoolZip,
);
$logEntry = \App\Models\AuditLogEntry::first();
expect($logEntry->message)->toEqual('Created school '.$schoolName)
->and($logEntry->affected['schools'])->toEqual([1])
->and($logEntry->user)->toEqual(auth()->user()->email);
});