From 43f06b9df93dcadced3396aaf035cf473a1e27df Mon Sep 17 00:00:00 2001 From: cooldeark <45022034+cooldeark@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:50:55 +0800 Subject: [PATCH 1/5] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a2daae5..08c737c 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ "minimum-stability": "dev", "require": { "php": "^8.0", - "illuminate/support": "~6.0|~7.0|~8.0", - "illuminate/cache": "~6.0|~7.0|~8.0" + "illuminate/support": "~6.0|~7.0|~8.0|~9.0", + "illuminate/cache": "~6.0|~7.0|~8.0|~9.0" }, "require-dev": { "orchestra/database": "^4.0", From 911392613a2050da5860377781877194dd031e38 Mon Sep 17 00:00:00 2001 From: cooldeark <45022034+cooldeark@users.noreply.github.com> Date: Wed, 17 Apr 2024 22:17:14 +0800 Subject: [PATCH 2/5] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 08c737c..b6700ae 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ "minimum-stability": "dev", "require": { "php": "^8.0", - "illuminate/support": "~6.0|~7.0|~8.0|~9.0", - "illuminate/cache": "~6.0|~7.0|~8.0|~9.0" + "illuminate/support": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0", + "illuminate/cache": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0" }, "require-dev": { "orchestra/database": "^4.0", From 87232038d7d47039ff6213114fdba70b96c766cf Mon Sep 17 00:00:00 2001 From: cooldeark Date: Thu, 30 May 2024 19:43:05 +0800 Subject: [PATCH 3/5] fix test case & support laravel11 --- composer.json | 11 +++--- .../migrations/create_role_user_table.php | 14 ++++---- database/migrations/create_roles_table.php | 4 +-- database/migrations/create_user_table.php | 36 +++++++++++++++++++ tests/TestCase.php | 30 ++++++++++++---- 5 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 database/migrations/create_user_table.php diff --git a/composer.json b/composer.json index b6700ae..f8d3397 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,14 @@ "minimum-stability": "dev", "require": { "php": "^8.0", - "illuminate/support": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0", - "illuminate/cache": "~6.0|~7.0|~8.0|~9.0|~10.0|~11.0" + "illuminate/support": "~8.0|~9.0|~10.0|~11.0", + "illuminate/cache": "~8.0|~9.0|~10.0|~11.0" }, "require-dev": { - "orchestra/database": "^4.0", - "orchestra/testbench": "^4.0", - "phpunit/phpunit": "~8.0" + "orchestra/database": "^6.0", + "orchestra/testbench": "^6.0", + "phpunit/phpunit": "~8.5|^9.5.10", + "laravel/legacy-factories": "^1.0" }, "autoload": { "psr-4": { diff --git a/database/migrations/create_role_user_table.php b/database/migrations/create_role_user_table.php index cbf8817..1a19e66 100644 --- a/database/migrations/create_role_user_table.php +++ b/database/migrations/create_role_user_table.php @@ -14,11 +14,9 @@ class CreateRoleUserTable extends Migration public function up() { Schema::create('role_user', function (Blueprint $table) { - $table->increments('id'); - $table->integer('role_id')->unsigned()->index(); - $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); - $table->integer('user_id')->unsigned()->index(); - $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->id(); + $table->foreignId('role_id')->constrained('roles')->onDelete('cascade'); + $table->foreignId('user_id')->constrained('users')->onDelete('cascade'); $table->timestamps(); }); } @@ -31,9 +29,9 @@ public function up() public function down() { Schema::table('role_user', function (Blueprint $table) { - $table->dropForeign('role_user_role_id_foreign'); - $table->dropForeign('role_user_user_id_foreign'); + $table->dropForeign(['role_id']); + $table->dropForeign(['user_id']); }); Schema::dropIfExists('role_user'); } -} +} \ No newline at end of file diff --git a/database/migrations/create_roles_table.php b/database/migrations/create_roles_table.php index 72cf541..418607f 100644 --- a/database/migrations/create_roles_table.php +++ b/database/migrations/create_roles_table.php @@ -14,7 +14,7 @@ class CreateRolesTable extends Migration public function up() { Schema::create('roles', function (Blueprint $table) { - $table->increments('id'); + $table->id(); $table->string('name'); $table->string('slug')->unique(); $table->string('description')->nullable(); @@ -32,4 +32,4 @@ public function down() { Schema::dropIfExists('roles'); } -} +} \ No newline at end of file diff --git a/database/migrations/create_user_table.php b/database/migrations/create_user_table.php new file mode 100644 index 0000000..3226b09 --- /dev/null +++ b/database/migrations/create_user_table.php @@ -0,0 +1,36 @@ +id(); // 使用 Laravel 默认的 bigIncrements 类型 + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 4f94a22..36c7dcf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,18 +4,20 @@ use HttpOz\Roles\Tests\Stubs\User; use Orchestra\Testbench\TestCase as Orchestra; +use Illuminate\Foundation\Testing\RefreshDatabase; class TestCase extends Orchestra { + use RefreshDatabase; + /** * Setup the test environment. */ public function setUp(): void { parent::setUp(); - $this->loadLaravelMigrations(['--database' => 'testbench']); + $this->artisan('migrate:fresh'); // This will drop all tables and re-run all migrations $this->setUpDatabase($this->app); - $this->withFactories(__DIR__ . '/../database/factories'); } @@ -36,9 +38,16 @@ public function getEnvironmentSetUp($app) { $app['config']->set('database.default', 'testbench'); $app['config']->set('database.connections.testbench', [ - 'driver' => 'sqlite', - 'database' => ':memory:', + 'driver' => 'pgsql', + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '5432'), + 'database' => env('DB_DATABASE', 'test_db'), + 'username' => env('DB_USERNAME', 'trustanchor'), + 'password' => env('DB_PASSWORD', 'Yai3hahMaepi9uyo3Joh'), + 'charset' => 'utf8', 'prefix' => '', + 'schema' => 'public', + 'sslmode' => 'prefer', ]); $app['config']->set('auth.providers.users.model', User::class); @@ -68,9 +77,16 @@ public function getEnvironmentSetUp($app) */ protected function setUpDatabase($app) { - include_once __DIR__ . '/../database/migrations/create_roles_table.php'; - include_once __DIR__ . '/../database/migrations/create_role_user_table.php'; + // Include default Laravel migrations to ensure users table exists + $this->artisan('migrate', ['--database' => 'testbench']); + + // Include custom migrations + include_once __DIR__ . '/../database/migrations/create_user_table.php'; + include_once __DIR__ . '/../database/migrations/002.php'; + include_once __DIR__ . '/../database/migrations/003.php'; + + (new \CreateUsersTable())->up(); (new \CreateRolesTable())->up(); (new \CreateRoleUserTable())->up(); } -} +} \ No newline at end of file From ceffa19ac7fc5bb852452e70662476344f8ca4a4 Mon Sep 17 00:00:00 2001 From: cooldeark Date: Thu, 30 May 2024 19:44:57 +0800 Subject: [PATCH 4/5] fix --- tests/TestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 36c7dcf..81e4627 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -82,8 +82,8 @@ protected function setUpDatabase($app) // Include custom migrations include_once __DIR__ . '/../database/migrations/create_user_table.php'; - include_once __DIR__ . '/../database/migrations/002.php'; - include_once __DIR__ . '/../database/migrations/003.php'; + include_once __DIR__ . '/../database/migrations/create_roles_table.php'; + include_once __DIR__ . '/../database/migrations/create_role_user_table.php'; (new \CreateUsersTable())->up(); (new \CreateRolesTable())->up(); From cb78761db65df1fa1181a8d002b28f5be1733074 Mon Sep 17 00:00:00 2001 From: cooldeark Date: Thu, 30 May 2024 19:50:25 +0800 Subject: [PATCH 5/5] minor fix --- database/migrations/create_user_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/create_user_table.php b/database/migrations/create_user_table.php index 3226b09..6cdf0f0 100644 --- a/database/migrations/create_user_table.php +++ b/database/migrations/create_user_table.php @@ -14,7 +14,7 @@ class CreateUsersTable extends Migration public function up() { Schema::create('users', function (Blueprint $table) { - $table->id(); // 使用 Laravel 默认的 bigIncrements 类型 + $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable();