99 lines
3.0 KiB
PHP
99 lines
3.0 KiB
PHP
<?php
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
use App\Actions\Students\CreateStudent;
|
|
use App\Actions\Students\UpdateStudent;
|
|
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);
|
|
$this->editor = app(UpdateStudent::class);
|
|
$this->editedStudent = ($this->creator)([
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'grade' => 8,
|
|
'school_id' => School::factory()->create()->id,
|
|
]);
|
|
});
|
|
|
|
it('can update a student with basic data', function () {
|
|
($this->editor)($this->editedStudent, [
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'grade' => 14,
|
|
'school_id' => School::factory()->create()->id,
|
|
]);
|
|
expect(Student::first()->grade)->toBe(14);
|
|
});
|
|
|
|
it('can include optional data', function () {
|
|
($this->editor)($this->editedStudent, [
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'grade' => 2,
|
|
'optional_data' => ['shirt_size' => 'M'],
|
|
'school_id' => School::factory()->create()->id,
|
|
]);
|
|
$student = Student::first();
|
|
expect($student->optional_data['shirt_size'])->toEqual('M')
|
|
->and($student->grade)->toBe(2);
|
|
});
|
|
|
|
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->editor)($this->editedStudent, [
|
|
'first_name' => 'Jane',
|
|
'last_name' => 'Doe',
|
|
'grade' => 8,
|
|
'optional_data' => ['shirt_size' => 'M'],
|
|
]);
|
|
$student = Student::first();
|
|
expect($student->school_id)->toEqual($school->id)
|
|
->and($student->first_name)->toEqual('Jane');
|
|
});
|
|
|
|
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,
|
|
]);
|
|
$student2 = ($this->creator)([
|
|
'first_name' => 'Jane',
|
|
'last_name' => 'Doe',
|
|
'grade' => 11,
|
|
'optional_data' => ['shirt_size' => 'XL'],
|
|
'school_id' => $school->id,
|
|
]);
|
|
($this->editor)($student2, [
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'grade' => 11,
|
|
'school_id' => $school->id,
|
|
]);
|
|
})->throws(AuditionAdminException::class, 'Student already exists');
|
|
|
|
it('throws an error if data is missing', function () {
|
|
$school = School::factory()->create();
|
|
($this->editor)($this->editedStudent, [
|
|
'last_name' => 'Doe',
|
|
'grade' => 8,
|
|
'optional_data' => ['shirt_size' => 'M'],
|
|
'school_id' => $school->id,
|
|
]);
|
|
})->throws(AuditionAdminException::class, 'Missing required data');
|