Skip to content

Commit

Permalink
Added Role index,create,show,edit tests & func
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinByrne committed Nov 8, 2020
1 parent 0d8eaaf commit 6d2378b
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/Http/Controllers/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@

class RoleController extends Controller
{
/**
* Show all the roles
*
* @return \Illuminate\Http\Response
*/
public function index()
{
abort_if(Gate::denies('role_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

$roles = Role::all();
}

/**
* Show the form to create a role
*
* @return \Illuminate\Http\Response
*/
public function create()
{
abort_if(Gate::denies('role_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
}

/**
* Create new role
*
Expand All @@ -24,6 +46,28 @@ public function store(StoreRoleRequest $request)
return redirect($role->path());
}

/**
* Show all the individual role
*
* @param \App\Models\Role $role
* @return \Illuminate\Http\Response
*/
public function show(Role $role)
{
abort_if(Gate::denies('role_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
}

/**
* Show the form to edit a role
*
* @param \App\Models\Role $role
* @return \Illuminate\Http\Response
*/
public function edit(Role $role)
{
abort_if(Gate::denies('role_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
}

/**
* Update a role
*
Expand Down
139 changes: 139 additions & 0 deletions tests/Feature/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,72 @@ class RoleTest extends TestCase
{
use RefreshDatabase;
use WithFaker;

/**
* test the index of roles.
*
* @return void
*/
public function testRoleIndex()
{
$this->withoutExceptionHandling();

$this->seed();
$user = User::factory()->create();
$adminId = Role::find(1)->id;
$user->roles()->sync([$adminId]);

$response = $this->actingAs($user)->get(route('roles.index'));

$response->assertOk();
}

/**
* test the index of roles without permission.
*
* @return void
*/
public function testRoleIndexWithoutPermission()
{
$response = $this->get(route('roles.index'));

$response->assertStatus(403);
}

/**
* test role create form.
*
* @return void
*/
public function testRoleCreateForm()
{
$this->withoutExceptionHandling();

$this->seed();
$user = User::factory()->create();
$adminId = Role::find(1)->id;
$user->roles()->sync([$adminId]);

$role = Role::factory()->create();

$response = $this->actingAs($user)->get(route('roles.create', [$role->id]));

$response->assertOk();
}

/**
* test role create form without permission.
*
* @return void
*/
public function testRoleCreateFormWithoutPermission()
{
$role = Role::factory()->create();

$response = $this->get(route('roles.create', [$role->id]));

$response->assertStatus(403);
}

/**
* test create new role.
Expand Down Expand Up @@ -80,6 +146,79 @@ public function testNewRoleWithoutPermission()
$response->assertStatus(403);
}

/**
* test a role can be shown.
*
* @return void
*/
public function testShowingRole()
{
$this->withoutExceptionHandling();

$this->seed();
$user = User::factory()->create();
$adminId = Role::find(1)->id;
$user->roles()->sync([$adminId]);

$role = Role::factory()->create();

$response = $this->actingAs($user)->get(route('roles.show', [$role->id]));

$response->assertOk();
}

/**
* test a role can be shown without permission.
*
* @return void
*/
public function testShowingRoleWithoutPermission()
{
$this->seed();
$user = User::factory()->create();

$role = Role::factory()->create();

$response = $this->actingAs($user)->get(route('roles.show', [$role->id]));

$response->assertStatus(403);
}

/**
* test role edit form.
*
* @return void
*/
public function testRoleEditForm()
{
$this->withoutExceptionHandling();

$this->seed();
$user = User::factory()->create();
$adminId = Role::find(1)->id;
$user->roles()->sync([$adminId]);

$role = Role::factory()->create();

$response = $this->actingAs($user)->get(route('roles.edit', [$role->id]));

$response->assertOk();
}

/**
* test role edit form without permission.
*
* @return void
*/
public function testRoleEditFormWithoutPermission()
{
$role = Role::factory()->create();

$response = $this->get(route('roles.edit', [$role->id]));

$response->assertStatus(403);
}

/**
* test a role can be updated.
*
Expand Down

0 comments on commit 6d2378b

Please sign in to comment.