Last active
July 21, 2019 08:59
-
-
Save sasin91/3c8e26f3660d70e70669384fa964c6f5 to your computer and use it in GitHub Desktop.
Auto mapping permissions to policies
This file contains hidden or 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\Unit\Authorization; | |
| use Tests\TestCase; | |
| use Illuminate\Foundation\Testing\RefreshDatabase; | |
| class AutomappingPermissionsTest extends TestCase | |
| { | |
| use RefreshDatabase; | |
| /** | |
| * All the class methods of the role policy | |
| * | |
| * @example [['viewAny'], ['view'], ['create'], ['update'], ['delete']] | |
| * @return array | |
| */ | |
| public function automappableActions(): array | |
| { | |
| return array_map(function (string $methodName) { | |
| return [$methodName]; | |
| }, get_class_methods(RolePolicy::class)); | |
| } | |
| /** | |
| * Test that the permission automatically maps the policy permission correctly, based on naming conventions. | |
| * | |
| * -> Permission::created (name="Create Role") :: ID = 1 | |
| * -> Gate::getPolicyFor(Role::class) | |
| * -> PolicyPermission::create(class_name="App\Policies\RolePolicy", method_name="create", permission_id=1) | |
| * | |
| * @test | |
| * @dataProvider automappableActions | |
| * @param string $action | |
| * @see \App\Concerns\AutomapsPolicyPermissions | |
| * @return void | |
| */ | |
| public function testItAutomapsThePolicyPermissionBasedOnNamingConventions(string $action) | |
| { | |
| $permission = Permission::create(['name' => "{$action} Role"]); | |
| $this->assertDatabaseHas(PolicyPermission::make()->getTable(), [ | |
| 'class_name' => RolePolicy::class, | |
| 'method_name' => $action, | |
| 'permission_id' => $permission->id | |
| ]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment