Skip to content

Commit

Permalink
Add PHPUnit annotations, for future compatibility with PHPUnit 12
Browse files Browse the repository at this point in the history
Annotations supported since PHPUnit 10
Docblock comments will be ignored in future

In a later release of this package (probably `v7.0`), will remove PHPUnit 9 support and all associated doc-comments.
  • Loading branch information
drbyte committed Feb 13, 2025
1 parent a6e35ee commit 85da609
Show file tree
Hide file tree
Showing 33 changed files with 396 additions and 30 deletions.
26 changes: 26 additions & 0 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Permission\Tests;

use Illuminate\Support\Facades\Artisan;
use PHPUnit\Framework\Attributes\Test;
use Spatie\Permission\Contracts\Role;

class BladeTest extends TestCase
Expand All @@ -21,6 +22,7 @@ protected function setUp(): void
}

/** @test */
#[Test]
public function all_blade_directives_will_evaluate_false_when_there_is_nobody_logged_in()
{
$permission = 'edit-articles';
Expand All @@ -40,6 +42,7 @@ public function all_blade_directives_will_evaluate_false_when_there_is_nobody_lo
}

/** @test */
#[Test]
public function all_blade_directives_will_evaluate_false_when_somebody_without_roles_or_permissions_is_logged_in()
{
$permission = 'edit-articles';
Expand All @@ -59,6 +62,7 @@ public function all_blade_directives_will_evaluate_false_when_somebody_without_r
}

/** @test */
#[Test]
public function all_blade_directives_will_evaluate_false_when_somebody_with_another_guard_is_logged_in()
{
$permission = 'edit-articles';
Expand All @@ -79,6 +83,7 @@ public function all_blade_directives_will_evaluate_false_when_somebody_with_anot
}

/** @test */
#[Test]
public function the_can_directive_can_accept_a_guard_name()
{
$user = $this->getWriter();
Expand Down Expand Up @@ -109,6 +114,7 @@ public function the_can_directive_can_accept_a_guard_name()
}

/** @test */
#[Test]
public function the_can_directive_will_evaluate_true_when_the_logged_in_user_has_the_permission()
{
$user = $this->getWriter();
Expand All @@ -121,6 +127,7 @@ public function the_can_directive_will_evaluate_true_when_the_logged_in_user_has
}

/** @test */
#[Test]
public function the_haspermission_directive_will_evaluate_true_when_the_logged_in_user_has_the_permission()
{
$user = $this->getWriter();
Expand All @@ -145,6 +152,7 @@ public function the_haspermission_directive_will_evaluate_true_when_the_logged_i
}

/** @test */
#[Test]
public function the_role_directive_will_evaluate_true_when_the_logged_in_user_has_the_role()
{
auth()->setUser($this->getWriter());
Expand All @@ -153,6 +161,7 @@ public function the_role_directive_will_evaluate_true_when_the_logged_in_user_ha
}

/** @test */
#[Test]
public function the_elserole_directive_will_evaluate_true_when_the_logged_in_user_has_the_role()
{
auth()->setUser($this->getMember());
Expand All @@ -161,6 +170,7 @@ public function the_elserole_directive_will_evaluate_true_when_the_logged_in_use
}

/** @test */
#[Test]
public function the_role_directive_will_evaluate_true_when_the_logged_in_user_has_the_role_for_the_given_guard()
{
auth('admin')->setUser($this->getSuperAdmin());
Expand All @@ -169,6 +179,7 @@ public function the_role_directive_will_evaluate_true_when_the_logged_in_user_ha
}

/** @test */
#[Test]
public function the_hasrole_directive_will_evaluate_true_when_the_logged_in_user_has_the_role()
{
auth()->setUser($this->getWriter());
Expand All @@ -177,6 +188,7 @@ public function the_hasrole_directive_will_evaluate_true_when_the_logged_in_user
}

/** @test */
#[Test]
public function the_hasrole_directive_will_evaluate_true_when_the_logged_in_user_has_the_role_for_the_given_guard()
{
auth('admin')->setUser($this->getSuperAdmin());
Expand All @@ -185,6 +197,7 @@ public function the_hasrole_directive_will_evaluate_true_when_the_logged_in_user
}

