74 lines
1.8 KiB
PHP
74 lines
1.8 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)(
|
|
'John',
|
|
'Doe',
|
|
8,
|
|
[],
|
|
School::factory()->create()->id
|
|
);
|
|
expect(Student::first())->toBeInstanceOf(Student::class);
|
|
});
|
|
|
|
it('can include optional data', function () {
|
|
($this->creator)(
|
|
'John',
|
|
'Doe',
|
|
8,
|
|
['shirt_size' => 'M'],
|
|
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)(
|
|
'John',
|
|
'Doe',
|
|
8,
|
|
['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)(
|
|
'John',
|
|
'Doe',
|
|
8,
|
|
['shirt_size' => 'M'],
|
|
$school->id
|
|
);
|
|
($this->creator)(
|
|
'John',
|
|
'Doe',
|
|
11,
|
|
['shirt_size' => 'XL'],
|
|
$school->id
|
|
);
|
|
})->throws(AuditionAdminException::class, 'Student already exists');
|