diff --git a/.gitattributes b/.gitattributes index 7e08fcde..6ccdbd3e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,3 +7,4 @@ /phpunit.xml export-ignore /readme.md export-ignore /tests export-ignore +/workbench export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 29ace4c7..53c9c930 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,23 +16,10 @@ jobs: runs-on: ubuntu-latest strategy: - matrix: - laravel: [6.*, 7.*, 8.*, 9.*, 10.*] - php: [7.2, 7.3, 8.0, 8.1, 8.2] - exclude: - - laravel: 10.* - php: 7.2 - - laravel: 10.* - php: 7.3 - - laravel: 10.* - php: 8.0 - - laravel: 8.* - php: 7.2 - - laravel: 9.* - php: 7.2 - - laravel: 9.* - php: 7.3 fail-fast: false + matrix: + laravel: [11.*] + php: [8.2, 8.3] name: Laravel ${{ matrix.laravel }}, PHP ${{ matrix.php }} @@ -48,8 +35,8 @@ jobs: - name: Install dependencies run: | - composer require "illuminate/auth:${{ matrix.laravel }}" "illuminate/cache:${{ matrix.laravel }}" "illuminate/container:${{ matrix.laravel }}" "illuminate/contracts:${{ matrix.laravel }}" "illuminate/database:${{ matrix.laravel }}" "illuminate/console:${{ matrix.laravel }}" "illuminate/events:${{ matrix.laravel }}" --no-interaction --no-update + composer require --dev "illuminate/auth:${{ matrix.laravel }}" "illuminate/cache:${{ matrix.laravel }}" "illuminate/container:${{ matrix.laravel }}" "illuminate/contracts:${{ matrix.laravel }}" "illuminate/database:${{ matrix.laravel }}" "illuminate/console:${{ matrix.laravel }}" "illuminate/events:${{ matrix.laravel }}" --no-interaction --no-update composer update --prefer-stable --prefer-dist --no-interaction --no-suggest - name: Run tests - run: vendor/bin/phpunit --do-not-cache-result + run: vendor/bin/pest --do-not-cache-result diff --git a/.gitignore b/.gitignore index 77bed77d..eabdb1f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.phpunit.cache/test-results .phpunit.result.cache composer.lock -vendor +vendor \ No newline at end of file diff --git a/composer.json b/composer.json index e8842b4c..79ff4575 100644 --- a/composer.json +++ b/composer.json @@ -24,33 +24,48 @@ }, "autoload-dev": { "psr-4": { - "Silber\\Bouncer\\Tests\\": "tests/" - }, - "files": [ - "tests/helpers.php" - ] + "Silber\\Bouncer\\Tests\\": "tests/", + "Workbench\\App\\": "workbench/app/", + "Workbench\\Database\\Factories\\": "workbench/database/factories/", + "Workbench\\Database\\Seeders\\": "workbench/database/seeders/" + } }, "require": { - "php": "^7.2|^8.0", - "illuminate/auth": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/cache": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/container": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/database": "^6.0|^7.0|^8.0|^9.0|^10.0" + "php": "^8.1|^8.2|^8.3", + "illuminate/auth": "^11.0", + "illuminate/cache": "^11.0", + "illuminate/container": "^11.0", + "illuminate/contracts": "^11.0", + "illuminate/database": "^11.0" }, "require-dev": { - "illuminate/console": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/events": "^6.0|^7.0|^8.0|^9.0|^10.0", - "larapack/dd": "^1.1", - "mockery/mockery": "^1.3.3", - "phpunit/phpunit": "^8.0|^9.0" + "illuminate/console": "^11.0", + "illuminate/events": "^11.0", + "phpunit/phpunit": "^11.0", + "orchestra/testbench": "^9.0", + "pestphp/pest": "3.x-dev" }, "suggest": { "illuminate/console": "Allows running the bouncer:clean artisan command", "illuminate/events": "Required for multi-tenancy support" }, "scripts": { - "test": "phpunit" + "test": "phpunit", + "post-autoload-dump": [ + "@clear", + "@prepare" + ], + "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": "@php vendor/bin/testbench workbench:build --ansi", + "serve": [ + "Composer\\Config::disableProcessTimeout", + "@build", + "@php vendor/bin/testbench serve" + ], + "lint": [ + "@php vendor/bin/phpstan analyse" + ] }, "minimum-stability": "dev", "prefer-stable": true, @@ -63,5 +78,10 @@ "Bouncer": "Silber\\Bouncer\\BouncerFacade" } } + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } } } diff --git a/migrations/create_bouncer_tables.php b/migrations/create_bouncer_tables.php index c1072026..3ae38dde 100644 --- a/migrations/create_bouncer_tables.php +++ b/migrations/create_bouncer_tables.php @@ -6,7 +6,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateBouncerTables extends Migration +return new class extends Migration { /** * Run the migrations. @@ -90,4 +90,4 @@ public function down() Schema::drop(Models::table('roles')); Schema::drop(Models::table('abilities')); } -} +}; diff --git a/phpunit.xml b/phpunit.xml index 422eeac6..78562694 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,11 @@ - - - - ./tests/ - - + + + + + + + ./tests/ + + diff --git a/tests/AbilitiesForModelsTest.php b/tests/AbilitiesForModelsTest.php index f7380c56..80a8d56c 100644 --- a/tests/AbilitiesForModelsTest.php +++ b/tests/AbilitiesForModelsTest.php @@ -2,20 +2,23 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Ability; use Illuminate\Database\Eloquent\Model; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class AbilitiesForModelsTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function model_blanket_ability($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function model_blanket_ability($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('edit', User::class); @@ -36,13 +39,11 @@ function model_blanket_ability($provider) $this->assertTrue($bouncer->cannot('edit', $user2)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function individual_model_ability($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function individual_model_ability($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('edit', $user2); @@ -67,13 +68,11 @@ function individual_model_ability($provider) $this->assertTrue($bouncer->cannot('edit', $user2)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function blanket_ability_and_individual_model_ability_are_kept_separate($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function blanket_ability_and_individual_model_ability_are_kept_separate($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('edit', User::class); $bouncer->allow($user1)->to('edit', $user2); @@ -87,23 +86,19 @@ function blanket_ability_and_individual_model_ability_are_kept_separate($provide $this->assertTrue($bouncer->can('edit', $user2)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_on_non_existent_model_throws($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_on_non_existent_model_throws($provider) { $this->expectException('InvalidArgumentException'); - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete', new User); } - /** - * @test - */ - function can_create_an_ability_for_a_model() + #[Test] + public function can_create_an_ability_for_a_model() { $ability = Ability::createForModel(Account::class, 'delete'); @@ -112,10 +107,8 @@ function can_create_an_ability_for_a_model() $this->assertNull($ability->entity_id); } - /** - * @test - */ - function can_create_an_ability_for_a_model_plus_extra_attributes() + #[Test] + public function can_create_an_ability_for_a_model_plus_extra_attributes() { $ability = Ability::createForModel(Account::class, [ 'name' => 'delete', @@ -128,10 +121,8 @@ function can_create_an_ability_for_a_model_plus_extra_attributes() $this->assertNull($ability->entity_id); } - /** - * @test - */ - function can_create_an_ability_for_a_model_instance() + #[Test] + public function can_create_an_ability_for_a_model_instance() { $user = User::create(); @@ -142,10 +133,8 @@ function can_create_an_ability_for_a_model_instance() $this->assertEquals('delete', $ability->name); } - /** - * @test - */ - function can_create_an_ability_for_a_model_instance_plus_extra_attributes() + #[Test] + public function can_create_an_ability_for_a_model_instance_plus_extra_attributes() { $user = User::create(); @@ -160,10 +149,8 @@ function can_create_an_ability_for_a_model_instance_plus_extra_attributes() $this->assertEquals('delete', $ability->name); } - /** - * @test - */ - function can_create_an_ability_for_all_models() + #[Test] + public function can_create_an_ability_for_all_models() { $ability = Ability::createForModel('*', 'delete'); @@ -172,10 +159,8 @@ function can_create_an_ability_for_all_models() $this->assertNull($ability->entity_id); } - /** - * @test - */ - function can_create_an_ability_for_all_models_plus_extra_attributes() + #[Test] + public function can_create_an_ability_for_all_models_plus_extra_attributes() { $ability = Ability::createForModel('*', [ 'name' => 'delete', diff --git a/tests/AbilityConstraintsTest.php b/tests/AbilityConstraintsTest.php index eb94fa63..4249ed44 100644 --- a/tests/AbilityConstraintsTest.php +++ b/tests/AbilityConstraintsTest.php @@ -2,26 +2,27 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Ability; use Silber\Bouncer\Constraints\Group; use Silber\Bouncer\Constraints\Constraint; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class AbilityConstraintsTest extends BaseTestCase { - /** - * @test - */ - function can_get_empty_constraints() + #[Test] + public function can_get_empty_constraints() { $group = Ability::createForModel(Account::class, '*')->getConstraints(); $this->assertInstanceOf(Group::class, $group); } - /** - * @test - */ - function can_check_if_has_constraints() + #[Test] + public function can_check_if_has_constraints() { $empty = Ability::makeForModel(Account::class, '*'); @@ -33,10 +34,8 @@ function can_check_if_has_constraints() $this->assertTrue($full->hasConstraints()); } - /** - * @test - */ - function can_set_and_get_constraints() + #[Test] + public function can_set_and_get_constraints() { $ability = Ability::makeForModel(Account::class, '*')->setConstraints( new Group([ diff --git a/tests/AuthorizableTest.php b/tests/AuthorizableTest.php index 09590dda..7af0ad82 100644 --- a/tests/AuthorizableTest.php +++ b/tests/AuthorizableTest.php @@ -2,17 +2,20 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class AuthorizableTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function checking_simple_abilities_on_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function checking_simple_abilities_on_roles($provider) { $provider(); @@ -25,11 +28,9 @@ function checking_simple_abilities_on_roles($provider) $this->assertTrue($role->cannot('cry')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function checking_model_abilities_on_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function checking_model_abilities_on_roles($provider) { $provider(); diff --git a/tests/AutoTitlesTest.php b/tests/AutoTitlesTest.php index 7986fdb0..6cd68e50 100644 --- a/tests/AutoTitlesTest.php +++ b/tests/AutoTitlesTest.php @@ -2,85 +2,74 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class AutoTitlesTest extends BaseTestCase { - /** - * @test - */ - function role_title_is_never_overwritten() + #[Test] + public function role_title_is_never_overwritten() { $role = Role::create(['name' => 'admin', 'title' => 'Something Else']); $this->assertEquals('Something Else', $role->title); } - /** - * @test - */ - function role_title_is_capitalized() + #[Test] + public function role_title_is_capitalized() { $role = Role::create(['name' => 'admin']); $this->assertEquals('Admin', $role->title); } - /** - * @test - */ - function role_title_with_spaces() + #[Test] + public function role_title_with_spaces() { $role = Role::create(['name' => 'site admin']); $this->assertEquals('Site admin', $role->title); } - /** - * @test - */ - function role_title_with_dashes() + #[Test] + public function role_title_with_dashes() { $role = Role::create(['name' => 'site-admin']); $this->assertEquals('Site admin', $role->title); } - /** - * @test - */ - function role_title_with_underscores() + #[Test] + public function role_title_with_underscores() { $role = Role::create(['name' => 'site_admin']); $this->assertEquals('Site admin', $role->title); } - /** - * @test - */ - function role_title_with_camel_casing() + #[Test] + public function role_title_with_camel_casing() { $role = Role::create(['name' => 'siteAdmin']); $this->assertEquals('Site admin', $role->title); } - /** - * @test - */ - function role_title_with_studly_casing() + #[Test] + public function role_title_with_studly_casing() { $role = Role::create(['name' => 'SiteAdmin']); $this->assertEquals('Site admin', $role->title); } - /** - * @test - */ - function ability_title_is_never_overwritten() + #[Test] + public function ability_title_is_never_overwritten() { $bouncer = $this->bouncer($user = User::create()); @@ -91,10 +80,8 @@ function ability_title_is_never_overwritten() $this->assertEquals('Something Else', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_wildcards() + #[Test] + public function ability_title_is_set_for_wildcards() { $bouncer = $this->bouncer($user = User::create()); @@ -103,10 +90,8 @@ function ability_title_is_set_for_wildcards() $this->assertEquals('All abilities', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_restricted_wildcards() + #[Test] + public function ability_title_is_set_for_restricted_wildcards() { $bouncer = $this->bouncer($user = User::create()); @@ -115,10 +100,8 @@ function ability_title_is_set_for_restricted_wildcards() $this->assertEquals('All simple abilities', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_simple_abilities() + #[Test] + public function ability_title_is_set_for_simple_abilities() { $bouncer = $this->bouncer($user = User::create()); @@ -127,10 +110,8 @@ function ability_title_is_set_for_simple_abilities() $this->assertEquals('Ban users', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_blanket_ownership_ability() + #[Test] + public function ability_title_is_set_for_blanket_ownership_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -139,10 +120,8 @@ function ability_title_is_set_for_blanket_ownership_ability() $this->assertEquals('Manage everything owned', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_restricted_ownership_ability() + #[Test] + public function ability_title_is_set_for_restricted_ownership_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -151,10 +130,8 @@ function ability_title_is_set_for_restricted_ownership_ability() $this->assertEquals('Edit everything owned', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_management_ability() + #[Test] + public function ability_title_is_set_for_management_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -163,10 +140,8 @@ function ability_title_is_set_for_management_ability() $this->assertEquals('Manage users', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_blanket_model_ability() + #[Test] + public function ability_title_is_set_for_blanket_model_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -175,10 +150,8 @@ function ability_title_is_set_for_blanket_model_ability() $this->assertEquals('Create users', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_regular_model_ability() + #[Test] + public function ability_title_is_set_for_regular_model_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -187,10 +160,8 @@ function ability_title_is_set_for_regular_model_ability() $this->assertEquals('Delete user #2', $bouncer->ability()->first()->title); } - /** - * @test - */ - function ability_title_is_set_for_a_global_action_ability() + #[Test] + public function ability_title_is_set_for_a_global_action_ability() { $bouncer = $this->bouncer($user = User::create()); diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index c3f800da..cd9edaff 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -2,229 +2,108 @@ namespace Silber\Bouncer\Tests; -require __DIR__.'/../migrations/create_bouncer_tables.php'; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; -use CreateBouncerTables; +use function Orchestra\Testbench\artisan; +use function Orchestra\Testbench\workbench_path; +use function Orchestra\Testbench\package_path; + +use Silber\Bouncer\Database\Models; +use Workbench\App\Models\User; +use Illuminate\Database\DatabaseManager; use Silber\Bouncer\Guard; use Silber\Bouncer\Bouncer; use Silber\Bouncer\Clipboard; -use Silber\Bouncer\Database\Models; -use Silber\Bouncer\Database\HasRolesAndAbilities; -use Silber\Bouncer\Database\Concerns\Authorizable; use Silber\Bouncer\Contracts\Clipboard as ClipboardContract; +use Illuminate\Foundation\Testing\RefreshDatabase; -use PHPUnit\Framework\TestCase; use Illuminate\Auth\Access\Gate; -use Illuminate\Cache\ArrayStore; -use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; -use Illuminate\Support\Facades\Schema; -use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Database\Capsule\Manager as DB; -use Illuminate\Database\Eloquent\Model as Eloquent; +use Illuminate\Database\Eloquent\Model; +use Orchestra\Testbench\TestCase; abstract class BaseTestCase extends TestCase { - /** - * The clipboard instance. - * - * @var \Silber\Bouncer\Contracts\Clipboard - */ - protected $clipboard; - - /** - * The database capsule instance. - * - * @var \Illuminate\Database\Capsule\Manager - */ - protected $db; - - /** - * The event dispatcher instance. - * - * @var \Illuminate\Events\Dispatcher - */ - protected static $dispatcher; + use RefreshDatabase; /** * Setup the world for the tests. - * - * @return void */ public function setUp(): void { - Container::setInstance(new Container); - - $this->registerDatabaseContainerBindings(); - - $this->migrate(); - + parent::setUp(); + Models::setUsersModel(User::class); - $this->registerClipboard(); - } - - protected function registerClipboard() - { - $this->clipboard = new Clipboard; - - Container::getInstance()->bind(ClipboardContract::class, function () { - return $this->clipboard; - }); - } - - protected function registerDatabaseContainerBindings() - { - $container = Container::getInstance(); - - Schema::setFacadeApplication($container); - - $container->bind('db', function () { - return $this->db(); - }); - - $container->bind('db.schema', function () { - return $this->db()->getConnection()->getSchemaBuilder(); - }); - } - - protected function migrate() - { - $this->db(); - - (new CreateBouncerTables)->up(); - - $this->migrateTestTables(); - } - - protected function migrateTestTables() - { - Schema::create('users', function ($table) { - $table->increments('id'); - $table->string('name')->nullable(); - $table->integer('age')->nullable(); - $table->timestamps(); - $table->softDeletes(); - }); - - Schema::create('accounts', function ($table) { - $table->increments('id'); - $table->integer('user_id')->unsigned()->nullable(); - $table->string('name')->nullable(); - $table->boolean('active')->default(true); - $table->timestamps(); - }); + static::registerClipboard(); } /** - * Tear down the database schema. + * Define database migrations. * * @return void */ - public function tearDown(): void + protected function defineDatabaseMigrations() { - $this->rollbackTestTables(); + artisan($this, 'migrate:install'); - (new CreateBouncerTables)->down(); + $this->loadMigrationsFrom(package_path('migrations')); + $this->loadMigrationsFrom(workbench_path('database/migrations')); + } - $this->clipboard = $this->db = null; + /** + * Register the clipboard with the container. + */ + protected static function registerClipboard(): void + { + Container::getInstance() + ->instance(ClipboardContract::class, static::makeClipboard()); } - protected function rollbackTestTables() + /** + * Make a new clipboard with the container. + */ + protected static function makeClipboard(): ClipboardContract { - Schema::drop('users'); - Schema::drop('accounts'); + return new Clipboard; } /** * Get a bouncer instance. - * - * @param \Illuminate\Database\Eloquent\Model|null $user - * @return \Silber\Bouncer\Bouncer */ - protected function bouncer(Eloquent $authority = null) + protected static function bouncer(Model $authority = null): Bouncer { - $gate = $this->gate($authority ?: User::create()); + $gate = static::gate($authority ?: User::create()); + + $clipboard = Container::getInstance()->make(ClipboardContract::class); - $bouncer = new Bouncer((new Guard($this->clipboard))->registerAt($gate)); + $bouncer = new Bouncer((new Guard($clipboard))->registerAt($gate)); return $bouncer->setGate($gate); } /** * Get an access gate instance. - * - * @param \Illuminate\Database\Eloquent\Model $user - * @return \Illuminate\Auth\Access\Gate */ - protected function gate(Eloquent $authority) + protected static function gate(Model $authority): Gate { - return new Gate(Container::getInstance(), function () use ($authority) { - return $authority; - }); + return new Gate(Container::getInstance(), fn () => $authority); } /** - * Get an instance of the database capsule manager. - * - * @return \Illuminate\Database\Capsule\Manager + * Get the Clipboard instance from the container. */ - protected function db() + protected function clipboard(): ClipboardContract { - if ($this->db) { - return $this->db; - } - - $this->db = new DB; - - $this->db->addConnection([ - 'driver' => 'sqlite', - 'database' => ':memory:', - ]); - - $this->db->bootEloquent(); - - $this->db->setAsGlobal(); - - Eloquent::setEventDispatcher($this->dispatcher()); - - return $this->db; + return Container::getInstance()->make(ClipboardContract::class); } /** - * Get the event dispatcher instance. - * - * @return \Illuminate\Events\Dispatcher + * Get the DB manager instance from the container. */ - protected function dispatcher() + protected function db(): DatabaseManager { - if (is_null(static::$dispatcher)) { - static::$dispatcher = new Dispatcher; - } - - return static::$dispatcher; + return Container::getInstance()->make('db'); } } - -class User extends Eloquent -{ - use Authorizable, HasRolesAndAbilities; - - protected $table = 'users'; - - protected $guarded = []; -} - -class UserWithSoftDeletes extends User -{ - use SoftDeletes; -} - -class Account extends Eloquent -{ - use Authorizable, HasRolesAndAbilities; - - protected $table = 'accounts'; - - protected $guarded = []; -} diff --git a/tests/BeforePoliciesTest.php b/tests/BeforePoliciesTest.php index 5a92a70a..2ce3415e 100644 --- a/tests/BeforePoliciesTest.php +++ b/tests/BeforePoliciesTest.php @@ -2,22 +2,25 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Bouncer; use Illuminate\Auth\Access\Gate; use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class BeforePoliciesTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function policy_forbids_and_bouncer_allows($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function policy_forbids_and_bouncer_allows($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->setUpWithPolicy($bouncer); @@ -32,13 +35,11 @@ function policy_forbids_and_bouncer_allows($provider) $this->assertTrue($bouncer->can('view', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function policy_allows_and_bouncer_forbids($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function policy_allows_and_bouncer_forbids($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->setUpWithPolicy($bouncer); @@ -53,13 +54,11 @@ function policy_allows_and_bouncer_forbids($provider) $this->assertTrue($bouncer->cannot('view', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function passes_auth_check_when_bouncer_allows($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function passes_auth_check_when_bouncer_allows($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->setUpWithPolicy($bouncer); @@ -74,13 +73,11 @@ function passes_auth_check_when_bouncer_allows($provider) $this->assertTrue($bouncer->can('view', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function fails_auth_check_when_bouncer_does_not_allow($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function fails_auth_check_when_bouncer_does_not_allow($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->setUpWithPolicy($bouncer); diff --git a/tests/BouncerSimpleTest.php b/tests/BouncerSimpleTest.php index dd700e9d..cd207d73 100644 --- a/tests/BouncerSimpleTest.php +++ b/tests/BouncerSimpleTest.php @@ -2,21 +2,24 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Exception; use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class BouncerSimpleTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $editSite = Ability::create(['name' => 'edit-site']); $banUsers = Ability::create(['name' => 'ban-users']); @@ -39,13 +42,11 @@ function can_give_and_remove_abilities($provider) } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_abilities_for_everyone($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_abilities_for_everyone($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $editSite = Ability::create(['name' => 'edit-site']); $banUsers = Ability::create(['name' => 'ban-users']); @@ -67,13 +68,11 @@ function can_give_and_remove_abilities_for_everyone($provider) $this->assertTrue($bouncer->cannot('access-dashboard')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_wildcard_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_wildcard_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('*'); @@ -86,13 +85,11 @@ function can_give_and_remove_wildcard_abilities($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_ignore_duplicate_ability_allowances($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_ignore_duplicate_ability_allowances($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('ban-users'); $bouncer->allow($user1)->to('ban-users'); @@ -114,13 +111,11 @@ function can_ignore_duplicate_ability_allowances($provider) $this->assertCount(2, $admin->abilities); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow('admin')->to('ban-users'); $bouncer->assign('admin')->to($user); @@ -137,10 +132,8 @@ function can_give_and_remove_roles($provider) $this->assertTrue($bouncer->cannot('ban-users')); } - /** - * @test - */ - function deleting_a_role_deletes_the_pivot_table_records() + #[Test] + public function deleting_a_role_deletes_the_pivot_table_records() { $bouncer = $this->bouncer(); @@ -157,13 +150,11 @@ function deleting_a_role_deletes_the_pivot_table_records() $this->assertEquals(1, $this->db()->table('permissions')->count()); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_multiple_roles_at_once($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_multiple_roles_at_once($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $admin = $this->role('admin'); $editor = $this->role('editor'); @@ -178,13 +169,11 @@ function can_give_and_remove_multiple_roles_at_once($provider) $this->assertTrue($bouncer->is($user)->notAn($admin, 'editor')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_roles_for_multiple_users_at_once($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_roles_for_multiple_users_at_once($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->assign(['admin', 'editor'])->to([$user1, $user2]); @@ -199,10 +188,8 @@ function can_give_and_remove_roles_for_multiple_users_at_once($provider) $this->assertTrue($bouncer->is($user1)->an('admin', 'editor')); } - /** - * @test - */ - function can_ignore_duplicate_role_assignments() + #[Test] + public function can_ignore_duplicate_role_assignments() { $bouncer = $this->bouncer($user = User::create()); @@ -212,13 +199,11 @@ function can_ignore_duplicate_role_assignments() $this->assertCount(1, $user->roles); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_disallow_abilities_on_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_disallow_abilities_on_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow('admin')->to('edit-site'); $bouncer->disallow('admin')->to('edit-site'); @@ -227,13 +212,11 @@ function can_disallow_abilities_on_roles($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallow_on_roles_does_not_disallow_for_users_with_matching_id($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallow_on_roles_does_not_disallow_for_users_with_matching_id($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); // Since the user is the first user created, its ID is 1. // Creating admin as the first role, it'll have its ID @@ -245,13 +228,11 @@ function disallow_on_roles_does_not_disallow_for_users_with_matching_id($provide $this->assertTrue($bouncer->can('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_user_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_user_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->assertTrue($bouncer->is($user)->notA('moderator')); $this->assertTrue($bouncer->is($user)->notAn('editor')); @@ -268,13 +249,11 @@ function can_check_user_roles($provider) $this->assertFalse($bouncer->is($user)->an('admin')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_multiple_user_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_multiple_user_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->assertTrue($bouncer->is($user)->notAn('editor', 'moderator')); $this->assertTrue($bouncer->is($user)->notAn('admin', 'moderator')); @@ -290,20 +269,16 @@ function can_check_multiple_user_roles($provider) $this->assertFalse($bouncer->is($user)->all('admin', 'moderator')); } - /** - * @test - */ - function can_get_an_empty_role_model() + #[Test] + public function can_get_an_empty_role_model() { $bouncer = $this->bouncer($user = User::create()); $this->assertInstanceOf(Role::class, $bouncer->role()); } - /** - * @test - */ - function can_fill_a_role_model() + #[Test] + public function can_fill_a_role_model() { $bouncer = $this->bouncer($user = User::create()); $role = $bouncer->role(['name' => 'test-role']); @@ -312,20 +287,16 @@ function can_fill_a_role_model() $this->assertEquals('test-role', $role->name); } - /** - * @test - */ - function can_get_an_empty_ability_model() + #[Test] + public function can_get_an_empty_ability_model() { $bouncer = $this->bouncer($user = User::create()); $this->assertInstanceOf(Ability::class, $bouncer->ability()); } - /** - * @test - */ - function can_fill_an_ability_model() + #[Test] + public function can_fill_an_ability_model() { $bouncer = $this->bouncer($user = User::create()); $ability = $bouncer->ability(['name' => 'test-ability']); @@ -334,13 +305,11 @@ function can_fill_an_ability_model() $this->assertEquals('test-ability', $ability->name); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_allow_abilities_from_a_defined_callback($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_allow_abilities_from_a_defined_callback($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->define('edit', function ($user, $account) { if ( ! $account instanceof Account) { @@ -354,13 +323,11 @@ function can_allow_abilities_from_a_defined_callback($provider) $this->assertFalse($bouncer->can('edit', new Account(['user_id' => 99]))); } - /** - * @test - * @dataProvider bouncerProvider - */ - function authorize_method_returns_response_with_correct_message($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function authorize_method_returns_response_with_correct_message($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('have-fun'); $bouncer->allow($user)->to('enjoy-life'); @@ -376,13 +343,11 @@ function authorize_method_returns_response_with_correct_message($provider) ); } - /** - * @test - * @dataProvider bouncerProvider - */ - function authorize_method_throws_for_unauthorized_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function authorize_method_throws_for_unauthorized_abilities($provider) { - list($bouncer) = $provider(); + [$bouncer] = $provider(); // The exception class thrown from the "authorize" method // has changed between different versions of Laravel, diff --git a/tests/CachedClipboardTest.php b/tests/CachedClipboardTest.php index 06219d03..b8f8857e 100644 --- a/tests/CachedClipboardTest.php +++ b/tests/CachedClipboardTest.php @@ -2,25 +2,29 @@ namespace Silber\Bouncer\Tests; -use Silber\Bouncer\CachedClipboard; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; +use Silber\Bouncer\CachedClipboard; +use Silber\Bouncer\Contracts\Clipboard as ClipboardContract; use Illuminate\Cache\ArrayStore; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Collection; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class CachedClipboardTest extends BaseTestCase { - function setUp(): void + /** + * Make a new clipboard with the container. + */ + protected static function makeClipboard(): ClipboardContract { - parent::setUp(); - - $this->clipboard = new CachedClipboard(new ArrayStore); + return new CachedClipboard(new ArrayStore); } - /** - * @test - */ - function it_caches_abilities() + #[Test] + public function it_caches_abilities() { $bouncer = $this->bouncer($user = User::create()); @@ -33,21 +37,17 @@ function it_caches_abilities() $this->assertEquals(['ban-users'], $this->getAbilities($user)); } - /** - * @test - */ - function it_caches_empty_abilities() + #[Test] + public function it_caches_empty_abilities() { $user = User::create(); - $this->assertInstanceOf(Collection::class, $this->clipboard->getAbilities($user)); - $this->assertInstanceOf(Collection::class, $this->clipboard->getAbilities($user)); + $this->assertInstanceOf(Collection::class, $this->clipboard()->getAbilities($user)); + $this->assertInstanceOf(Collection::class, $this->clipboard()->getAbilities($user)); } - /** - * @test - */ - function it_caches_roles() + #[Test] + public function it_caches_roles() { $bouncer = $this->bouncer($user = User::create()); @@ -60,10 +60,8 @@ function it_caches_roles() $this->assertFalse($bouncer->is($user)->a('moderator')); } - /** - * @test - */ - function it_always_checks_roles_in_the_cache() + #[Test] + public function it_always_checks_roles_in_the_cache() { $bouncer = $this->bouncer($user = User::create()); $admin = $bouncer->role()->create(['name' => 'admin']); @@ -83,10 +81,8 @@ function it_always_checks_roles_in_the_cache() $this->db()->connection()->disableQueryLog(); } - /** - * @test - */ - function it_can_refresh_the_cache() + #[Test] + public function it_can_refresh_the_cache() { $cache = new ArrayStore; @@ -108,10 +104,8 @@ function it_can_refresh_the_cache() $this->assertEquals(['create-posts', 'edit-posts'], $this->getAbilities($user)); } - /** - * @test - */ - function it_can_refresh_the_cache_only_for_one_user() + #[Test] + public function it_can_refresh_the_cache_only_for_one_user() { $user1 = User::create(); $user2 = User::create(); diff --git a/tests/CleanCommandTest.php b/tests/CleanCommandTest.php index 71114bae..b5aa9ebc 100644 --- a/tests/CleanCommandTest.php +++ b/tests/CleanCommandTest.php @@ -2,17 +2,30 @@ namespace Silber\Bouncer\Tests; -use Silber\Bouncer\Database\Ability; +use Illuminate\Console\Application as Artisan; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use Silber\Bouncer\Console\CleanCommand; +use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\Account; +use Workbench\App\Models\User; class CleanCommandTest extends BaseTestCase { - use Concerns\TestsConsoleCommands; - /** - * @test + * Setup the world for the tests. */ - function the_orphaned_flag() + public function setUp(): void + { + Artisan::starting( + fn ($artisan) => $artisan->resolveCommands(CleanCommand::class) + ); + + parent::setUp(); + } + + #[Test] + public function the_orphaned_flag() { $bouncer = $this->bouncer($user = User::create())->dontCache(); @@ -21,16 +34,16 @@ function the_orphaned_flag() $this->assertEquals(3, Ability::query()->count()); - $this->clean(['--unassigned' => true], 'Deleted 2 unassigned abilities.'); + $this + ->artisan('bouncer:clean --unassigned') + ->expectsOutput('Deleted 2 unassigned abilities.'); $this->assertEquals(1, Ability::query()->count()); $this->assertTrue($bouncer->can('throw-dishes')); } - /** - * @test - */ - function the_orphaned_flag_with_no_orphaned_abilities() + #[Test] + public function the_orphaned_flag_with_no_orphaned_abilities() { $bouncer = $this->bouncer($user = User::create())->dontCache(); @@ -38,15 +51,15 @@ function the_orphaned_flag_with_no_orphaned_abilities() $this->assertEquals(3, Ability::query()->count()); - $this->clean(['--unassigned' => true], 'No unassigned abilities.'); + $this + ->artisan('bouncer:clean --unassigned') + ->expectsOutput('No unassigned abilities.'); $this->assertEquals(3, Ability::query()->count()); } - /** - * @test - */ - function the_missing_flag() + #[Test] + public function the_missing_flag() { $bouncer = $this->bouncer($user1 = User::create())->dontCache(); @@ -66,7 +79,9 @@ function the_missing_flag() $this->assertEquals(6, Ability::query()->count()); - $this->clean(['--orphaned' => true], 'Deleted 2 orphaned abilities.'); + $this + ->artisan('bouncer:clean --orphaned') + ->expectsOutput('Deleted 2 orphaned abilities.'); $this->assertEquals(4, Ability::query()->count()); $this->assertTrue($bouncer->can('create', Account::class)); @@ -75,10 +90,8 @@ function the_missing_flag() $this->assertTrue($bouncer->can('update', $account2)); } - /** - * @test - */ - function no_flags() + #[Test] + public function no_flags() { $bouncer = $this->bouncer($user1 = User::create())->dontCache(); @@ -96,25 +109,11 @@ function no_flags() $this->assertEquals(4, Ability::query()->count()); - $this->clean([], [ - 'Deleted 1 unassigned ability.', - 'Deleted 1 orphaned ability.' - ]); + $this + ->artisan('bouncer:clean') + ->expectsOutput('Deleted 1 unassigned ability.') + ->expectsOutput('Deleted 1 orphaned ability.'); $this->assertEquals(2, Ability::query()->count()); } - - /** - * Run the clean command, and see the given message in the output. - * - * @param array $parameters - * @param string|array $message - * @return \Prophecy\Prophecy\ObjectProphecy - */ - protected function clean(array $parameters = [], $message) - { - return $this->runCommand( - new CleanCommand, $parameters, $this->predictOutputMessage($message) - ); - } } diff --git a/tests/Concerns/TestsClipboards.php b/tests/Concerns/TestsClipboards.php index a79bb498..69f2a93e 100644 --- a/tests/Concerns/TestsClipboards.php +++ b/tests/Concerns/TestsClipboards.php @@ -2,8 +2,11 @@ namespace Silber\Bouncer\Tests\Concerns; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Clipboard; -use Silber\Bouncer\Tests\User; +use Workbench\App\Models\User; use Silber\Bouncer\CachedClipboard; use Illuminate\Cache\NullStore; @@ -15,19 +18,19 @@ trait TestsClipboards * * @return array */ - function bouncerProvider() + public static function bouncerProvider() { return [ 'basic clipboard' => [ function ($authoriesCount = 1, $authority = User::class) { - return $this->provideBouncer( + return static::provideBouncer( new Clipboard, $authoriesCount, $authority ); } ], 'null cached clipboard' => [ function ($authoriesCount = 1, $authority = User::class) { - return $this->provideBouncer( + return static::provideBouncer( new CachedClipboard(new NullStore), $authoriesCount, $authority ); } @@ -43,15 +46,14 @@ function ($authoriesCount = 1, $authority = User::class) { * @param string $authority * @return array */ - protected function provideBouncer($clipboard, $authoriesCount, $authority) + protected static function provideBouncer($clipboard, $authoriesCount, $authority) { - $authorities = array_map(function () use ($authority) { - return $authority::create(); - }, range(0, $authoriesCount)); - - $this->clipboard = $clipboard; + $authorities = array_map( + fn () => $authority::create(), + range(0, $authoriesCount), + ); - $bouncer = $this->bouncer($authorities[0]); + $bouncer = static::bouncer($authorities[0]); return array_merge([$bouncer], $authorities); } diff --git a/tests/Concerns/TestsConsoleCommands.php b/tests/Concerns/TestsConsoleCommands.php deleted file mode 100644 index 2a8f230f..00000000 --- a/tests/Concerns/TestsConsoleCommands.php +++ /dev/null @@ -1,110 +0,0 @@ -shouldReceive('call')->andReturnUsing(function ($callback) { - list($command, $method) = $callback; - - $command->{$method}(); - }); - - $laravel->shouldReceive('make')->andReturnUsing(function ($class, $arguments) { - if (empty($arguments['input'])) { - return new $class($arguments['output']); - } - - return new $class($arguments['input'], $arguments['output']); - }); - - return $laravel; - } - - /** - * Get a prophesy for the console output class. - * - * @return \Symfony\Component\Console\Output\NullOutput - */ - protected function output() - { - $output = m::mock(NullOutput::class); - - $output->shouldReceive('getVerbosity')->andReturn(NullOutput::VERBOSITY_QUIET); - $output->shouldReceive('getFormatter')->andReturn(new OutputFormatter); - - return $output; - } - - /** - * Predict the output of a command. - * - * @param string|array $message - * @return \Closure - */ - protected function predictOutputMessage($message) - { - $messages = is_array($message) ? $message : [$message]; - - return function ($output) use ($messages) { - foreach ($messages as $message) { - $output - ->shouldReceive('writeln') - ->withArgs(function ($m) use ($message) { - return $m == $message; - }); - } - }; - } - - /** - * Run the given command. - * - * @param \Illuminate\Console\Command $command - * @param \Closure|array|null $parameters - * @param \Closure|null $outputPredictions - * @return mixed - */ - protected function runCommand(Command $command, $parameters = [], $outputPredictions = null) - { - $output = $this->output(); - - if ($parameters instanceof Closure) { - $outputPredictions = $parameters; - - $parameters = []; - } - - if (! is_null($outputPredictions)) { - $outputPredictions($output); - } - - $command->setLaravel($this->laravel()); - - $command->run(new ArrayInput($parameters), $output); - - return $output; - } -} diff --git a/tests/CustomAuthorityTest.php b/tests/CustomAuthorityTest.php index 63b7d417..263e1ca2 100644 --- a/tests/CustomAuthorityTest.php +++ b/tests/CustomAuthorityTest.php @@ -2,19 +2,22 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\HasRolesAndAbilities; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class CustomAuthorityTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_abilities($provider) { - list($bouncer, $account) = $provider(1, Account::class); + [$bouncer, $account] = $provider(1, Account::class); $bouncer->allow($account)->to('edit-site'); @@ -25,13 +28,11 @@ function can_give_and_remove_abilities($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_roles($provider) { - list($bouncer, $account) = $provider(1, Account::class); + [$bouncer, $account] = $provider(1, Account::class); $bouncer->allow('admin')->to('edit-site'); $bouncer->assign('admin')->to($account); @@ -48,13 +49,11 @@ function can_give_and_remove_roles($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_disallow_abilities_on_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_disallow_abilities_on_roles($provider) { - list($bouncer, $account) = $provider(1, Account::class); + [$bouncer, $account] = $provider(1, Account::class); $bouncer->allow('admin')->to('edit-site'); $bouncer->disallow('admin')->to('edit-site'); @@ -63,13 +62,11 @@ function can_disallow_abilities_on_roles($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_roles($provider) { - list($bouncer, $account) = $provider(1, Account::class); + [$bouncer, $account] = $provider(1, Account::class); $this->assertTrue($bouncer->is($account)->notA('moderator')); $this->assertTrue($bouncer->is($account)->notAn('editor')); @@ -86,13 +83,11 @@ function can_check_roles($provider) $this->assertFalse($bouncer->is($account)->an('admin')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_multiple_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_multiple_roles($provider) { - list($bouncer, $account) = $provider(1, Account::class); + [$bouncer, $account] = $provider(1, Account::class); $this->assertTrue($bouncer->is($account)->notAn('editor', 'moderator')); $this->assertTrue($bouncer->is($account)->notAn('admin', 'moderator')); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index bcecf8c3..09b7e6c5 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -2,16 +2,19 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Bouncer; use Illuminate\Auth\Access\Gate; use Illuminate\Container\Container; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class FactoryTest extends BaseTestCase { - /** - * @test - */ - function can_create_default_bouncer_instance() + #[Test] + public function can_create_default_bouncer_instance() { $bouncer = Bouncer::create(); @@ -20,10 +23,8 @@ function can_create_default_bouncer_instance() $this->assertTrue($bouncer->usesCachedClipboard()); } - /** - * @test - */ - function can_create_bouncer_instance_for_given_the_user() + #[Test] + public function can_create_bouncer_instance_for_given_the_user() { $bouncer = Bouncer::create($user = User::create()); @@ -33,10 +34,8 @@ function can_create_bouncer_instance_for_given_the_user() $this->assertTrue($bouncer->cannot('delete-bouncers')); } - /** - * @test - */ - function can_build_up_bouncer_with_the_given_user() + #[Test] + public function can_build_up_bouncer_with_the_given_user() { $bouncer = Bouncer::make()->withUser($user = User::create())->create(); @@ -46,10 +45,8 @@ function can_build_up_bouncer_with_the_given_user() $this->assertTrue($bouncer->cannot('delete-bouncers')); } - /** - * @test - */ - function can_build_up_bouncer_with_the_given_gate() + #[Test] + public function can_build_up_bouncer_with_the_given_gate() { $user = User::create(); diff --git a/tests/ForbidTest.php b/tests/ForbidTest.php index a196ed15..b0786e73 100644 --- a/tests/ForbidTest.php +++ b/tests/ForbidTest.php @@ -2,20 +2,23 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; class ForbidTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function an_allowed_simple_ability_is_not_granted_when_forbidden($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function an_allowed_simple_ability_is_not_granted_when_forbidden($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('edit-site'); $bouncer->forbid($user)->to('edit-site'); @@ -27,13 +30,11 @@ function an_allowed_simple_ability_is_not_granted_when_forbidden($provider) $this->assertTrue($bouncer->can('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function an_allowed_model_ability_is_not_granted_when_forbidden($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function an_allowed_model_ability_is_not_granted_when_forbidden($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete', $user); $bouncer->forbid($user)->to('delete', $user); @@ -45,13 +46,11 @@ function an_allowed_model_ability_is_not_granted_when_forbidden($provider) $this->assertTrue($bouncer->can('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function an_allowed_model_class_ability_is_not_granted_when_forbidden($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function an_allowed_model_class_ability_is_not_granted_when_forbidden($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete', User::class); $bouncer->forbid($user)->to('delete', User::class); @@ -63,13 +62,11 @@ function an_allowed_model_class_ability_is_not_granted_when_forbidden($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_a_single_model_forbids_even_with_allowed_model_class_ability($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_a_single_model_forbids_even_with_allowed_model_class_ability($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete', User::class); $bouncer->forbid($user)->to('delete', $user); @@ -81,13 +78,11 @@ function forbidding_a_single_model_forbids_even_with_allowed_model_class_ability $this->assertTrue($bouncer->can('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_a_single_model_does_not_forbid_other_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_a_single_model_does_not_forbid_other_models($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('delete', User::class); $bouncer->forbid($user1)->to('delete', $user2); @@ -95,13 +90,11 @@ function forbidding_a_single_model_does_not_forbid_other_models($provider) $this->assertTrue($bouncer->can('delete', $user1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_a_model_class_forbids_individual_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_a_model_class_forbids_individual_models($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete', $user); $bouncer->forbid($user)->to('delete', User::class); @@ -113,13 +106,11 @@ function forbidding_a_model_class_forbids_individual_models($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_an_ability_through_a_role($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_an_ability_through_a_role($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->forbid('admin')->to('delete', User::class); $bouncer->allow($user)->to('delete', User::class); @@ -139,13 +130,11 @@ function forbidding_an_ability_through_a_role($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_an_ability_allowed_through_a_role($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_an_ability_allowed_through_a_role($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow('admin')->to('delete', User::class); $bouncer->forbid($user)->to('delete', User::class); @@ -155,13 +144,11 @@ function forbidding_an_ability_allowed_through_a_role($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_an_ability_when_everything_is_allowed($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_an_ability_when_everything_is_allowed($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->everything(); $bouncer->forbid($user)->toManage(User::class); @@ -170,13 +157,11 @@ function forbidding_an_ability_when_everything_is_allowed($provider) $this->assertTrue($bouncer->cannot('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbid_an_ability_on_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbid_an_ability_on_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->everything(); $bouncer->forbid($user)->to('delete')->everything(); @@ -186,13 +171,11 @@ function forbid_an_ability_on_everything($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_and_unforbidding_an_ability_for_everyone($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_and_unforbidding_an_ability_for_everyone($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->everything(); $bouncer->forbidEveryone()->to('delete', Account::class); @@ -205,13 +188,11 @@ function forbidding_and_unforbidding_an_ability_for_everyone($provider) $this->assertTrue($bouncer->can('delete', Account::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_an_ability_stops_all_further_checks($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_an_ability_stops_all_further_checks($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->define('sleep', function () { return true; diff --git a/tests/HasRolesAndAbilitiesTraitTest.php b/tests/HasRolesAndAbilitiesTraitTest.php index 9aba6060..72193a5b 100644 --- a/tests/HasRolesAndAbilitiesTraitTest.php +++ b/tests/HasRolesAndAbilitiesTraitTest.php @@ -2,19 +2,23 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Models; +use Workbench\App\Models\User; +use Workbench\App\Models\UserWithSoftDeletes; +use Workbench\App\Models\Account; class HasRolesAndAbilitiesTraitTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function get_abilities_gets_all_allowed_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function get_abilities_gets_all_allowed_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow('admin')->to('edit-site'); $bouncer->allow($user)->to('create-posts'); @@ -29,13 +33,11 @@ function get_abilities_gets_all_allowed_abilities($provider) ); } - /** - * @test - * @dataProvider bouncerProvider - */ - function get_forbidden_abilities_gets_all_forbidden_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function get_forbidden_abilities_gets_all_forbidden_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->forbid('admin')->to('edit-site'); $bouncer->forbid($user)->to('create-posts'); @@ -50,13 +52,11 @@ function get_forbidden_abilities_gets_all_forbidden_abilities($provider) ); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow('edit-site'); @@ -67,13 +67,11 @@ function can_give_and_remove_abilities($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow('delete', $user); @@ -86,13 +84,11 @@ function can_give_and_remove_model_abilities($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_give_and_remove_ability_for_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_give_and_remove_ability_for_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow()->everything(); @@ -107,13 +103,11 @@ function can_give_and_remove_ability_for_everything($provider) $this->assertTrue($bouncer->cannot('*', '*')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_forbid_and_unforbid_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_forbid_and_unforbid_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow('edit-site'); $user->forbid('edit-site'); @@ -125,13 +119,11 @@ function can_forbid_and_unforbid_abilities($provider) $this->assertTrue($bouncer->can('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_forbid_and_unforbid_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_forbid_and_unforbid_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow('delete', $user); $user->forbid('delete', $user); @@ -143,13 +135,11 @@ function can_forbid_and_unforbid_model_abilities($provider) $this->assertTrue($bouncer->can('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_forbid_and_unforbid_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_forbid_and_unforbid_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $user->allow('delete', $user); $user->forbid()->everything(); @@ -161,13 +151,11 @@ function can_forbid_and_unforbid_everything($provider) $this->assertTrue($bouncer->can('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_assign_and_retract_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_assign_and_retract_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow('admin')->to('edit-site'); $user->assign('admin'); @@ -181,13 +169,11 @@ function can_assign_and_retract_roles($provider) $this->assertTrue($bouncer->cannot('edit-site')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->assertTrue($user->isNotAn('admin')); $this->assertFalse($user->isAn('admin')); @@ -203,13 +189,11 @@ function can_check_roles($provider) $this->assertTrue($user->isNotAn('editor')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_check_multiple_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_check_multiple_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $this->assertFalse($user->isAn('admin', 'editor')); @@ -221,10 +205,8 @@ function can_check_multiple_roles($provider) $this->assertFalse($user->isAll('moderator', 'admin')); } - /** - * @test - */ - function deleting_a_model_deletes_the_permissions_pivot_table_records() + #[Test] + public function deleting_a_model_deletes_the_permissions_pivot_table_records() { $bouncer = $this->bouncer(); @@ -241,10 +223,8 @@ function deleting_a_model_deletes_the_permissions_pivot_table_records() $this->assertEquals(1, $this->db()->table('permissions')->count()); } - /** - * @test - */ - function soft_deleting_a_model_persists_the_permissions_pivot_table_records() + #[Test] + public function soft_deleting_a_model_persists_the_permissions_pivot_table_records() { Models::setUsersModel(UserWithSoftDeletes::class); @@ -263,10 +243,8 @@ function soft_deleting_a_model_persists_the_permissions_pivot_table_records() $this->assertEquals(2, $this->db()->table('permissions')->count()); } - /** - * @test - */ - function deleting_a_model_deletes_the_assigned_roles_pivot_table_records() + #[Test] + public function deleting_a_model_deletes_the_assigned_roles_pivot_table_records() { $bouncer = $this->bouncer(); @@ -283,10 +261,8 @@ function deleting_a_model_deletes_the_assigned_roles_pivot_table_records() $this->assertEquals(1, $this->db()->table('assigned_roles')->count()); } - /** - * @test - */ - function soft_deleting_a_model_persists_the_assigned_roles_pivot_table_records() + #[Test] + public function soft_deleting_a_model_persists_the_assigned_roles_pivot_table_records() { Models::setUsersModel(UserWithSoftDeletes::class); diff --git a/tests/MultiTenancyTest.php b/tests/MultiTenancyTest.php index 18de1e7b..cb09baec 100644 --- a/tests/MultiTenancyTest.php +++ b/tests/MultiTenancyTest.php @@ -2,6 +2,9 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Models; use Silber\Bouncer\Database\Ability; @@ -12,6 +15,9 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class MultiTenancyTest extends BaseTestCase { use Concerns\TestsClipboards; @@ -28,10 +34,8 @@ function tearDown(): void parent::tearDown(); } - /** - * @test - */ - function can_set_and_get_the_current_scope() + #[Test] + public function can_set_and_get_the_current_scope() { $bouncer = $this->bouncer(); @@ -41,10 +45,8 @@ function can_set_and_get_the_current_scope() $this->assertEquals(1, $bouncer->scope()->get()); } - /** - * @test - */ - function can_remove_the_current_scope() + #[Test] + public function can_remove_the_current_scope() { $bouncer = $this->bouncer(); @@ -55,13 +57,11 @@ function can_remove_the_current_scope() $this->assertNull($bouncer->scope()->get()); } - /** - * @test - * @dataProvider bouncerProvider - */ - function creating_roles_and_abilities_automatically_scopes_them($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function creating_roles_and_abilities_automatically_scopes_them($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); @@ -74,13 +74,11 @@ function creating_roles_and_abilities_automatically_scopes_them($provider) $this->assertEquals(1, $this->db()->table('assigned_roles')->value('scope')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_roles_is_properly_scoped($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_roles_is_properly_scoped($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); $bouncer->assign(['writer', 'reader'])->to($user); @@ -104,13 +102,11 @@ function syncing_roles_is_properly_scoped($provider) $this->assertEquals(1, $user->roles()->count()); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_abilities_is_properly_scoped($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_abilities_is_properly_scoped($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); $bouncer->allow($user)->to(['write', 'read']); @@ -136,13 +132,11 @@ function syncing_abilities_is_properly_scoped($provider) $this->assertEquals(1, $user->abilities()->count()); } - /** - * @test - * @dataProvider bouncerProvider - */ - function scoped_abilities_do_not_work_when_unscoped($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function scoped_abilities_do_not_work_when_unscoped($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); $bouncer->allow($user)->to(['write', 'read']); @@ -156,13 +150,11 @@ function scoped_abilities_do_not_work_when_unscoped($provider) $this->assertFalse($bouncer->can('read')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function relation_queries_are_properly_scoped($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function relation_queries_are_properly_scoped($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); $bouncer->allow($user)->to('create', User::class); @@ -189,13 +181,11 @@ function relation_queries_are_properly_scoped($provider) $this->assertTrue($bouncer->cannot('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function relation_queries_can_be_scoped_exclusively($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function relation_queries_can_be_scoped_exclusively($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1)->onlyRelations(); $bouncer->allow($user)->to('create', User::class); @@ -222,13 +212,11 @@ function relation_queries_can_be_scoped_exclusively($provider) $this->assertTrue($bouncer->cannot('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function scoping_also_returns_global_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function scoping_also_returns_global_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('create', User::class); @@ -244,13 +232,11 @@ function scoping_also_returns_global_abilities($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_abilities_only_affects_the_current_scope($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_abilities_only_affects_the_current_scope($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1); $bouncer->allow($user)->to('create', User::class); @@ -270,13 +256,11 @@ function forbidding_abilities_only_affects_the_current_scope($provider) $this->assertTrue($bouncer->cannot('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallowing_abilities_only_affects_the_current_scope($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallowing_abilities_only_affects_the_current_scope($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $admin = $bouncer->role()->create(['name' => 'admin']); $user->assign($admin); @@ -297,13 +281,11 @@ function disallowing_abilities_only_affects_the_current_scope($provider) $this->assertTrue($bouncer->cannot('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function unforbidding_abilities_only_affects_the_current_scope($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function unforbidding_abilities_only_affects_the_current_scope($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $admin = $bouncer->role()->create(['name' => 'admin']); $user->assign($admin); @@ -326,13 +308,11 @@ function unforbidding_abilities_only_affects_the_current_scope($provider) $this->assertTrue($bouncer->can('create', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function assigning_and_retracting_roles_scopes_them_properly($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function assigning_and_retracting_roles_scopes_them_properly($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1)->onlyRelations(); $bouncer->assign('admin')->to($user); @@ -351,13 +331,11 @@ function assigning_and_retracting_roles_scopes_them_properly($provider) $this->assertFalse($bouncer->is($user)->an('admin')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function role_abilities_can_be_excluded_from_scopes($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function role_abilities_can_be_excluded_from_scopes($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope()->to(1) ->onlyRelations() @@ -372,13 +350,11 @@ function role_abilities_can_be_excluded_from_scopes($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_set_custom_scope($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_set_custom_scope($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->scope(new MultiTenancyNullScopeStub)->to(1); @@ -389,10 +365,8 @@ function can_set_custom_scope($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - */ - function can_set_the_scope_temporarily() + #[Test] + public function can_set_the_scope_temporarily() { $bouncer = $this->bouncer(); @@ -408,10 +382,8 @@ function can_set_the_scope_temporarily() $this->assertNull($bouncer->scope()->get()); } - /** - * @test - */ - function can_remove_the_scope_temporarily() + #[Test] + public function can_remove_the_scope_temporarily() { $bouncer = $this->bouncer(); diff --git a/tests/MultipleAbilitiesTest.php b/tests/MultipleAbilitiesTest.php index f958d42f..d5c35df1 100644 --- a/tests/MultipleAbilitiesTest.php +++ b/tests/MultipleAbilitiesTest.php @@ -2,17 +2,21 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class MultipleAbilitiesTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_multiple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_multiple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete']); @@ -20,13 +24,11 @@ function allowing_multiple_abilities($provider) $this->assertTrue($bouncer->can('delete')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_multiple_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_multiple_model_abilities($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to(['edit', 'delete'], $user1); @@ -36,13 +38,11 @@ function allowing_multiple_model_abilities($provider) $this->assertTrue($bouncer->cannot('delete', $user2)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_multiple_blanket_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_multiple_blanket_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete'], User::class); @@ -50,13 +50,11 @@ function allowing_multiple_blanket_model_abilities($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_an_ability_on_multiple_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_an_ability_on_multiple_models($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to('delete', [Account::class, $user1]); @@ -66,13 +64,11 @@ function allowing_an_ability_on_multiple_models($provider) $this->assertTrue($bouncer->cannot('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_multiple_abilities_on_multiple_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_multiple_abilities_on_multiple_models($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to(['update', 'delete'], [Account::class, $user1]); @@ -83,13 +79,11 @@ function allowing_multiple_abilities_on_multiple_models($provider) $this->assertTrue($bouncer->cannot('update', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function allowing_multiple_abilities_via_a_map($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function allowing_multiple_abilities_via_a_map($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $account1 = Account::create(); $account2 = Account::create(); @@ -115,13 +109,11 @@ function allowing_multiple_abilities_via_a_map($provider) $this->assertTrue($bouncer->can('access-dashboard')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallowing_multiple_abilties($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallowing_multiple_abilties($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete']); $bouncer->disallow($user)->to(['edit', 'delete']); @@ -130,13 +122,11 @@ function disallowing_multiple_abilties($provider) $this->assertTrue($bouncer->cannot('delete')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallowing_multiple_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallowing_multiple_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['view', 'edit', 'delete'], $user); $bouncer->disallow($user)->to(['edit', 'delete'], $user); @@ -146,13 +136,11 @@ function disallowing_multiple_model_abilities($provider) $this->assertTrue($bouncer->cannot('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallowing_multiple_blanket_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallowing_multiple_blanket_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete'], User::class); $bouncer->disallow($user)->to(['edit', 'delete'], User::class); @@ -161,13 +149,11 @@ function disallowing_multiple_blanket_model_abilities($provider) $this->assertTrue($bouncer->cannot('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function disallowing_multiple_abilities_via_a_map($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function disallowing_multiple_abilities_via_a_map($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $account1 = Account::create(); $account2 = Account::create(); @@ -190,13 +176,11 @@ function disallowing_multiple_abilities_via_a_map($provider) $this->assertTrue($bouncer->cannot('update', $account1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_multiple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_multiple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete']); $bouncer->forbid($user)->to(['edit', 'delete']); @@ -205,13 +189,11 @@ function forbidding_multiple_abilities($provider) $this->assertTrue($bouncer->cannot('delete')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_multiple_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_multiple_model_abilities($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $bouncer->allow($user1)->to(['view', 'edit', 'delete']); $bouncer->allow($user1)->to(['view', 'edit', 'delete'], $user1); @@ -228,13 +210,11 @@ function forbidding_multiple_model_abilities($provider) $this->assertTrue($bouncer->can('delete', $user2)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_multiple_blanket_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_multiple_blanket_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['edit', 'delete']); $bouncer->allow($user)->to(['edit', 'delete'], Account::class); @@ -252,13 +232,11 @@ function forbidding_multiple_blanket_model_abilities($provider) $this->assertTrue($bouncer->cannot('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function forbidding_multiple_abilities_via_a_map($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function forbidding_multiple_abilities_via_a_map($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $account1 = Account::create(); $account2 = Account::create(); @@ -281,13 +259,11 @@ function forbidding_multiple_abilities_via_a_map($provider) $this->assertTrue($bouncer->cannot('update', $account1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function unforbidding_multiple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function unforbidding_multiple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['view', 'edit', 'delete']); $bouncer->forbid($user)->to(['view', 'edit', 'delete']); @@ -298,13 +274,11 @@ function unforbidding_multiple_abilities($provider) $this->assertTrue($bouncer->can('delete')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function unforbidding_multiple_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function unforbidding_multiple_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['view', 'edit', 'delete'], $user); $bouncer->forbid($user)->to(['view', 'edit', 'delete'], $user); @@ -315,13 +289,11 @@ function unforbidding_multiple_model_abilities($provider) $this->assertTrue($bouncer->can('delete', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function unforbidding_multiple_blanket_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function unforbidding_multiple_blanket_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to(['view', 'edit', 'delete'], User::class); $bouncer->forbid($user)->to(['view', 'edit', 'delete'], User::class); @@ -332,13 +304,11 @@ function unforbidding_multiple_blanket_model_abilities($provider) $this->assertTrue($bouncer->can('delete', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function unforbidding_multiple_abilities_via_a_map($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function unforbidding_multiple_abilities_via_a_map($provider) { - list($bouncer, $user1, $user2) = $provider(2); + [$bouncer, $user1, $user2] = $provider(2); $account1 = Account::create(); $account2 = Account::create(); @@ -368,13 +338,11 @@ function unforbidding_multiple_abilities_via_a_map($provider) $this->assertTrue($bouncer->can('update', $account1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function passthru_can_any_check($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function passthru_can_any_check($provider) { - list($bouncer, $user1) = $provider(); + [$bouncer, $user1] = $provider(); $user1->allow('create', User::class); diff --git a/tests/OwnershipTest.php b/tests/OwnershipTest.php index b64bb4f8..efc2521a 100644 --- a/tests/OwnershipTest.php +++ b/tests/OwnershipTest.php @@ -2,21 +2,25 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Models; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class OwnershipTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function can_own_a_model_class($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_own_a_model_class($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toOwn(Account::class); @@ -39,13 +43,11 @@ function can_own_a_model_class($provider) $this->assertTrue($bouncer->cannot('update', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_own_a_model($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_own_a_model($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $account1 = Account::create(['user_id' => $user->id]); $account2 = Account::create(['user_id' => $user->id]); @@ -70,13 +72,11 @@ function can_own_a_model($provider) $this->assertTrue($bouncer->cannot('update', $account1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_own_a_model_for_a_given_ability($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_own_a_model_for_a_given_ability($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $account1 = Account::create(['user_id' => $user->id]); $account2 = Account::create(['user_id' => $user->id]); @@ -105,13 +105,11 @@ function can_own_a_model_for_a_given_ability($provider) $this->assertTrue($bouncer->cannot('update', $account1)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_own_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_own_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toOwnEverything(); @@ -131,13 +129,11 @@ function can_own_everything($provider) $this->assertTrue($bouncer->cannot('delete', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_own_everything_for_a_given_ability($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_own_everything_for_a_given_ability($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toOwnEverything()->to('view'); @@ -158,13 +154,11 @@ function can_own_everything_for_a_given_ability($provider) $this->assertTrue($bouncer->cannot('view', $account)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_use_custom_ownership_attribute($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_use_custom_ownership_attribute($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->ownedVia('userId'); @@ -177,13 +171,11 @@ function can_use_custom_ownership_attribute($provider) Models::reset(); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_use_custom_ownership_attribute_for_model_type($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_use_custom_ownership_attribute_for_model_type($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->ownedVia(Account::class, 'userId'); @@ -196,13 +188,11 @@ function can_use_custom_ownership_attribute_for_model_type($provider) Models::reset(); } - /** - * @test - * @dataProvider bouncerProvider - */ - function can_forbid_abilities_after_owning_a_model_class($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function can_forbid_abilities_after_owning_a_model_class($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toOwn(Account::class); $bouncer->forbid($user)->to('publish', Account::class); diff --git a/tests/QueryScopes/RoleScopesTest.php b/tests/QueryScopes/RoleScopesTest.php index 5dbb3c26..19590026 100644 --- a/tests/QueryScopes/RoleScopesTest.php +++ b/tests/QueryScopes/RoleScopesTest.php @@ -2,16 +2,17 @@ namespace Silber\Bouncer\Tests\QueryScopes; -use Silber\Bouncer\Tests\User; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; use Silber\Bouncer\Database\Role; use Silber\Bouncer\Tests\BaseTestCase; class RoleScopesTest extends BaseTestCase { - /** - * @test - */ - function roles_can_be_constrained_by_a_user() + #[Test] + public function roles_can_be_constrained_by_a_user() { $bouncer = $this->bouncer($user = User::create()); @@ -32,10 +33,8 @@ function roles_can_be_constrained_by_a_user() $this->assertFalse($roles->contains('name', 'subscriber')); } - /** - * @test - */ - function roles_can_be_constrained_by_a_collection_of_users() + #[Test] + public function roles_can_be_constrained_by_a_collection_of_users() { $user1 = User::create(); $user2 = User::create(); @@ -60,10 +59,8 @@ function roles_can_be_constrained_by_a_collection_of_users() $this->assertFalse($roles->contains('name', 'admin')); } - /** - * @test - */ - function roles_can_be_constrained_by_a_model_name_and_keys() + #[Test] + public function roles_can_be_constrained_by_a_model_name_and_keys() { $user1 = User::create(); $user2 = User::create(); diff --git a/tests/QueryScopes/UserIsScopesTest.php b/tests/QueryScopes/UserIsScopesTest.php index 2bcb909a..3cfa95d0 100644 --- a/tests/QueryScopes/UserIsScopesTest.php +++ b/tests/QueryScopes/UserIsScopesTest.php @@ -2,16 +2,17 @@ namespace Silber\Bouncer\Tests\QueryScopes; -use Silber\Bouncer\Tests\User; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; use Silber\Bouncer\Database\Role; use Silber\Bouncer\Tests\BaseTestCase; class UserIsScopesTest extends BaseTestCase { - /** - * @test - */ - function users_can_be_constrained_to_having_a_role() + #[Test] + public function users_can_be_constrained_to_having_a_role() { $user1 = User::create(['name' => 'Joseph']); $user2 = User::create(['name' => 'Silber']); @@ -25,10 +26,8 @@ function users_can_be_constrained_to_having_a_role() $this->assertEquals('Joseph', $users->first()->name); } - /** - * @test - */ - function users_can_be_constrained_to_having_one_of_many_roles() + #[Test] + public function users_can_be_constrained_to_having_one_of_many_roles() { $user1 = User::create(['name' => 'Joseph']); $user2 = User::create(['name' => 'Silber']); @@ -42,10 +41,8 @@ function users_can_be_constrained_to_having_one_of_many_roles() $this->assertEquals('Silber', $users->first()->name); } - /** - * @test - */ - function users_can_be_constrained_to_having_all_provided_roles() + #[Test] + public function users_can_be_constrained_to_having_all_provided_roles() { $user1 = User::create(['name' => 'Joseph']); $user2 = User::create(['name' => 'Silber']); @@ -59,10 +56,8 @@ function users_can_be_constrained_to_having_all_provided_roles() $this->assertEquals('Joseph', $users->first()->name); } - /** - * @test - */ - function users_can_be_constrained_to_not_having_a_role() + #[Test] + public function users_can_be_constrained_to_not_having_a_role() { $user1 = User::create(); $user2 = User::create(); @@ -80,10 +75,8 @@ function users_can_be_constrained_to_not_having_a_role() $this->assertTrue($users->contains($user3)); } - /** - * @test - */ - function users_can_be_constrained_to_not_having_any_of_the_given_roles() + #[Test] + public function users_can_be_constrained_to_not_having_any_of_the_given_roles() { $user1 = User::create(); $user2 = User::create(); diff --git a/tests/ReportedIssuesTest.php b/tests/ReportedIssuesTest.php index e99a06c9..8cd90545 100644 --- a/tests/ReportedIssuesTest.php +++ b/tests/ReportedIssuesTest.php @@ -2,21 +2,27 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class ReportedIssuesTest extends BaseTestCase { use Concerns\TestsClipboards; /** - * @test - * @dataProvider bouncerProvider * @see https://github.com/JosephSilber/bouncer/pull/589 */ + #[Test] + #[DataProvider('bouncerProvider')] function forbid_an_ability_on_everything_with_zero_id($provider) { - list($bouncer, $user1, $user2, $user3) = $provider(3); + [$bouncer, $user1, $user2, $user3] = $provider(3); $user2->setAttribute($user2->getKeyName(), 0); diff --git a/tests/SyncTest.php b/tests/SyncTest.php index b230e7e8..8c7ecb8c 100644 --- a/tests/SyncTest.php +++ b/tests/SyncTest.php @@ -2,20 +2,24 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class SyncTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_roles($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_roles($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $admin = $this->role('admin'); $editor = $this->role('editor'); @@ -32,13 +36,11 @@ function syncing_roles($provider) $this->assertTrue($bouncer->is($user)->notAn($admin)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $editSite = Ability::create(['name' => 'edit-site']); $banUsers = Ability::create(['name' => 'ban-users']); @@ -57,13 +59,11 @@ function syncing_abilities($provider) $this->assertTrue($bouncer->can('access-dashboard')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_abilities_with_a_map($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_abilities_with_a_map($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $deleteUser = Ability::createForModel($user, 'delete'); $createAccounts = Ability::createForModel(Account::class, 'create'); @@ -86,13 +86,11 @@ function syncing_abilities_with_a_map($provider) $this->assertTrue($bouncer->can('access-dashboard')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_forbidden_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_forbidden_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $editSite = Ability::create(['name' => 'edit-site']); $banUsers = Ability::create(['name' => 'ban-users']); @@ -112,13 +110,11 @@ function syncing_forbidden_abilities($provider) $this->assertTrue($bouncer->cannot('access-dashboard')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function syncing_a_roles_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function syncing_a_roles_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $editSite = Ability::create(['name' => 'edit-site']); $banUsers = Ability::create(['name' => 'ban-users']); @@ -138,10 +134,8 @@ function syncing_a_roles_abilities($provider) $this->assertTrue($bouncer->can('access-dashboard')); } - /** - * @test - */ - function syncing_user_abilities_does_not_alter_role_abilities_with_same_id() + #[Test] + public function syncing_user_abilities_does_not_alter_role_abilities_with_same_id() { $user = User::create(['id' => 1]); $bouncer = $this->bouncer($user); @@ -157,10 +151,8 @@ function syncing_user_abilities_does_not_alter_role_abilities_with_same_id() $this->assertTrue($role->can('drink')); } - /** - * @test - */ - function syncing_abilities_does_not_affect_another_entity_type_with_same_id() + #[Test] + public function syncing_abilities_does_not_affect_another_entity_type_with_same_id() { $user = User::create(['id' => 1]); $account = Account::create(['id' => 1]); @@ -179,10 +171,8 @@ function syncing_abilities_does_not_affect_another_entity_type_with_same_id() $this->assertTrue($account->can('relax')); } - /** - * @test - */ - function syncing_roles_does_not_affect_another_entity_type_with_same_id() + #[Test] + public function syncing_roles_does_not_affect_another_entity_type_with_same_id() { $user = User::create(['id' => 1]); $account = Account::create(['id' => 1]); diff --git a/tests/TablePrefixTest.php b/tests/TablePrefixTest.php index ee31a656..fe0d21a8 100644 --- a/tests/TablePrefixTest.php +++ b/tests/TablePrefixTest.php @@ -2,6 +2,12 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class TablePrefixTest extends BaseTestCase { use Concerns\TestsClipboards; @@ -13,26 +19,22 @@ protected function registerDatabaseContainerBindings() $this->db()->connection()->setTablePrefix('bouncer_'); } - /** - * @test - * @dataProvider bouncerProvider - */ - function test_ability_queries_work_with_prefix($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function test_ability_queries_work_with_prefix($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->everything(); $this->assertTrue($bouncer->can('do-something')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function test_role_queries_work_with_prefix($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function test_role_queries_work_with_prefix($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->assign('artisan')->to($user); diff --git a/tests/TitledAbilitiesTest.php b/tests/TitledAbilitiesTest.php index a0a9c527..d1f2b737 100644 --- a/tests/TitledAbilitiesTest.php +++ b/tests/TitledAbilitiesTest.php @@ -2,14 +2,18 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class TitledAbilitiesTest extends BaseTestCase { - /** - * @test - */ - function allowing_simple_ability() + #[Test] + public function allowing_simple_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -20,10 +24,8 @@ function allowing_simple_ability() $this->seeTitledAbility('Dashboard administration'); } - /** - * @test - */ - function allowing_model_class_ability() + #[Test] + public function allowing_model_class_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -34,10 +36,8 @@ function allowing_model_class_ability() $this->seeTitledAbility('Create users'); } - /** - * @test - */ - function allowing_model_ability() + #[Test] + public function allowing_model_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -48,10 +48,8 @@ function allowing_model_ability() $this->seeTitledAbility('Delete user #1'); } - /** - * @test - */ - function allowing_everything() + #[Test] + public function allowing_everything() { $bouncer = $this->bouncer($user = User::create()); @@ -62,10 +60,8 @@ function allowing_everything() $this->seeTitledAbility('Omnipotent'); } - /** - * @test - */ - function allowing_to_manage_a_model_class() + #[Test] + public function allowing_to_manage_a_model_class() { $bouncer = $this->bouncer($user = User::create()); @@ -76,10 +72,8 @@ function allowing_to_manage_a_model_class() $this->seeTitledAbility('Manage users'); } - /** - * @test - */ - function allowing_to_manage_a_model() + #[Test] + public function allowing_to_manage_a_model() { $bouncer = $this->bouncer($user = User::create()); @@ -90,10 +84,8 @@ function allowing_to_manage_a_model() $this->seeTitledAbility('Manage user #1'); } - /** - * @test - */ - function allowing_an_ability_on_everything() + #[Test] + public function allowing_an_ability_on_everything() { $bouncer = $this->bouncer($user = User::create()); @@ -104,10 +96,8 @@ function allowing_an_ability_on_everything() $this->seeTitledAbility('Create anything'); } - /** - * @test - */ - function allowing_to_own_a_model_class() + #[Test] + public function allowing_to_own_a_model_class() { $bouncer = $this->bouncer($user = User::create()); @@ -118,10 +108,8 @@ function allowing_to_own_a_model_class() $this->seeTitledAbility('Manage onwed account'); } - /** - * @test - */ - function allowing_to_own_a_model() + #[Test] + public function allowing_to_own_a_model() { $bouncer = $this->bouncer($user = User::create()); @@ -132,10 +120,8 @@ function allowing_to_own_a_model() $this->seeTitledAbility('Manage user #1 when owned'); } - /** - * @test - */ - function allowing_to_own_everything() + #[Test] + public function allowing_to_own_everything() { $bouncer = $this->bouncer($user = User::create()); @@ -146,10 +132,8 @@ function allowing_to_own_everything() $this->seeTitledAbility('Manage anything onwed'); } - /** - * @test - */ - function forbidding_simple_ability() + #[Test] + public function forbidding_simple_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -160,10 +144,8 @@ function forbidding_simple_ability() $this->seeTitledAbility('Dashboard administration'); } - /** - * @test - */ - function forbidding_model_class_ability() + #[Test] + public function forbidding_model_class_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -174,10 +156,8 @@ function forbidding_model_class_ability() $this->seeTitledAbility('Create users'); } - /** - * @test - */ - function forbidding_model_ability() + #[Test] + public function forbidding_model_ability() { $bouncer = $this->bouncer($user = User::create()); @@ -188,10 +168,8 @@ function forbidding_model_ability() $this->seeTitledAbility('Delete user #1'); } - /** - * @test - */ - function forbidding_everything() + #[Test] + public function forbidding_everything() { $bouncer = $this->bouncer($user = User::create()); @@ -202,10 +180,8 @@ function forbidding_everything() $this->seeTitledAbility('Omnipotent'); } - /** - * @test - */ - function forbidding_to_manage_a_model_class() + #[Test] + public function forbidding_to_manage_a_model_class() { $bouncer = $this->bouncer($user = User::create()); @@ -216,10 +192,8 @@ function forbidding_to_manage_a_model_class() $this->seeTitledAbility('Manage users'); } - /** - * @test - */ - function forbidding_to_manage_a_model() + #[Test] + public function forbidding_to_manage_a_model() { $bouncer = $this->bouncer($user = User::create()); @@ -230,10 +204,8 @@ function forbidding_to_manage_a_model() $this->seeTitledAbility('Manage user #1'); } - /** - * @test - */ - function forbidding_an_ability_on_everything() + #[Test] + public function forbidding_an_ability_on_everything() { $bouncer = $this->bouncer($user = User::create()); @@ -244,10 +216,8 @@ function forbidding_an_ability_on_everything() $this->seeTitledAbility('Create anything'); } - /** - * @test - */ - function forbidding_to_own_a_model_class() + #[Test] + public function forbidding_to_own_a_model_class() { $bouncer = $this->bouncer($user = User::create()); @@ -258,10 +228,8 @@ function forbidding_to_own_a_model_class() $this->seeTitledAbility('Manage onwed account'); } - /** - * @test - */ - function forbidding_to_own_a_model() + #[Test] + public function forbidding_to_own_a_model() { $bouncer = $this->bouncer($user = User::create()); @@ -272,10 +240,8 @@ function forbidding_to_own_a_model() $this->seeTitledAbility('Manage user #1 when owned'); } - /** - * @test - */ - function forbidding_to_own_everything() + #[Test] + public function forbidding_to_own_everything() { $bouncer = $this->bouncer($user = User::create()); diff --git a/tests/Unit/Constraints/BuilderTest.php b/tests/Unit/Constraints/BuilderTest.php index 09cac42a..56f91802 100644 --- a/tests/Unit/Constraints/BuilderTest.php +++ b/tests/Unit/Constraints/BuilderTest.php @@ -2,6 +2,9 @@ namespace Silber\Bouncer\Tests\Unit\Constraints; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use PHPUnit\Framework\TestCase; use Silber\Bouncer\Constraints\Group; use Silber\Bouncer\Constraints\Builder; @@ -9,10 +12,8 @@ class BuilderTest extends TestCase { - /** - * @test - */ - function building_without_constraints_returns_empty_group() + #[Test] + public function building_without_constraints_returns_empty_group() { $actual = (new Builder())->build(); @@ -21,20 +22,16 @@ function building_without_constraints_returns_empty_group() $this->assertTrue($expected->equals($actual)); } - /** - * @test - */ - function a_single_where_returns_a_single_constraint() + #[Test] + public function a_single_where_returns_a_single_constraint() { $constraint = Builder::make()->where('active', false)->build(); $this->assertTrue($constraint->equals(Constraint::where('active', false))); } - /** - * @test - */ - function a_single_where_column_returns_a_single_column_constraint() + #[Test] + public function a_single_where_column_returns_a_single_column_constraint() { $builder = Builder::make()->whereColumn('team_id', 'team_id'); @@ -43,10 +40,8 @@ function a_single_where_column_returns_a_single_column_constraint() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function a_single_or_where_returns_a_single_or_constraint() + #[Test] + public function a_single_or_where_returns_a_single_or_constraint() { $actual = Builder::make()->orWhere('active', false)->build(); @@ -55,10 +50,8 @@ function a_single_or_where_returns_a_single_or_constraint() $this->assertTrue($expected->equals($actual)); } - /** - * @test - */ - function two_wheres_return_a_group() + #[Test] + public function two_wheres_return_a_group() { $builder = Builder::make() ->where('active', false) @@ -71,10 +64,8 @@ function two_wheres_return_a_group() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function two_where_columns_return_a_group() + #[Test] + public function two_where_columns_return_a_group() { $builder = Builder::make() ->whereColumn('active', false) @@ -87,10 +78,8 @@ function two_where_columns_return_a_group() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function or_wheres_return_a_group() + #[Test] + public function or_wheres_return_a_group() { $builder = Builder::make() ->where('active', false) @@ -103,10 +92,8 @@ function or_wheres_return_a_group() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function nested_wheres_return_a_group() + #[Test] + public function nested_wheres_return_a_group() { $builder = Builder::make()->where('active', false)->where(function ($query) { $query->where('a', 'b')->where('c', 'd'); @@ -123,10 +110,8 @@ function nested_wheres_return_a_group() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function nested_or_where_returns_an_or_group() + #[Test] + public function nested_or_where_returns_an_or_group() { $builder = Builder::make()->where('active', false)->orWhere(function ($query) { $query->where('a', 'b')->where('c', 'd'); @@ -143,10 +128,8 @@ function nested_or_where_returns_an_or_group() $this->assertTrue($expected->equals($builder->build())); } - /** - * @test - */ - function can_nest_multiple_levels() + #[Test] + public function can_nest_multiple_levels() { $builder = Builder::make() ->where('active', false) diff --git a/tests/Unit/Constraints/ConstraintTest.php b/tests/Unit/Constraints/ConstraintTest.php index f1271fee..b34d2132 100644 --- a/tests/Unit/Constraints/ConstraintTest.php +++ b/tests/Unit/Constraints/ConstraintTest.php @@ -2,8 +2,11 @@ namespace Silber\Bouncer\Tests\Unit\Constraints; -use Silber\Bouncer\Tests\User; -use Silber\Bouncer\Tests\Account; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; +use Workbench\App\Models\Account; use PHPUnit\Framework\TestCase; use Silber\Bouncer\Constraints\Constraint; @@ -12,10 +15,8 @@ class ConstraintTest extends TestCase { - /** - * @test - */ - function value_constraint_implicit_equals() + #[Test] + public function value_constraint_implicit_equals() { $authority = new User(); $activeAccount = new Account(['active' => true]); @@ -27,10 +28,8 @@ function value_constraint_implicit_equals() $this->assertFalse($constraint->check($inactiveAccount, $authority)); } - /** - * @test - */ - function value_constraint_explicit_equals() + #[Test] + public function value_constraint_explicit_equals() { $authority = new User(); $activeAccount = new Account(['active' => true]); @@ -42,10 +41,8 @@ function value_constraint_explicit_equals() $this->assertFalse($constraint->check($inactiveAccount, $authority)); } - /** - * @test - */ - function value_constraint_explicit_double_equals() + #[Test] + public function value_constraint_explicit_double_equals() { $authority = new User(); $activeAccount = new Account(['active' => true]); @@ -57,10 +54,8 @@ function value_constraint_explicit_double_equals() $this->assertFalse($constraint->check($inactiveAccount, $authority)); } - /** - * @test - */ - function value_constraint_not_equals() + #[Test] + public function value_constraint_not_equals() { $authority = new User(); $activeAccount = new Account(['active' => true]); @@ -72,10 +67,8 @@ function value_constraint_not_equals() $this->assertFalse($constraint->check($inactiveAccount, $authority)); } - /** - * @test - */ - function value_constraint_greater_than() + #[Test] + public function value_constraint_greater_than() { $authority = new User(); $forty = new User(['age' => 40]); @@ -87,10 +80,8 @@ function value_constraint_greater_than() $this->assertFalse($constraint->check($forty, $authority)); } - /** - * @test - */ - function value_constraint_less_than() + #[Test] + public function value_constraint_less_than() { $authority = new User(); $thirtyNine = new User(['age' => 39]); @@ -102,10 +93,8 @@ function value_constraint_less_than() $this->assertFalse($constraint->check($forty, $authority)); } - /** - * @test - */ - function value_constraint_greater_than_or_equal() + #[Test] + public function value_constraint_greater_than_or_equal() { $authority = new User(); $minor = new User(['age' => 17]); @@ -119,10 +108,8 @@ function value_constraint_greater_than_or_equal() $this->assertFalse($constraint->check($minor, $authority)); } - /** - * @test - */ - function value_constraint_less_than_or_equal() + #[Test] + public function value_constraint_less_than_or_equal() { $authority = new User(); $youngerTeen = new User(['age' => 18]); @@ -136,10 +123,8 @@ function value_constraint_less_than_or_equal() $this->assertFalse($constraint->check($adult, $authority)); } - /** - * @test - */ - function column_constraint_implicit_equals() + #[Test] + public function column_constraint_implicit_equals() { $authority = new User(['age' => 1]); $one = new User(['age' => 1]); @@ -151,10 +136,8 @@ function column_constraint_implicit_equals() $this->assertFalse($constraint->check($two, $authority)); } - /** - * @test - */ - function column_constraint_explicit_equals() + #[Test] + public function column_constraint_explicit_equals() { $authority = new User(['age' => 1]); $one = new User(['age' => 1]); @@ -166,10 +149,8 @@ function column_constraint_explicit_equals() $this->assertFalse($constraint->check($two, $authority)); } - /** - * @test - */ - function column_constraint_explicit_double_equals() + #[Test] + public function column_constraint_explicit_double_equals() { $authority = new User(['age' => 1]); $one = new User(['age' => 1]); @@ -181,10 +162,8 @@ function column_constraint_explicit_double_equals() $this->assertFalse($constraint->check($two, $authority)); } - /** - * @test - */ - function column_constraint_not_equals() + #[Test] + public function column_constraint_not_equals() { $authority = new User(['age' => 1]); $one = new User(['age' => 1]); @@ -196,10 +175,8 @@ function column_constraint_not_equals() $this->assertFalse($constraint->check($one, $authority)); } - /** - * @test - */ - function column_constraint_greater_than() + #[Test] + public function column_constraint_greater_than() { $authority = new User(['age' => 18]); @@ -214,10 +191,8 @@ function column_constraint_greater_than() $this->assertFalse($constraint->check($same, $authority)); } - /** - * @test - */ - function column_constraint_less_than() + #[Test] + public function column_constraint_less_than() { $authority = new User(['age' => 18]); @@ -232,10 +207,8 @@ function column_constraint_less_than() $this->assertFalse($constraint->check($same, $authority)); } - /** - * @test - */ - function column_constraint_greater_than_or_equal() + #[Test] + public function column_constraint_greater_than_or_equal() { $authority = new User(['age' => 18]); @@ -250,10 +223,8 @@ function column_constraint_greater_than_or_equal() $this->assertFalse($constraint->check($younger, $authority)); } - /** - * @test - */ - function column_constraint_less_than_or_equal() + #[Test] + public function column_constraint_less_than_or_equal() { $authority = new User(['age' => 18]); @@ -268,10 +239,8 @@ function column_constraint_less_than_or_equal() $this->assertFalse($constraint->check($older, $authority)); } - /** - * @test - */ - function value_constraint_can_be_properly_serialized_and_deserialized() + #[Test] + public function value_constraint_can_be_properly_serialized_and_deserialized() { $authority = new User(); $activeAccount = new Account(['active' => true]); @@ -286,10 +255,8 @@ function value_constraint_can_be_properly_serialized_and_deserialized() $this->assertFalse($constraint->check($inactiveAccount, $authority)); } - /** - * @test - */ - function column_constraint_can_be_properly_serialized_and_deserialized() + #[Test] + public function column_constraint_can_be_properly_serialized_and_deserialized() { $authority = new User(['age' => 1]); $one = new User(['age' => 1]); diff --git a/tests/Unit/Constraints/GroupsTest.php b/tests/Unit/Constraints/GroupsTest.php index 1713db11..4aa4f031 100644 --- a/tests/Unit/Constraints/GroupsTest.php +++ b/tests/Unit/Constraints/GroupsTest.php @@ -2,8 +2,11 @@ namespace Silber\Bouncer\Tests\Unit\Constraints; -use Silber\Bouncer\Tests\User; -use Silber\Bouncer\Tests\Account; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + +use Workbench\App\Models\User; +use Workbench\App\Models\Account; use PHPUnit\Framework\TestCase; use Silber\Bouncer\Constraints\Group; @@ -11,10 +14,8 @@ class GroupsTest extends TestCase { - /** - * @test - */ - function named_and_constructor() + #[Test] + public function named_and_constructor() { $group = Group::withAnd(); @@ -22,10 +23,8 @@ function named_and_constructor() $this->assertEquals('and', $group->logicalOperator()); } - /** - * @test - */ - function named_or_constructor() + #[Test] + public function named_or_constructor() { $group = Group::withOr(); @@ -33,10 +32,8 @@ function named_or_constructor() $this->assertEquals('or', $group->logicalOperator()); } - /** - * @test - */ - function group_of_constraints_only_passes_if_all_constraints_pass_the_check() + #[Test] + public function group_of_constraints_only_passes_if_all_constraints_pass_the_check() { $account = new Account([ 'name' => 'the-account', @@ -57,10 +54,8 @@ function group_of_constraints_only_passes_if_all_constraints_pass_the_check() $this->assertFalse($groupB->check($account, new User)); } - /** - * @test - */ - function group_of_ors_passes_if_any_constraint_passes_the_check() + #[Test] + public function group_of_ors_passes_if_any_constraint_passes_the_check() { $account = new Account([ 'name' => 'the-account', @@ -81,10 +76,8 @@ function group_of_ors_passes_if_any_constraint_passes_the_check() $this->assertFalse($groupB->check($account, new User)); } - /** - * @test - */ - function group_can_be_serialized_and_deserialized() + #[Test] + public function group_can_be_serialized_and_deserialized() { $activeAccount = new Account([ 'name' => 'the-account', @@ -106,10 +99,8 @@ function group_can_be_serialized_and_deserialized() $this->assertFalse($group->check($inactiveAccount, new User)); } - /** - * @test - */ - function group_can_be_added_to() + #[Test] + public function group_can_be_added_to() { $activeAccount = new Account([ 'name' => 'account', diff --git a/tests/WildcardsTest.php b/tests/WildcardsTest.php index 0f8f2699..93525bf5 100644 --- a/tests/WildcardsTest.php +++ b/tests/WildcardsTest.php @@ -2,20 +2,24 @@ namespace Silber\Bouncer\Tests; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; + use Silber\Bouncer\Database\Role; use Silber\Bouncer\Database\Ability; +use Workbench\App\Models\User; +use Workbench\App\Models\Account; + class WildcardsTest extends BaseTestCase { use Concerns\TestsClipboards; - /** - * @test - * @dataProvider bouncerProvider - */ - function a_wildard_ability_allows_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function a_wildard_ability_allows_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('*'); @@ -28,13 +32,11 @@ function a_wildard_ability_allows_everything($provider) $this->assertTrue($bouncer->cannot('*')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function manage_allows_all_actions_on_a_model($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function manage_allows_all_actions_on_a_model($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toManage($user); @@ -49,13 +51,11 @@ function manage_allows_all_actions_on_a_model($provider) $this->assertTrue($bouncer->cannot('edit', $user)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function manage_on_a_model_class_allows_all_actions_on_all_its_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function manage_on_a_model_class_allows_all_actions_on_all_its_models($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toManage(User::class); @@ -74,13 +74,11 @@ function manage_on_a_model_class_allows_all_actions_on_all_its_models($provider) $this->assertTrue($bouncer->cannot('edit', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function always_allows_the_action_on_all_models($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function always_allows_the_action_on_all_models($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete')->everything(); @@ -96,13 +94,11 @@ function always_allows_the_action_on_all_models($provider) $this->assertTrue($bouncer->cannot('delete', '*')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function everything_allows_everything($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function everything_allows_everything($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->everything(); @@ -127,13 +123,11 @@ function everything_allows_everything($provider) $this->assertTrue($bouncer->cannot('ban', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function a_simple_wildard_ability_denies_model_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function a_simple_wildard_ability_denies_model_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('*'); @@ -143,13 +137,11 @@ function a_simple_wildard_ability_denies_model_abilities($provider) $this->assertTrue($bouncer->cannot('*', User::class)); } - /** - * @test - * @dataProvider bouncerProvider - */ - function manage_denies_simple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function manage_denies_simple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toManage($user); @@ -157,13 +149,11 @@ function manage_denies_simple_abilities($provider) $this->assertTrue($bouncer->cannot('*')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function manage_on_a_model_class_denies_simple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function manage_on_a_model_class_denies_simple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->toManage(User::class); @@ -171,13 +161,11 @@ function manage_on_a_model_class_denies_simple_abilities($provider) $this->assertTrue($bouncer->cannot('edit')); } - /** - * @test - * @dataProvider bouncerProvider - */ - function always_denies_simple_abilities($provider) + #[Test] + #[DataProvider('bouncerProvider')] + public function always_denies_simple_abilities($provider) { - list($bouncer, $user) = $provider(); + [$bouncer, $user] = $provider(); $bouncer->allow($user)->to('delete')->everything(); diff --git a/tests/helpers.php b/tests/helpers.php deleted file mode 100644 index dab77104..00000000 --- a/tests/helpers.php +++ /dev/null @@ -1,18 +0,0 @@ -increments('id'); + $table->string('name')->nullable(); + $table->integer('age')->nullable(); + $table->timestamps(); + $table->softDeletes(); + }); + + Schema::create('accounts', function ($table) { + $table->increments('id'); + $table->integer('user_id')->unsigned()->nullable(); + $table->string('name')->nullable(); + $table->boolean('active')->default(true); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('users'); + Schema::drop('accounts'); + } +}; \ No newline at end of file