From 659ede044f3596ae0bf8dbc3d448baa2a04f7acd Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Mon, 22 Jul 2024 16:39:59 -0700 Subject: [PATCH 1/3] GH-34 - update PHPStan for user factories. Updated user seeder handling. --- config/playground-test.php | 70 ++++++++- database/factories/DefaultUserFactory.php | 5 - resources/CustomUsersTableSeeder.php | 133 +++++++++++++++--- src/Models/AbstractUser.php | 3 - src/Models/DefaultUser.php | 6 +- src/Models/Demo.php | 4 +- src/Models/UserWithRole.php | 6 + src/Models/UserWithRoleAndPrivileges.php | 6 + src/Models/UserWithRoleAndRoles.php | 6 + .../UserWithRoleAndRolesAndPrivileges.php | 10 +- src/Models/UserWithSanctum.php | 5 + src/Models/UserWithWithoutSanctumContract.php | 5 + src/ServiceProvider.php | 9 +- 13 files changed, 223 insertions(+), 45 deletions(-) diff --git a/config/playground-test.php b/config/playground-test.php index ef6ef0a..040ffb0 100644 --- a/config/playground-test.php +++ b/config/playground-test.php @@ -3,12 +3,62 @@ declare(strict_types=1); return [ - 'password' => env('PLAYGROUND_TEST_PASSWORD', 'password'), + + /* + |-------------------------------------------------------------------------- + | Credentials for testing + |-------------------------------------------------------------------------- + | + | If the password is empty, a random value will be set. + | + | For extra security, a password hash may be set in + | PLAYGROUND_TEST_PASSWORD; and then PLAYGROUND_TEST_PASSWORD_ENCRYPTED must + | be set to true. + | + */ + + 'password' => env('PLAYGROUND_TEST_PASSWORD', ''), + 'password_encrypted' => (bool) env('PLAYGROUND_TEST_PASSWORD_ENCRYPTED', false), + + /* + |-------------------------------------------------------------------------- + | Roles and Privileges + |-------------------------------------------------------------------------- + | + | with_role: users.role string + | with_roles: users.roles role[] + | with_privileges: users.privileges privilege[] + | + | All privileges: ['*'] + | + */ + + 'with_active' => (bool) env('PLAYGROUND_TEST_WITH_ACTIVE', true), + + 'with_description' => (bool) env('PLAYGROUND_TEST_WITH_DESCRIPTION', true), + + 'with_privileges' => (bool) env('PLAYGROUND_TEST_WITH_PRIVILEGES', true), + + 'with_role' => (bool) env('PLAYGROUND_TEST_WITH_ROLE', true), + + 'with_roles' => (bool) env('PLAYGROUND_TEST_WITH_ROLES', true), + + 'with_status' => (bool) env('PLAYGROUND_TEST_WITH_STATUS', true), + + /* + |-------------------------------------------------------------------------- + | Users + |-------------------------------------------------------------------------- + | + | + */ + 'users' => [ 'admin' => [ 'env' => 'TEST_EMAIL_ADMIN', 'name' => 'Admin Nimda', + 'privileges' => [], 'role' => 'admin', 'roles' => [ 'user', @@ -21,6 +71,7 @@ 'client' => [ 'env' => 'TEST_EMAIL_CLIENT', 'name' => 'Client Tneilc', + 'privileges' => [], 'role' => 'client', 'roles' => [], 'description' => 'User: client', @@ -29,6 +80,7 @@ 'client-admin' => [ 'env' => 'TEST_EMAIL_CLIENT_ADMIN', 'name' => 'Client Admin Nimda Tneilc', + 'privileges' => [], 'role' => 'client', 'roles' => [ 'client-admin', @@ -39,6 +91,7 @@ 'partner' => [ 'env' => 'TEST_EMAIL_PARTNER', 'name' => 'Partner Rentrap', + 'privileges' => [], 'role' => 'partner', 'roles' => [], 'description' => 'User: partner', @@ -47,6 +100,7 @@ 'partner-admin' => [ 'env' => 'TEST_EMAIL_PARTNER_ADMIN', 'name' => 'Partner Admin Nimd Rentrap', + 'privileges' => [], 'role' => 'partner', 'roles' => [ 'partner-admin', @@ -57,6 +111,7 @@ 'sales' => [ 'env' => 'TEST_EMAIL_SALES', 'name' => 'Sales Selas', + 'privileges' => [], 'role' => 'sales', 'roles' => [ 'user', @@ -68,6 +123,7 @@ 'env' => 'TEST_EMAIL_SALES_ADMIN', 'role' => 'sales', 'name' => 'Sales Admin Nimda Troppus', + 'privileges' => [], 'roles' => [ 'user', 'sales-admin', @@ -78,6 +134,7 @@ 'support' => [ 'env' => 'TEST_EMAIL_SUPPORT', 'name' => 'Support Troppus', + 'privileges' => [], 'role' => 'support', 'roles' => [ 'user', @@ -88,6 +145,7 @@ 'support-admin' => [ 'env' => 'TEST_EMAIL_SUPPORT_ADMIN', 'name' => 'Support Admin Nimda Troppus', + 'privileges' => [], 'role' => 'support', 'roles' => [ 'user', @@ -99,6 +157,7 @@ 'vendor' => [ 'env' => 'TEST_EMAIL_VENDOR', 'name' => 'Vendor Rodnev', + 'privileges' => [], 'role' => 'vendor', 'roles' => [ 'user', @@ -109,6 +168,7 @@ 'vendor-admin' => [ 'env' => 'TEST_EMAIL_VENDOR_ADMIN', 'name' => 'Vendor Admin Nimda Rodnev', + 'privileges' => [], 'role' => 'vendor', 'roles' => [ 'user', @@ -120,6 +180,7 @@ 'manager' => [ 'env' => 'TEST_EMAIL_MANAGER', 'name' => 'Manager Reganam', + 'privileges' => [], 'role' => 'manager', 'roles' => [ 'user', @@ -132,6 +193,7 @@ 'manager-admin' => [ 'env' => 'TEST_EMAIL_MANAGER_ADMIN', 'name' => 'Manager Admin Nimda Reganam', + 'privileges' => [], 'role' => 'manager', 'roles' => [ 'user', @@ -145,6 +207,7 @@ 'wheel' => [ 'env' => 'TEST_EMAIL_WHEEL', 'name' => 'Wheel Leehw', + 'privileges' => [], 'role' => 'admin', 'roles' => [ 'root', @@ -155,6 +218,9 @@ 'root' => [ 'env' => 'TEST_EMAIL_ROOT', 'name' => 'Root Toor', + 'privileges' => [ + '*', + ], 'role' => 'root', 'roles' => [], 'description' => 'User: root', @@ -163,6 +229,7 @@ 'user' => [ 'env' => 'TEST_EMAIL_USER', 'name' => 'User Resu', + 'privileges' => [], 'role' => 'user', 'roles' => [], 'description' => 'User: user', @@ -171,6 +238,7 @@ 'user-admin' => [ 'env' => 'TEST_EMAIL_USER_ADMIN', 'name' => 'User Admin Nimda Resu', + 'privileges' => [], 'role' => 'user', 'roles' => [ 'user', diff --git a/database/factories/DefaultUserFactory.php b/database/factories/DefaultUserFactory.php index 3a4d307..15de653 100644 --- a/database/factories/DefaultUserFactory.php +++ b/database/factories/DefaultUserFactory.php @@ -19,11 +19,6 @@ */ class DefaultUserFactory extends Factory { - /** - * The name of the factory's corresponding model. - * - * @var class-string - */ protected $model = DefaultUser::class; /** diff --git a/resources/CustomUsersTableSeeder.php b/resources/CustomUsersTableSeeder.php index a772c57..bceab09 100644 --- a/resources/CustomUsersTableSeeder.php +++ b/resources/CustomUsersTableSeeder.php @@ -1,15 +1,16 @@ + */ + protected string $userClass = User::class; + /** * Run the database seeds. * @@ -33,6 +51,42 @@ public function run() return; } + /** + * @var class-string + */ + $userClass = $this->userClass; + + if (! empty(config('auth.providers.users.model')) + && is_string(config('auth.providers.users.model')) + && class_exists(config('auth.providers.users.model')) + ) { + $userClass = config('auth.providers.users.model'); + } + + if (empty($config['with_active'])) { + $this->withActive = false; + } + + if (empty($config['with_description'])) { + $this->withDescription = false; + } + + if (empty($config['with_privileges'])) { + $this->withPrivileges = false; + } + + if (empty($config['with_role'])) { + $this->withRole = false; + } + + if (empty($config['with_roles'])) { + $this->withRoles = false; + } + + if (empty($config['with_status'])) { + $this->withStatus = false; + } + $password = empty($config['password']) || ! is_string($config['password']) ? '' : $config['password']; // $password = 'testing'; $password_encrypted = ! empty($config['password_encrypted']); @@ -48,30 +102,37 @@ public function run() foreach ($config['users'] as $slug => $meta) { $email = sprintf('%1$s@example.com', Str::slug($slug)); - $model = User::where('email', $email)->first(); + $model = $userClass::where('email', $email)->first(); + + $data = [ + 'name' => empty($meta['name']) || ! is_string($meta['name']) ? 'Some Name' : $meta['name'], + ]; + + if ($this->withActive) { + $data['active'] = true; + } + + if ($this->withRole) { + $data['description'] = empty($meta['description']) || ! is_string($meta['description']) ? '' : $meta['description']; + } + + if ($this->withRole) { + $data['role'] = empty($meta['role']) || ! is_string($meta['role']) ? '' : $meta['role']; + } + + if ($this->withRole) { + $data['status'] = empty($meta['status']) || ! is_numeric($meta['status']) ? 0 : $meta['status']; + } if (empty($model)) { - $model = new User([ - 'name' => empty($meta['name']) || ! is_string($meta['name']) ? 'Some Name' : $meta['name'], - 'description' => empty($meta['description']) || ! is_string($meta['description']) ? '' : $meta['description'], - 'active' => true, - 'email' => $email, - 'role' => empty($meta['role']) || ! is_string($meta['role']) ? '' : $meta['role'], - 'status' => empty($meta['status']) || ! is_numeric($meta['status']) ? 0 : $meta['status'], - ]); + $data['email'] = $email; + $model = $userClass::create($data); } else { - $model->update([ - 'name' => empty($meta['name']) || ! is_string($meta['name']) ? 'Some Name' : $meta['name'], - 'description' => empty($meta['description']) || ! is_string($meta['description']) ? '' : $meta['description'], - 'active' => true, - 'role' => empty($meta['role']) || ! is_string($meta['role']) ? '' : $meta['role'], - 'status' => empty($meta['status']) || ! is_numeric($meta['status']) ? 0 : $meta['status'], - ]); + $model->update($data); } - $roles = []; - - if (is_array($meta['roles'])) { + if ($this->withRoles && is_array($meta['roles'])) { + $roles = []; foreach ($meta['roles'] as $role) { if (! empty($role) && is_string($role) @@ -81,9 +142,35 @@ public function run() $roles[] = $role; } } + $model->roles = $roles; + } + + if ($this->withPrivileges && is_array($meta['privileges'])) { + $privileges = []; + foreach ($meta['privileges'] as $privilege) { + if (! empty($privilege) + && is_string($privilege) + && ! in_array($privilege, $privileges) + ) { + $privileges[] = $privilege; + } + } + $model->privileges = $privileges; } - $model->roles = $roles; + // dd([ + // '__METHOD__' => __METHOD__, + // '$password' => $password, + // '$password_encrypted' => $password_encrypted, + // '$email' => $email, + // '$slug' => $slug, + // '$meta' => $meta, + // '$roles' => $roles, + // '$privileges' => $privileges, + // // '$config' => $config, + // // '$model' => $model->toArray(), + // '$model' => $model, + // ]); // Reset the password $model->password = $password; diff --git a/src/Models/AbstractUser.php b/src/Models/AbstractUser.php index 91a1d62..dd675ff 100644 --- a/src/Models/AbstractUser.php +++ b/src/Models/AbstractUser.php @@ -6,7 +6,6 @@ */ namespace Playground\Test\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as BaseUser; /** @@ -23,8 +22,6 @@ */ abstract class AbstractUser extends BaseUser { - use HasFactory; - /** * @var array */ diff --git a/src/Models/DefaultUser.php b/src/Models/DefaultUser.php index c6e544e..e1ad0a0 100644 --- a/src/Models/DefaultUser.php +++ b/src/Models/DefaultUser.php @@ -6,6 +6,7 @@ namespace Playground\Test\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; +use Database\Factories\Playground\Test\Models\DefaultUserFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -19,7 +20,10 @@ */ class DefaultUser extends Authenticatable { - use HasFactory, Notifiable; + /** @use HasFactory */ + use HasFactory; + + use Notifiable; /** * The attributes that are mass assignable. diff --git a/src/Models/Demo.php b/src/Models/Demo.php index 4a063f0..3106a89 100644 --- a/src/Models/Demo.php +++ b/src/Models/Demo.php @@ -1,9 +1,9 @@ */ + use HasFactory; + /** * @var array */ diff --git a/src/Models/UserWithRoleAndPrivileges.php b/src/Models/UserWithRoleAndPrivileges.php index 96fb325..289e852 100644 --- a/src/Models/UserWithRoleAndPrivileges.php +++ b/src/Models/UserWithRoleAndPrivileges.php @@ -6,6 +6,9 @@ */ namespace Playground\Test\Models; +use Database\Factories\Playground\Test\Models\UserWithRoleAndRolesAndPrivilegesFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; + /** * \Playground\Test\Models\UserWithRoleAndPrivileges * @@ -23,6 +26,9 @@ class UserWithRoleAndPrivileges extends AbstractUser { use Concerns\UserPrivileges; + /** @use HasFactory */ + use HasFactory; + /** * @var array */ diff --git a/src/Models/UserWithRoleAndRoles.php b/src/Models/UserWithRoleAndRoles.php index 1475825..75f1547 100644 --- a/src/Models/UserWithRoleAndRoles.php +++ b/src/Models/UserWithRoleAndRoles.php @@ -6,6 +6,9 @@ */ namespace Playground\Test\Models; +use Database\Factories\Playground\Test\Models\UserWithRoleFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; + /** * \Playground\Test\Models\UserWithRoleAndRoles * @@ -22,6 +25,9 @@ class UserWithRoleAndRoles extends AbstractUser { use Concerns\UserPrivileges; + /** @use HasFactory */ + use HasFactory; + /** * @var array */ diff --git a/src/Models/UserWithRoleAndRolesAndPrivileges.php b/src/Models/UserWithRoleAndRolesAndPrivileges.php index 76d95af..e78e6d6 100644 --- a/src/Models/UserWithRoleAndRolesAndPrivileges.php +++ b/src/Models/UserWithRoleAndRolesAndPrivileges.php @@ -1,11 +1,14 @@ */ + use HasFactory; + /** * @var array */ diff --git a/src/Models/UserWithSanctum.php b/src/Models/UserWithSanctum.php index 6ad56b0..77b926f 100644 --- a/src/Models/UserWithSanctum.php +++ b/src/Models/UserWithSanctum.php @@ -6,6 +6,8 @@ */ namespace Playground\Test\Models; +use Database\Factories\Playground\Test\Models\UserWithSanctumFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract; use Laravel\Sanctum\HasApiTokens; @@ -24,4 +26,7 @@ class UserWithSanctum extends AbstractUser implements HasApiTokensContract { use HasApiTokens; + + /** @use HasFactory */ + use HasFactory; } diff --git a/src/Models/UserWithWithoutSanctumContract.php b/src/Models/UserWithWithoutSanctumContract.php index b3fc013..c1b051c 100644 --- a/src/Models/UserWithWithoutSanctumContract.php +++ b/src/Models/UserWithWithoutSanctumContract.php @@ -6,6 +6,8 @@ */ namespace Playground\Test\Models; +use Database\Factories\Playground\Test\Models\UserWithWithoutSanctumContractFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Laravel\Sanctum\HasApiTokens; /** @@ -26,4 +28,7 @@ class UserWithWithoutSanctumContract extends AbstractUser { use HasApiTokens; + + /** @use HasFactory */ + use HasFactory; } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index db4a222..6bdb1dd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -47,14 +47,12 @@ public function about(): void $testPassword = ! empty($config['password']) && is_string($config['password']) ? str_repeat('*', strlen($config['password'])) : ''; $testPasswordEncrypted = ! empty($config['password_encrypted']); - $version = $this->version(); - AboutCommand::add('Playground: Test', fn () => [ 'PLAYGROUND_TEST_PASSWORD_ENCRYPTED' => $testPasswordEncrypted ? 'ENCRYPTED' : 'PLAIN TEXT', 'User Lists' => sprintf('[%s]', implode(', ', array_keys($users))), 'PLAYGROUND_TEST_PASSWORD Save' => sprintf('[%s]', $testPassword), 'Package' => $this->package, - 'Version' => $version, + 'Version' => ServiceProvider::VERSION, ]); } @@ -65,9 +63,4 @@ public function register(): void $this->package ); } - - public function version(): string - { - return static::VERSION; - } } From de288eccb58a1d17c861d182ca02660b64df1b62 Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Mon, 22 Jul 2024 16:45:02 -0700 Subject: [PATCH 2/3] GH-34 - update PHPStan for user factories. Updated user seeder handling. --- README.md | 14 +++++++------- database/factories/UserWithSanctumFactory.php | 4 ++-- .../UserWithWithoutSanctumContractFactory.php | 4 ++-- database/factories/WidgetFactory.php | 4 ++-- src/Models/AbstractUser.php | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 655fc19..a0da6b7 100644 --- a/README.md +++ b/README.md @@ -38,21 +38,21 @@ composer cloc ``` ➜ playground-test git:(develop) ✗ composer cloc > cloc --exclude-dir=output,vendor . - 151 text files. - 88 unique files. - 64 files ignored. + 159 text files. + 93 unique files. + 67 files ignored. -github.com/AlDanial/cloc v 1.98 T=0.11 s (795.4 files/s, 87907.6 lines/s) +github.com/AlDanial/cloc v 1.98 T=0.12 s (760.8 files/s, 82920.9 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- -PHP 79 1703 2037 5209 +PHP 84 1767 2117 5473 XML 4 0 15 294 YAML 1 5 0 275 Markdown 3 35 0 88 -JSON 1 0 0 65 +JSON 1 0 0 67 ------------------------------------------------------------------------------- -SUM: 88 1743 2052 5931 +SUM: 93 1807 2132 6197 ------------------------------------------------------------------------------- ``` diff --git a/database/factories/UserWithSanctumFactory.php b/database/factories/UserWithSanctumFactory.php index aaf6d27..074dfd1 100644 --- a/database/factories/UserWithSanctumFactory.php +++ b/database/factories/UserWithSanctumFactory.php @@ -1,9 +1,9 @@ Date: Tue, 30 Jul 2024 10:33:08 -0700 Subject: [PATCH 3/3] GH-34 - updated README --- README.md | 8 ++++---- composer.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a0da6b7..6770e99 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,13 @@ composer cloc ``` ``` -➜ playground-test git:(develop) ✗ composer cloc +➜ playground-test git:(feature/GH-34) composer cloc > cloc --exclude-dir=output,vendor . - 159 text files. + 158 text files. 93 unique files. - 67 files ignored. + 66 files ignored. -github.com/AlDanial/cloc v 1.98 T=0.12 s (760.8 files/s, 82920.9 lines/s) +github.com/AlDanial/cloc v 1.98 T=0.15 s (615.0 files/s, 67029.5 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- diff --git a/composer.json b/composer.json index 85bdfa9..aad8f38 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "playground", "test" ], - "homepage": "https://gammamatrix-playground.readthedocs.io/", + "homepage": "https://github.com/gammamatrix/playground-test", "license": "MIT", "authors": [ {