From c7c2141e3aeffe6d60eb87a46e2ec6df999730ee Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Mon, 8 Jul 2024 11:55:05 -0700 Subject: [PATCH] v73.0.0 --- README.md | 55 +++++++++++++++++ composer.json | 5 +- config/playground.php | 13 ++++ database/factories/UserFactory.php | 60 +++++++++++++++++++ .../0001_01_01_000000_create_users_table.php | 51 ---------------- ...31_create_personal_access_tokens_table.php | 35 ----------- .../0001_01_01_000001_create_cache_table.php | 37 ------------ .../0001_01_01_000002_create_jobs_table.php | 59 ------------------ .../0001_01_01_000000_create_users_table.php | 0 .../0001_01_01_000001_create_cache_table.php | 0 .../0001_01_01_000002_create_jobs_table.php | 0 ...31_create_personal_access_tokens_table.php | 0 src/ServiceProvider.php | 50 +++++++++++----- .../Console/Commands/About/CommandTest.php | 13 ++++ tests/Feature/TestCase.php | 40 ++++--------- tests/Unit/PackageProviders.php | 22 +++++++ tests/Unit/ServiceProvider/InstanceTest.php | 5 +- tests/Unit/TestCase.php | 24 +------- 18 files changed, 214 insertions(+), 255 deletions(-) delete mode 100644 database/migrations-laravel/0001_01_01_000000_create_users_table.php delete mode 100644 database/migrations-laravel/2024_03_13_210031_create_personal_access_tokens_table.php delete mode 100644 database/migrations-playground/0001_01_01_000001_create_cache_table.php delete mode 100644 database/migrations-playground/0001_01_01_000002_create_jobs_table.php rename database/{migrations-playground => migrations}/0001_01_01_000000_create_users_table.php (100%) rename database/{migrations-laravel => migrations}/0001_01_01_000001_create_cache_table.php (100%) rename database/{migrations-laravel => migrations}/0001_01_01_000002_create_jobs_table.php (100%) rename database/{migrations-playground => migrations}/2024_03_13_210031_create_personal_access_tokens_table.php (100%) create mode 100644 tests/Unit/PackageProviders.php diff --git a/README.md b/README.md index c9e0ff7..9ee3944 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,66 @@ Playground provides information in the `artisan about` command. screenshot of artisan about command with Playground. +### Environment Variables + +| env() | config() | +|-------------------------------------|-------------------------------------| +| `PLAYGROUND_LOAD_MIGRATIONS` | `playground.load.migrations` | +- The loading option for migrations does not take effect if the migrations have been exported to your app. The control for loading is handled in the package [ServiceProvider.](src/ServiceProvider.php) + + ## Migrations The migrations provided in this package are used for [PHPunit 11](https://docs.phpunit.de/en/11.0/) feature testing with [Orchestra Testbench](https://packages.tools/testbench.html). - They will not be exported in software builds. +## Cloc + +```sh +composer cloc +``` + +``` +➜ playground git:(develop) ✗ composer cloc +> cloc --exclude-dir=output,vendor . + 98 text files. + 62 unique files. + 38 files ignored. + +github.com/AlDanial/cloc v 1.98 T=0.11 s (582.6 files/s, 57515.8 lines/s) +------------------------------------------------------------------------------- +Language files blank comment code +------------------------------------------------------------------------------- +PHP 52 921 856 3591 +YAML 1 5 0 275 +XML 3 0 9 222 +JSON 2 0 0 103 +Markdown 3 42 0 82 +INI 1 3 0 12 +------------------------------------------------------------------------------- +SUM: 62 971 865 4285 +------------------------------------------------------------------------------- +``` + +## PHPStan + +Tests at level 9 on: +- `config/` +- `database/` +- `src/` +- `tests/Feature/` +- `tests/Unit/` + +```sh +composer analyse +``` + +## Coding Standards + +```sh +composer format +``` + ## Testing ```sh diff --git a/composer.json b/composer.json index e08891d..2e0072f 100644 --- a/composer.json +++ b/composer.json @@ -66,8 +66,9 @@ } }, "scripts": { - "test": "vendor/bin/testbench package:test", + "analyse": "vendor/bin/phpstan analyse --verbose --debug --level max", + "cloc": "cloc --exclude-dir=output,vendor .", "format": "vendor/bin/php-cs-fixer fix", - "analyse": "vendor/bin/phpstan analyse --verbose --debug --level max" + "test": "vendor/bin/phpunit" } } diff --git a/config/playground.php b/config/playground.php index ef8b4a7..7c55df2 100644 --- a/config/playground.php +++ b/config/playground.php @@ -6,6 +6,19 @@ 'about' => (bool) env('PLAYGROUND_ABOUT', true), + /* + |-------------------------------------------------------------------------- + | Loading + |-------------------------------------------------------------------------- + | + | By default, migrations are disabled. + | + */ + + 'load' => [ + 'migrations' => (bool) env('PLAYGROUND_LOAD_MIGRATIONS', false), + ], + /* |-------------------------------------------------------------------------- | Packages diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 44046f2..1e18e38 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -69,4 +69,64 @@ public function unverified(): static 'email_verified_at' => null, ]); } + + /** + * Indicate that the user has the admin role. + * + * @return Factory + */ + public function admin(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'admin', + ]); + } + + /** + * Indicate that the user has the guest role. + * + * @return Factory + */ + public function guest(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'guest', + ]); + } + + /** + * Indicate that the user has the manager role. + * + * @return Factory + */ + public function manager(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'manager', + ]); + } + + /** + * Indicate that the user has the root role. + * + * @return Factory + */ + public function root(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'root', + ]); + } + + /** + * Indicate that the user has the wheel role. + * + * @return Factory + */ + public function wheel(): Factory + { + return $this->state(fn (array $attributes) => [ + 'role' => 'wheel', + ]); + } } diff --git a/database/migrations-laravel/0001_01_01_000000_create_users_table.php b/database/migrations-laravel/0001_01_01_000000_create_users_table.php deleted file mode 100644 index 5ef79e4..0000000 --- a/database/migrations-laravel/0001_01_01_000000_create_users_table.php +++ /dev/null @@ -1,51 +0,0 @@ -id(); - $table->string('name'); - $table->string('email')->unique(); - $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); - $table->rememberToken(); - $table->timestamps(); - }); - - Schema::create('password_reset_tokens', function (Blueprint $table) { - $table->string('email')->primary(); - $table->string('token'); - $table->timestamp('created_at')->nullable(); - }); - - Schema::create('sessions', function (Blueprint $table) { - $table->string('id')->primary(); - $table->foreignId('user_id')->nullable()->index(); - $table->string('ip_address', 45)->nullable(); - $table->text('user_agent')->nullable(); - $table->longText('payload'); - $table->integer('last_activity')->index(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('users'); - Schema::dropIfExists('password_reset_tokens'); - Schema::dropIfExists('sessions'); - } -}; diff --git a/database/migrations-laravel/2024_03_13_210031_create_personal_access_tokens_table.php b/database/migrations-laravel/2024_03_13_210031_create_personal_access_tokens_table.php deleted file mode 100644 index 8dd13c9..0000000 --- a/database/migrations-laravel/2024_03_13_210031_create_personal_access_tokens_table.php +++ /dev/null @@ -1,35 +0,0 @@ -id(); - $table->morphs('tokenable'); - $table->string('name'); - $table->string('token', 64)->unique(); - $table->text('abilities')->nullable(); - $table->timestamp('last_used_at')->nullable(); - $table->timestamp('expires_at')->nullable(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('personal_access_tokens'); - } -}; diff --git a/database/migrations-playground/0001_01_01_000001_create_cache_table.php b/database/migrations-playground/0001_01_01_000001_create_cache_table.php deleted file mode 100644 index 960e12b..0000000 --- a/database/migrations-playground/0001_01_01_000001_create_cache_table.php +++ /dev/null @@ -1,37 +0,0 @@ -string('key')->primary(); - $table->mediumText('value'); - $table->integer('expiration'); - }); - - Schema::create('cache_locks', function (Blueprint $table) { - $table->string('key')->primary(); - $table->string('owner'); - $table->integer('expiration'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('cache'); - Schema::dropIfExists('cache_locks'); - } -}; diff --git a/database/migrations-playground/0001_01_01_000002_create_jobs_table.php b/database/migrations-playground/0001_01_01_000002_create_jobs_table.php deleted file mode 100644 index 0dcb8c4..0000000 --- a/database/migrations-playground/0001_01_01_000002_create_jobs_table.php +++ /dev/null @@ -1,59 +0,0 @@ -id(); - $table->string('queue')->index(); - $table->longText('payload'); - $table->unsignedTinyInteger('attempts'); - $table->unsignedInteger('reserved_at')->nullable(); - $table->unsignedInteger('available_at'); - $table->unsignedInteger('created_at'); - }); - - Schema::create('job_batches', function (Blueprint $table) { - $table->string('id')->primary(); - $table->string('name'); - $table->integer('total_jobs'); - $table->integer('pending_jobs'); - $table->integer('failed_jobs'); - $table->longText('failed_job_ids'); - $table->mediumText('options')->nullable(); - $table->integer('cancelled_at')->nullable(); - $table->integer('created_at'); - $table->integer('finished_at')->nullable(); - }); - - Schema::create('failed_jobs', function (Blueprint $table) { - $table->id(); - $table->string('uuid')->unique(); - $table->text('connection'); - $table->text('queue'); - $table->longText('payload'); - $table->longText('exception'); - $table->timestamp('failed_at')->useCurrent(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('jobs'); - Schema::dropIfExists('job_batches'); - Schema::dropIfExists('failed_jobs'); - } -}; diff --git a/database/migrations-playground/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php similarity index 100% rename from database/migrations-playground/0001_01_01_000000_create_users_table.php rename to database/migrations/0001_01_01_000000_create_users_table.php diff --git a/database/migrations-laravel/0001_01_01_000001_create_cache_table.php b/database/migrations/0001_01_01_000001_create_cache_table.php similarity index 100% rename from database/migrations-laravel/0001_01_01_000001_create_cache_table.php rename to database/migrations/0001_01_01_000001_create_cache_table.php diff --git a/database/migrations-laravel/0001_01_01_000002_create_jobs_table.php b/database/migrations/0001_01_01_000002_create_jobs_table.php similarity index 100% rename from database/migrations-laravel/0001_01_01_000002_create_jobs_table.php rename to database/migrations/0001_01_01_000002_create_jobs_table.php diff --git a/database/migrations-playground/2024_03_13_210031_create_personal_access_tokens_table.php b/database/migrations/2024_03_13_210031_create_personal_access_tokens_table.php similarity index 100% rename from database/migrations-playground/2024_03_13_210031_create_personal_access_tokens_table.php rename to database/migrations/2024_03_13_210031_create_personal_access_tokens_table.php diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index da65199..abd8c91 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -25,16 +25,43 @@ public function boot(): void */ $config = config($this->package); - if ($this->app->runningInConsole()) { - // Publish configuration - $this->publishes([ - sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package) => config_path(sprintf('%1$s.php', $this->package)), - ], 'playground-config'); + if (! empty($config['load']) && is_array($config['load'])) { + + if ($this->app->runningInConsole()) { + // Publish configuration + $this->publishes([ + sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package) => config_path(sprintf('%1$s.php', $this->package)), + ], 'playground-config'); + + // Publish migrations + $this->publishMigrations(); + + // Load migrations + if (! empty($config['load']['migrations'])) { + $this->loadMigrationsFrom(dirname(__DIR__).'/database/migrations'); + } + } + + if (! empty($config['about'])) { + $this->about($config); + } } + } - if (! empty($config['about'])) { - $this->about($config); + public function publishMigrations(): void + { + $migrations = []; + + foreach ([ + '0001_01_01_000000_create_users_table.php', + '0001_01_01_000001_create_cache_table.php', + '0001_01_01_000002_create_jobs_table.php', + '2024_03_13_210031_create_personal_access_tokens_table.php', + ] as $file) { + $migrations[dirname(__DIR__).'/database/migrations/'.$file] = database_path('migrations/'.$file); } + + $this->publishes($migrations, 'playground-migrations'); } /** @@ -44,8 +71,6 @@ public function about(array $config): void { $packages = ! empty($config['packages']) && is_array($config['packages']) ? $config['packages'] : []; - $version = $this->version(); - /** * @var class-string $auth_providers_users_model */ @@ -86,7 +111,7 @@ public function about(array $config): void 'Packages' => implode(', ', $packages), 'Package' => $this->package, - 'Version' => $version, + 'Version' => ServiceProvider::VERSION, ]); } @@ -209,9 +234,4 @@ public function register(): void $this->package ); } - - public function version(): string - { - return static::VERSION; - } } diff --git a/tests/Feature/Console/Commands/About/CommandTest.php b/tests/Feature/Console/Commands/About/CommandTest.php index 5debecc..ab1117c 100644 --- a/tests/Feature/Console/Commands/About/CommandTest.php +++ b/tests/Feature/Console/Commands/About/CommandTest.php @@ -16,6 +16,19 @@ #[CoversClass(ServiceProvider::class)] class CommandTest extends TestCase { + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function defineEnvironment($app) + { + parent::defineEnvironment($app); + + $app['config']->set('playground.load.migrations', true); + } + public function test_command_about_displays_package_information_and_succeed_with_code_0(): void { /** diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index dce32c4..e4b1bee 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -6,42 +6,24 @@ */ namespace Tests\Feature\Playground; +use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\Unit\Playground\PackageProviders; + /** * \Tests\Feature\Playground\TestCase */ class TestCase extends \Tests\Unit\Playground\TestCase { + use DatabaseTransactions; + use PackageProviders; + + protected bool $hasMigrations = true; + protected bool $load_migrations_laravel = false; + protected bool $load_migrations_package = false; + protected bool $load_migrations_playground = false; - /** - * Setup the test environment. - */ - protected function setUp(): void - { - parent::setUp(); - - if (! empty(env('TEST_DB_MIGRATIONS'))) { - if ($this->load_migrations_laravel) { - $this->loadMigrationsFrom(dirname(dirname(__DIR__)).'/database/migrations-laravel'); - } - if ($this->load_migrations_playground) { - $this->loadMigrationsFrom(dirname(dirname(__DIR__)).'/database/migrations-playground'); - } - } - } - - /** - * Set up the environment. - * - * @param \Illuminate\Foundation\Application $app - */ - protected function getEnvironmentSetUp($app) - { - $app['config']->set('auth.providers.users.model', 'Playground\\Test\\Models\\User'); - $app['config']->set('playground-auth.verify', 'user'); - $app['config']->set('auth.testing.password', 'password'); - $app['config']->set('auth.testing.hashed', false); - } + protected bool $setUpUserForPlayground = false; } diff --git a/tests/Unit/PackageProviders.php b/tests/Unit/PackageProviders.php new file mode 100644 index 0000000..a9d312a --- /dev/null +++ b/tests/Unit/PackageProviders.php @@ -0,0 +1,22 @@ +newInstanceWithoutConstructor(); - $this->assertNotEmpty(ServiceProvider::VERSION); $this->assertIsString(ServiceProvider::VERSION); - $this->assertSame(ServiceProvider::VERSION, $instance->version()); } public function test_userPrimaryKeyType_with_empty_model(): void diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index 063d5ae..03c9a5e 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -6,8 +6,6 @@ */ namespace Tests\Unit\Playground; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Playground\ServiceProvider; use Playground\Test\OrchestraTestCase; /** @@ -15,25 +13,5 @@ */ class TestCase extends OrchestraTestCase { - use DatabaseTransactions; - - protected function getPackageProviders($app) - { - return [ - ServiceProvider::class, - ]; - } - - /** - * Set up the environment. - * - * @param \Illuminate\Foundation\Application $app - */ - protected function getEnvironmentSetUp($app) - { - $app['config']->set('auth.providers.users.model', 'Playground\\Test\\Models\\User'); - $app['config']->set('playground-auth.verify', 'user'); - $app['config']->set('auth.testing.password', 'password'); - $app['config']->set('auth.testing.hashed', false); - } + use PackageProviders; }