auditionadmin/tests-old/Feature/Pages/Setup/RoomsIndexTest.php

126 lines
4.1 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\Entry;
use App\Models\Room;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\get;
uses(RefreshDatabase::class);
function roomTestSetup(): void
{
$rooms = Room::factory()->count(3)->create();
foreach ($rooms as $room) {
Audition::factory()->count(3)->create(['room_id' => $room->id]);
}
$auditions = Audition::all();
foreach ($auditions as $audition) {
$n = fake()->numberBetween(1, 4);
Entry::factory()->count($n)->create(['audition_id' => $audition->id]);
}
}
it('only allows an admin to manage rooms', function () {
get(route('admin.rooms.index'))
->assertRedirect(route('home'));
actAsNormal();
get(route('admin.rooms.index'))
->assertRedirect(route('dashboard'))
->assertSessionHas('error', 'You are not authorized to perform this action');
actAsAdmin();
get(route('admin.rooms.index'))
->assertOk();
});
it('has a link to create a new room', function () {
actAsAdmin();
get(route('admin.rooms.index'))
->assertSee('New Room')
->assertSee(route('admin.rooms.create'));
});
it('has a card for each room, including the total number of entries in that room', function () {
// Arrange
roomTestSetup();
// Act & Assert
actAsAdmin();
$response = get(route('admin.rooms.index'));
$response->assertOk();
foreach (Room::all() as $room) {
$roomEntryCount = $room->entries()->count();
$response->assertSeeInOrder(['<h3', e($room->name), '</h3>', $roomEntryCount], false);
}
});
it('has a form for editing each room', function () {
// Arrange
roomTestSetup();
// Act & Assert
actAsAdmin();
$response = get(route('admin.rooms.index'));
$response->assertOk();
foreach (Room::all() as $room) {
if ($room->id === 0) {
continue;
}
$response->assertSeeInOrder([
route('admin.rooms.update', $room),
'<input',
'name=', 'name',
'<input',
'name=',
'description',
], false);
}
});
it('can edit a room', function () {
$room = Room::factory()->create();
$formData = [
'name' => 'New Room Name',
'description' => 'New Room Description',
];
actAsAdmin();
$response = $this->patch(route('admin.rooms.update', $room), $formData);
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertRedirect(route('admin.rooms.index'))
->assertSessionHas('success', 'Room updated successfully')
->assertSessionHasNoErrors();
$this->assertDatabaseHas('rooms', $formData);
});
it('can create a room', function () {
$formData = [
'name' => 'New Room Name',
'description' => 'New Room Description',
];
actAsAdmin();
$response = $this->post(route('admin.rooms.store'), $formData);
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertRedirect(route('admin.rooms.index'))
->assertSessionHas('success', 'Room created.')
->assertSessionHasNoErrors();
$this->assertDatabaseHas('rooms', $formData);
});
it('can delete a room', function () {
// Arrange
$room = Room::factory()->create();
// Act & Assert
actAsAdmin();
$response = $this->delete(route('admin.rooms.destroy', $room));
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertRedirect(route('admin.rooms.index'))
->assertSessionHas('success', 'Room deleted.')
->assertSessionHasNoErrors();
$this->assertDatabaseMissing('rooms', ['id' => $room->id]);
});
it('will not delete a room with auditions', function () {
$room = Room::factory()->create();
Audition::factory()->create(['room_id' => $room->id]);
actAsAdmin();
$response = $this->delete(route('admin.rooms.destroy', $room));
/** @noinspection PhpUnhandledExceptionInspection */
$response->assertRedirect(route('admin.rooms.index'))
->assertSessionHas('error',
'Cannot delete room with auditions. First move the auditions to unassigned or another room')
->assertSessionHasNoErrors();
});