Create school test and action created
This commit is contained in:
parent
b4bf94d9f8
commit
bd207f8e4a
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
||||
});
|
||||
Loading…
Reference in New Issue