Skip to content

Instantly share code, notes, and snippets.

@scrubmx
Created March 13, 2017 16:29
Show Gist options
  • Save scrubmx/dc397cbe75a6dde29468d6a972e70de8 to your computer and use it in GitHub Desktop.
Save scrubmx/dc397cbe75a6dde29468d6a972e70de8 to your computer and use it in GitHub Desktop.
Laravel authorization test helpers
<?php
namespace Tests;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Determine if the current user has a given ability.
*
* @param string $ability
* @param array|mixed $arguments
* @return bool
*/
protected function isAuthorized($ability, $arguments = [])
{
return $this->app->make(Gate::class)->check($ability, $arguments);
}
/**
* Determine if the current user has a given ability.
*
* @param string $ability
* @param array|mixed $arguments
* @return $this
*/
public function seeIsAuthorized($ability, $arguments = [])
{
$this->assertTrue($this->isAuthorized($ability, $arguments), 'The user is not authorized');
return $this;
}
/**
* Determine if the current user does not have a given ability.
*
* @param string $ability
* @param array|mixed $arguments
* @return $this
*/
public function dontSeeIsAuthorized($ability, $arguments = [])
{
$this->assertFalse($this->isAuthorized($ability, $arguments), 'The user is authorized');
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment