|
| 1 | +<?php |
| 2 | + |
| 3 | +use Ajimoti\RolesAndPermissions\Tests\Enums\MerchantRole; |
| 4 | +use Ajimoti\RolesAndPermissions\Tests\Models\Merchant; |
| 5 | +use Ajimoti\RolesAndPermissions\Tests\Models\User; |
| 6 | + |
| 7 | +beforeEach(function () { |
| 8 | + config()->set('roles-and-permissions.roles_enum.merchant_user', MerchantRole::class); |
| 9 | + |
| 10 | + $this->model = Merchant::factory()->create(); |
| 11 | + $this->user = User::factory()->create(); |
| 12 | + $this->role = MerchantRole::getRandomValue(); |
| 13 | + $this->roleInstance = MerchantRole::fromValue($this->role); |
| 14 | + |
| 15 | + $this->user->of($this->model)->assign($this->role); |
| 16 | + $this->model->assign($this->role); |
| 17 | +}); |
| 18 | + |
| 19 | +it('can check that a model has a role using magic method', function () { |
| 20 | + expect($this->model->hasRole($this->role))->toBeTrue(); |
| 21 | + |
| 22 | + expect($this->model->{"is".$this->roleInstance->key}())->toBeTrue(); |
| 23 | +})->group('MagicMethods'); |
| 24 | + |
| 25 | +it('can check that a model has a permission using magic method', function () { |
| 26 | + expect($this->model->hasRole($this->role))->toBeTrue(); |
| 27 | + |
| 28 | + foreach ($this->roleInstance->permissions as $permission) { |
| 29 | + expect($this->model->{"can".$permission->key}())->toBeTrue(); |
| 30 | + } |
| 31 | +})->group('MagicMethods'); |
| 32 | + |
| 33 | +it('can check that a many-to-many relationship has a role using magic method', function () { |
| 34 | + expect($this->user->of($this->model)->hasRole($this->role))->toBeTrue(); |
| 35 | + |
| 36 | + expect($this->user->of($this->model)->{"is".$this->roleInstance->key}())->toBeTrue(); |
| 37 | +})->group('MagicMethods'); |
| 38 | + |
| 39 | +it('can check that a many-to-many relationship has a permission using magic method', function () { |
| 40 | + expect($this->user->of($this->model)->hasRole($this->role))->toBeTrue(); |
| 41 | + |
| 42 | + foreach ($this->roleInstance->permissions as $permission) { |
| 43 | + expect($this->user->of($this->model)->{"can".$permission->key}())->toBeTrue(); |
| 44 | + } |
| 45 | +})->group('MagicMethods'); |
| 46 | + |
| 47 | +it('magic method functionality does not interfere with laravel `is()` method', function () { |
| 48 | + expect($this->user->is(User::factory()->create()))->toBeFalse(); |
| 49 | + |
| 50 | + expect($this->user->is($this->user))->toBeTrue(); |
| 51 | +})->group('MagicMethods'); |
| 52 | + |
| 53 | +it('magic method functionality does not interfere with laravel `can()` method', function () { |
| 54 | + expect($this->model->can("a_random_permission_ha_ha_ha"))->toBeFalse(); |
| 55 | + |
| 56 | + // Cases where MerchantRole::Customer is the assigned role, |
| 57 | + // the permissions list will be empty |
| 58 | + if ($this->roleInstance->permissions->isNotEmpty()) { |
| 59 | + $permission = $this->roleInstance->permissions->random(); |
| 60 | + |
| 61 | + expect($this->model->can($permission->value))->toBeTrue(); |
| 62 | + } |
| 63 | +})->group('MagicMethods'); |
0 commit comments