auditionadmin/tests/Feature/Pages/Admin/SchoolsCreateTest.php

102 lines
3.0 KiB
PHP

<?php
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->nonAdminUser = User::factory()->create();
$this->tabUser = User::factory()->tab()->create();
$this->school = School::factory()->create();
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.schools.create';
get(route($checkRoute))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute))->assertOk();
actingAs($this->nonAdminUser);
get(route($checkRoute))->assertRedirect(route('dashboard'));
});
it('submits a post request', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.schools.create'));
$response->assertOk();
$response->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.schools.store'),
'/form',
]);
});
it('has all needed fields', function () {
// Arrange
actingAs($this->adminUser);
$fieldNames = [
'name',
'address',
'city',
'state',
'zip',
];
// Act & Assert
$response = get(route('admin.schools.create'));
$response->assertOk();
foreach ($fieldNames as $fieldName) {
$response->assertSeeInOrder([
'input',
'name=',
$fieldName,
'/',
]);
}
});
it('rejects a submission by a non administrator', function () {
// Arrange
actingAs($this->nonAdminUser);
// Act & Assert
$response = post(route('admin.schools.store'), [
'name' => 'Hacker High',
'address' => 'Lost Highway',
]);
$response->assertRedirect(route('dashboard'));
});
it('allows an administrator to create a school', function () {
// Arrange
actingAs($this->adminUser);
$newData = [
'name' => fake()->city(),
'address' => fake()->streetAddress(),
'city' => fake()->city(),
'state' => 'OK',
'zip' => fake()->postcode(),
];
// Act
$response = post(route('admin.schools.store'), $newData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.schools.index'));
// Get the latest created user
$createdSchool = School::where('name', $newData['name'])->first();
expect($createdSchool->name)->toBe($newData['name'])
->and($createdSchool->address)->toBe($newData['address'])
->and($createdSchool->city)->toBe($newData['city'])
->and($createdSchool->state)->toBe($newData['state'])
->and($createdSchool->zip)->toEqual($newData['zip']);
get(route('admin.schools.index'))
->assertOk()
->assertSee($newData['name']);
});