auditionadmin/tests/Feature/app/Http/Controllers/EntryContrllerTest.php

127 lines
4.8 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\Entry;
use App\Models\School;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
uses(RefreshDatabase::class);
describe('EntryController::index', function () {
it('denies access if the user does not have a school', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('entries.index'));
$response->assertForbidden();
});
it('provides an index of the entries for the users school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
Student::factory()->count(3)
->forSchool($school)
->has(Entry::factory()->count(2))
->create();
expect(Entry::count())->toEqual(6);
$response = $this->actingAs($user)->get(route('entries.index'));
$response->assertOk()
->assertViewIs('entries.index')
->assertViewHas('entries');
$this->assertCount(6, $response->viewData('entries'));
foreach (Entry::all() as $entry) {
$response->assertSee($entry->student->full_name());
}
});
it('provides a form for creating new entries', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
Student::factory()->count(3)
->forSchool($school)
->has(Entry::factory()->count(2))
->create();
Audition::each(function (Audition $audition) {
$audition->update(['entry_deadline' => now()->subDays(10)]);
});
$openAuditions = Audition::factory()->count(12)->create(['entry_deadline' => now()->addDays(10)]);
$response = $this->actingAs($user)->get(route('entries.index'));
$response->assertOk()
->assertViewHas('students')
->assertViewHas('auditions')
->assertSee(route('entries.store'));
$this->assertCount(12, $response->viewData('auditions'));
$this->assertCount(3, $response->viewData('students'));
foreach ($openAuditions as $audition) {
$response->assertSee($audition->name);
}
});
});
describe('EntryController::store', function () {
it('denies access if the user does not have a school', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('entries.store'));
$response->assertForbidden();
});
it('creates a new entry for the users school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
$student = Student::factory()->forSchool($school)->create(['grade' => 10]);
$audition = Audition::factory()->create([
'minimum_grade' => 9,
'maximum_grade' => 12,
'entry_deadline' => now()->addDays(10),
]);
$response = actingAs($user)->post(route('entries.store'), [
'student_id' => $student->id,
'audition_id' => $audition->id,
'for_advancement' => 'on',
'for_seating' => 'on',
]);
$response->assertRedirect(route('entries.index'))
->assertSessionHas('success');
$this->assertCount(1, Entry::all());
$entry = Entry::first();
expect($entry->student_id)->toBe($student->id)
->and($entry->audition_id)->toBe($audition->id)
->and($entry->for_advancement)->toBeTruthy()
->and($entry->for_seating)->toBeTruthy();
});
});
describe('EntryController::destroy', function () {
it('denies access if the user is not a director at the entries school', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$user->update(['school_id' => $school->id]);
$user->refresh();
$entry = Entry::factory()->create();
$response = $this->actingAs($user)->delete(route('entries.destroy', $entry->id));
$response->assertForbidden();
});
it('deletes an entry', function () {
$user = User::factory()->create();
$school = School::factory()->create();
$student = Student::factory()->forSchool($school)->create();
$entry = Entry::factory()->forStudent($student)->create();
$user->school_id = $school->id;
$user->save();
expect(Entry::count())->toEqual(1);
$response = $this->actingAs($user)->delete(route('entries.destroy', $entry->id));
$response->assertRedirect(route('entries.index'))
->assertSessionHas('success');
expect(Entry::count())->toEqual(0);
});
});