/** @test */
#[Test]
public function the_unlessrole_directive_will_evaluate_true_when_the_logged_in_user_does_not_have_the_role()
{
auth()->setUser($this->getWriter());
Expand All @@ -193,6 +206,7 @@ public function the_unlessrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
#[Test]
public function the_unlessrole_directive_will_evaluate_true_when_the_logged_in_user_does_not_have_the_role_for_the_given_guard()
{
auth('admin')->setUser($this->getSuperAdmin());
Expand All @@ -202,6 +216,7 @@ public function the_unlessrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
#[Test]
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_any_of_the_required_roles()
{
$roles = ['writer', 'intern'];
Expand All @@ -213,6 +228,7 @@ public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles()
{
$roles = ['member', 'writer', 'intern'];
Expand All @@ -224,6 +240,7 @@ public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
#[Test]
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_for_the_given_guard()
{
$roles = ['super-admin', 'moderator'];
Expand All @@ -235,6 +252,7 @@ public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
#[Test]
public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_user_does_have_some_of_the_required_roles_in_pipe()
{
$guard = 'admin';
Expand All @@ -245,6 +263,7 @@ public function the_hasanyrole_directive_will_evaluate_true_when_the_logged_in_u
}

/** @test */
#[Test]
public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_some_of_the_required_roles_in_pipe()
{
$guard = '';
Expand All @@ -255,6 +274,7 @@ public function the_hasanyrole_directive_will_evaluate_false_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_does_not_have_all_required_roles()
{
$roles = ['member', 'writer'];
Expand All @@ -266,6 +286,7 @@ public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles()
{
$roles = ['member', 'writer'];
Expand All @@ -281,6 +302,7 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_for_the_given_guard()
{
$roles = ['super-admin', 'moderator'];
Expand All @@ -296,6 +318,7 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_pipe()
{
$guard = 'admin';
Expand All @@ -310,6 +333,7 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_pipe()
{
$guard = '';
Expand All @@ -323,6 +347,7 @@ public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_user_does_have_all_required_roles_in_array()
{
$guard = 'admin';
Expand All @@ -337,6 +362,7 @@ public function the_hasallroles_directive_will_evaluate_true_when_the_logged_in_
}

/** @test */
#[Test]
public function the_hasallroles_directive_will_evaluate_false_when_the_logged_in_user_doesnt_have_all_required_roles_in_array()
{
$guard = '';
Expand Down
17 changes: 17 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\Test;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
Expand Down Expand Up @@ -42,6 +43,7 @@ protected function setUp(): void
}

/** @test */
#[Test]
public function it_can_cache_the_permissions()
{
$this->resetQueryCount();
Expand All @@ -52,6 +54,7 @@ public function it_can_cache_the_permissions()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_creating_a_permission()
{
app(Permission::class)->create(['name' => 'new']);
Expand All @@ -64,6 +67,7 @@ public function it_flushes_the_cache_when_creating_a_permission()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_updating_a_permission()
{
$permission = app(Permission::class)->create(['name' => 'new']);
Expand All @@ -79,6 +83,7 @@ public function it_flushes_the_cache_when_updating_a_permission()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_creating_a_role()
{
app(Role::class)->create(['name' => 'new']);
Expand All @@ -91,6 +96,7 @@ public function it_flushes_the_cache_when_creating_a_role()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_updating_a_role()
{
$role = app(Role::class)->create(['name' => 'new']);
Expand All @@ -106,6 +112,7 @@ public function it_flushes_the_cache_when_updating_a_role()
}

/** @test */
#[Test]
public function removing_a_permission_from_a_user_should_not_flush_the_cache()
{
$this->testUser->givePermissionTo('edit-articles');
Expand All @@ -122,6 +129,7 @@ public function removing_a_permission_from_a_user_should_not_flush_the_cache()
}

/** @test */
#[Test]
public function removing_a_role_from_a_user_should_not_flush_the_cache()
{
$this->testUser->assignRole('testRole');
Expand All @@ -138,6 +146,7 @@ public function removing_a_role_from_a_user_should_not_flush_the_cache()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_removing_a_role_from_a_permission()
{
$this->testUserPermission->assignRole('testRole');
Expand All @@ -154,6 +163,7 @@ public function it_flushes_the_cache_when_removing_a_role_from_a_permission()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_assign_a_permission_to_a_role()
{
$this->testUserRole->givePermissionTo('edit-articles');
Expand All @@ -166,6 +176,7 @@ public function it_flushes_the_cache_when_assign_a_permission_to_a_role()
}

/** @test */
#[Test]
public function user_creation_should_not_flush_the_cache()
{
$this->registrar->getPermissions();
Expand All @@ -181,6 +192,7 @@ public function user_creation_should_not_flush_the_cache()
}

/** @test */
#[Test]
public function it_flushes_the_cache_when_giving_a_permission_to_a_role()
{
$this->testUserRole->givePermissionTo($this->testUserPermission);
Expand All @@ -193,6 +205,7 @@ public function it_flushes_the_cache_when_giving_a_permission_to_a_role()
}

/** @test */
#[Test]
public function has_permission_to_should_use_the_cache()
{
$this->testUserRole->givePermissionTo(['edit-articles', 'edit-news', 'Edit News']);
Expand All @@ -217,6 +230,7 @@ public function has_permission_to_should_use_the_cache()
}

/** @test */
#[Test]
public function the_cache_should_differentiate_by_guard_name()
{
$this->expectException(PermissionDoesNotExist::class);
Expand All @@ -235,6 +249,7 @@ public function the_cache_should_differentiate_by_guard_name()
}

/** @test */
#[Test]
public function get_all_permissions_should_use_the_cache()
{
$this->testUserRole->givePermissionTo($expected = ['edit-articles', 'edit-news']);
Expand All @@ -253,6 +268,7 @@ public function get_all_permissions_should_use_the_cache()
}

/** @test */
#[Test]
public function get_all_permissions_should_not_over_hydrate_roles()
{
$this->testUserRole->givePermissionTo(['edit-articles', 'edit-news']);
Expand All @@ -264,6 +280,7 @@ public function get_all_permissions_should_not_over_hydrate_roles()
}

/** @test */
#[Test]
public function it_can_reset_the_cache_with_artisan_command()
{
Artisan::call('permission:create-permission', ['name' => 'new-permission']);
Expand Down
Loading

0 comments on commit 85da609

Please sign in to comment.