auditionadmin/tests/Feature/app/Actions/Students/CreateStudentTest.php

83 lines
2.4 KiB
PHP

<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Students\CreateStudent;
use App\Exceptions\AuditionAdminException;
use App\Models\School;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->creator = app(CreateStudent::class);
});
it('can create a student with basic data', function () {
($this->creator)([
'first_name' => 'John',
'last_name' => 'Doe',
'grade' => 8,
'school_id' => School::factory()->create()->id,
]);
expect(Student::first())->toBeInstanceOf(Student::class);
});
it('can include optional data', function () {
($this->creator)([
'first_name' => 'John',
'last_name' => 'Doe',
'grade' => 8,
'optional_data' => ['shirt_size' => 'M'],
'school_id' => School::factory()->create()->id,
]);
$student = Student::first();
expect($student->optional_data['shirt_size'])->toEqual('M');
});
it('uses the current users school if none is specified', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
$this->actingAs($user);
($this->creator)([
'first_name' => 'John',
'last_name' => 'Doe',
'grade' => 8,
'optional_data' => ['shirt_size' => 'M'],
]);
$student = Student::first();
expect($student->school_id)->toEqual($school->id);
});
it('throws an error if we try to create a duplicate student (same name and school)', function () {
$school = School::factory()->create();
($this->creator)([
'first_name' => 'John',
'last_name' => 'Doe',
'grade' => 8,
'optional_data' => ['shirt_size' => 'M'],
'school_id' => $school->id,
]);
($this->creator)([
'first_name' => 'John',
'last_name' => 'Doe',
'grade' => 11,
'optional_data' => ['shirt_size' => 'XL'],
'school_id' => $school->id,
]);
})->throws(AuditionAdminException::class, 'Student already exists');
it('throws an error if data is missing', function () {
$school = School::factory()->create();
($this->creator)([
'last_name' => 'Doe',
'grade' => 8,
'optional_data' => ['shirt_size' => 'M'],
'school_id' => $school->id,
]);
})->throws(AuditionAdminException::class, 'Missing required data');