diff --git a/composer.json b/composer.json index cddef1c..31936d2 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ "minimum-stability": "dev", "require": { "php": "^7.3|^8.0", - "illuminate/support": "~8.0", - "illuminate/cache": "~8.0" + "illuminate/cache": "^8.0", + "illuminate/support": "^8.0" }, "require-dev": { "orchestra/database": "^6.0", @@ -22,7 +22,8 @@ }, "autoload": { "psr-4": { - "HttpOz\\Roles\\": "src/" + "HttpOz\\Roles\\": "src/", + "HttpOz\\Roles\\Database\\Factories\\": "database/factories/" } }, "autoload-dev": { @@ -31,6 +32,8 @@ } }, "config": { + "optimize-autoloader": true, + "preferred-install": "dist", "sort-packages": true }, "extra": { diff --git a/config/roles.php b/config/roles.php index 8f4b24e..2d8e030 100644 --- a/config/roles.php +++ b/config/roles.php @@ -34,7 +34,7 @@ | | Here you can enable cache and change the period for which cache should | remember the roles. Time should be in seconds, refer to official docs - | https://laravel.com/docs/5.8/cache. + | https://laravel.com/docs/8.x/cache. */ 'cache' => [ diff --git a/phpunit.xml b/phpunit.xml index 91bfe02..e9f226d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,36 +1,29 @@ - - - - ./tests/Feature - - - - ./tests/Unit - - - - - ./src - - - - - - - - - - - - - + + + + ./src + + + + + + + + + ./tests/Feature + + + ./tests/Unit + + + + + + + + + + + diff --git a/src/Models/Role.php b/src/Models/Role.php index 3bc655b..4dd3a27 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -3,7 +3,9 @@ namespace HttpOz\Roles\Models; +use HttpOz\Roles\Database\Factories\RoleFactory; use HttpOz\Roles\Traits\Sluggable; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use HttpOz\Roles\Traits\RoleHasRelations; @@ -11,7 +13,7 @@ class Role extends Model implements RoleHasRelationsContract { - use Sluggable, RoleHasRelations; + use Sluggable, RoleHasRelations, HasFactory; /** * The attributes that are mass assignable. @@ -42,4 +44,9 @@ public static function findBySlug($slug) { return self::where('slug', $slug)->first(); } + protected static function newFactory() + { + return new RoleFactory(); + } + } diff --git a/src/RolesServiceProvider.php b/src/RolesServiceProvider.php index aa06ff1..b218502 100644 --- a/src/RolesServiceProvider.php +++ b/src/RolesServiceProvider.php @@ -16,21 +16,14 @@ class RolesServiceProvider extends ServiceProvider */ public function boot() { - $this->publishes([ - __DIR__ . '/../config/roles.php' => config_path('roles.php') - ], 'config'); - - $stub = __DIR__ . '/../database/migrations/'; - $target = database_path('migrations').'/'; - - $this->publishes([ - $stub.'create_roles_table.php' => $target . '2016_09_04_000000_create_roles_table.php', - $stub.'create_role_user_table.php' => $target . '2016_09_04_100000_create_role_user_table.php' - ], 'migrations'); + // Load migrations + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations/'); - $this->publishes([ - __DIR__.'/../resources/views' => base_path('resources/views/vendor'), - ], 'views'); + // publish config file + $this->publishes([__DIR__ . '/../config/roles.php' => config_path('roles.php')], 'config'); + + // publish views + $this->loadViewsFrom(__DIR__.'/../resources/views', 'roles'); $this->registerBladeExtensions(); } diff --git a/tests/Feature/CreateRoleTest.php b/tests/Feature/CreateRoleTest.php index 3f5f5c5..9ff6218 100644 --- a/tests/Feature/CreateRoleTest.php +++ b/tests/Feature/CreateRoleTest.php @@ -14,7 +14,7 @@ class CreateRoleTest extends TestCase */ public function testCanCreateRole() { - $adminRole = factory(Role::class)->create([ + $adminRole = Role::factory()->create([ 'name' => 'Admin', 'slug' => 'admin', 'description' => 'Custodians of the system.', diff --git a/tests/Feature/RoleMiddlewareTest.php b/tests/Feature/RoleMiddlewareTest.php index 91a9ba3..eb7cbe0 100644 --- a/tests/Feature/RoleMiddlewareTest.php +++ b/tests/Feature/RoleMiddlewareTest.php @@ -6,13 +6,10 @@ use HttpOz\Roles\Tests\Stubs\User; use HttpOz\Roles\Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; class RoleMiddlewareTest extends TestCase { - use DatabaseMigrations; - /** * A basic test example. * @@ -20,8 +17,8 @@ class RoleMiddlewareTest extends TestCase */ public function testForbiddenRoleRoute() { - $createdUser = factory(User::class)->create(); - $createdRole = factory(Role::class)->create([ + $createdUser = User::factory()->create(); + $createdRole = Role::factory()->create([ 'name' => 'Admin', 'slug' => 'admin', ]); @@ -37,9 +34,5 @@ public function testForbiddenRoleRoute() $this->assertEquals($createdRole->id, $foundRole->id); $this->assertEquals($createdUser->name, $foundUser->name); - - /*$this->get('/admin/users') - ->assertStatus(200) - ->assertSee('I am an admin.');*/ } } diff --git a/tests/Feature/UserRoleTest.php b/tests/Feature/UserRoleTest.php index f39aa07..cadc232 100644 --- a/tests/Feature/UserRoleTest.php +++ b/tests/Feature/UserRoleTest.php @@ -4,18 +4,15 @@ use HttpOz\Roles\Tests\Stubs\User; use \HttpOz\Roles\Models\Role; -use Illuminate\Foundation\Testing\DatabaseMigrations; use HttpOz\Roles\Tests\TestCase; class UserRoleTest extends TestCase { - use DatabaseMigrations; - public function testUserHasRoleOnAttach() { - $admin = factory(User::class)->create(); - $role = factory(Role::class)->create(); + $admin = User::factory()->create(); + $role = Role::factory()->create(); $adminRole = Role::findBySlug('admin'); $user = User::find(1); diff --git a/tests/Stubs/User.php b/tests/Stubs/User.php index e9a53fa..01a451c 100644 --- a/tests/Stubs/User.php +++ b/tests/Stubs/User.php @@ -2,6 +2,7 @@ namespace HttpOz\Roles\Tests\Stubs; +use HttpOz\Roles\Database\Factories\UserFactory; use \HttpOz\Roles\Traits\HasRole; use \HttpOz\Roles\Contracts\HasRole as HasRoleContract; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -21,4 +22,11 @@ public function getAuthIdentifiersName(): array { return ['email', 'username']; } + + + + protected static function newFactory() + { + return new UserFactory(); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4f94a22..baebbb8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,11 @@ namespace HttpOz\Roles\Tests; +use HttpOz\Roles\Models\Role; +use HttpOz\Roles\RolesServiceProvider; use HttpOz\Roles\Tests\Stubs\User; +use Illuminate\Foundation\Application; +use Orchestra\Database\ConsoleServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; class TestCase extends Orchestra @@ -15,21 +19,20 @@ public function setUp(): void parent::setUp(); $this->loadLaravelMigrations(['--database' => 'testbench']); $this->setUpDatabase($this->app); - - $this->withFactories(__DIR__ . '/../database/factories'); } - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { return [ - \Orchestra\Database\ConsoleServiceProvider::class + RolesServiceProvider::class, + ConsoleServiceProvider::class ]; } /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app * @return void */ public function getEnvironmentSetUp($app) @@ -50,7 +53,7 @@ public function getEnvironmentSetUp($app) 'expiry' => 20160, ], 'models' => [ - 'role' => \HttpOz\Roles\Models\Role::class + 'role' => Role::class ], 'pretend' => [ 'enabled' => false, @@ -64,7 +67,7 @@ public function getEnvironmentSetUp($app) /** * Set up the database. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app */ protected function setUpDatabase($app) { diff --git a/tests/Unit/HasRoleTraitTest.php b/tests/Unit/HasRoleTraitTest.php index 045fd22..329399a 100644 --- a/tests/Unit/HasRoleTraitTest.php +++ b/tests/Unit/HasRoleTraitTest.php @@ -6,125 +6,134 @@ use HttpOz\Roles\Tests\Stubs\User; use HttpOz\Roles\Tests\TestCase; -class HasRoleTraitTest extends TestCase { - public function testCanAttachRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); +class HasRoleTraitTest extends TestCase +{ + public function testCanAttachRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + + $this->assertEquals(1, $user->roles()->count()); + } - $user->attachRole( $adminRole ); + public function testCanDetachRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $user->detachRole($adminRole); + + $this->assertEquals(1, $user->roles()->count()); + $this->assertEquals('manager', $user->roles()->first()->slug); + } + + public function testCanDetachAllRoles() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $this->assertEquals(2, $user->roles()->count()); + + $user->detachAllRoles(); + + $this->assertEquals(0, $user->roles()->count()); + } + + public function testHasRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + + $user->attachRole($adminRole); + + $this->assertFalse($user->hasRole('manager')); + + config(['roles.cache.enabled' => true]); + + $this->assertTrue($user->hasRole('admin')); + $this->assertFalse($user->hasRole('manager')); + } + + public function testIsRole() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); + + $this->assertTrue($user->isRole($adminRole->slug)); + $this->assertFalse($user->isRole($managerRole->slug)); + + config(['roles.pretend.enabled' => true]); + + + $this->assertTrue($user->isRole($managerRole->slug)); + } + + public function testIsAll() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); + $user->attachRole($managerRole); + + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); + } + + public function testSyncRoles() + { + $user = User::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); - $this->assertEquals( 1, $user->roles()->count()); - } + $user->syncRoles([$managerRole->id, $adminRole->id]); - public function testCanDetachRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - $user->attachRole( $adminRole ); - $user->attachRole( $managerRole ); + config(['roles.pretend.enabled' => true]); - $user->detachRole( $adminRole ); + $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); + } - $this->assertEquals( 1, $user->roles()->count() ); - $this->assertEquals( 'manager', $user->roles()->first()->slug ); - } + public function testGroup() + { + $user = USer::factory()->create([ + 'name' => 'The Oz' + ]); + $adminRole = Role::factory()->create(); + $managerRole = Role::factory()->create(['name' => 'Manager', 'slug' => 'manager']); + $user->attachRole($adminRole); - public function testCanDetachAllRoles() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); + $this->assertEquals('system.admin', $user->group()); + $this->assertTrue($user->inGroup('system.admin')); + $this->assertFalse($user->inGroup('default')); - $user->attachRole( $adminRole ); - $user->attachRole( $managerRole ); - - $this->assertEquals( 2, $user->roles()->count() ); - - $user->detachAllRoles(); - - $this->assertEquals( 0, $user->roles()->count() ); - } - - public function testHasRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - - $user->attachRole($adminRole); - - $this->assertFalse($user->hasRole('manager')); - - config(['roles.cache.enabled' => true]); - - $this->assertTrue($user->hasRole('admin')); - $this->assertFalse($user->hasRole('manager')); - } - - public function testIsRole() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - - $this->assertTrue($user->isRole($adminRole->slug)); - $this->assertFalse($user->isRole($managerRole->slug)); - - config(['roles.pretend.enabled' => true]); - - - $this->assertTrue($user->isRole($managerRole->slug)); - } - - public function testIsAll() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - $user->attachRole($managerRole); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - } - - public function testSyncRoles() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - - $user->syncRoles([$managerRole->id, $adminRole->id]); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - - config(['roles.pretend.enabled' => true]); - - $this->assertTrue($user->isAll([$adminRole->id, $managerRole->id])); - } - - public function testGroup() { - $user = factory( User::class )->create( [ - 'name' => 'The Oz' - ] ); - $adminRole = factory( Role::class )->create(); - $managerRole = factory( Role::class )->create( [ 'name' => 'Manager', 'slug' => 'manager' ] ); - $user->attachRole($adminRole); - - $this->assertEquals('system.admin', $user->group()); - $this->assertTrue($user->inGroup('system.admin')); - $this->assertFalse($user->inGroup('default')); - - } + } } \ No newline at end of file diff --git a/tests/Unit/RoleHasRelationsTraitTest.php b/tests/Unit/RoleHasRelationsTraitTest.php index 0c9f886..13f1b9f 100644 --- a/tests/Unit/RoleHasRelationsTraitTest.php +++ b/tests/Unit/RoleHasRelationsTraitTest.php @@ -10,8 +10,8 @@ class RoleHasRelationsTraitTest extends TestCase { public function testRoleCanFetchUsers() { - $role = factory(Role::class)->create(); - factory(User::class, 10)->create()->each(function ($u) use ($role) { + $role = Role::factory()->create(); + User::factory(10)->create()->each(function ($u) use ($role) { $u->attachRole($role); }); diff --git a/tests/Unit/RoleTest.php b/tests/Unit/RoleTest.php index f87855a..0573083 100644 --- a/tests/Unit/RoleTest.php +++ b/tests/Unit/RoleTest.php @@ -3,16 +3,13 @@ namespace HttpOz\Roles\Tests\Unit; use HttpOz\Roles\Models\Role; -use Illuminate\Foundation\Testing\DatabaseMigrations; use HttpOz\Roles\Tests\TestCase; class RoleTest extends TestCase { - use DatabaseMigrations; - public function testRoleCanBeFoundBySlug() { - $createdRole = factory(Role::class)->create(['name' => 'Admin', 'slug' => 'admin']); + $createdRole = Role::factory()->create(['name' => 'Admin', 'slug' => 'admin']); $foundRole = Role::findBySlug('admin'); $this->assertEquals($createdRole->id, $foundRole->id);