Created
March 13, 2017 16:29
-
-
Save scrubmx/dc397cbe75a6dde29468d6a972e70de8 to your computer and use it in GitHub Desktop.
Laravel authorization test helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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