From 67a4855847fc8d7dddc675a76f41902c621628c7 Mon Sep 17 00:00:00 2001 From: thapacodes4u Date: Mon, 20 Oct 2025 05:26:11 +0545 Subject: [PATCH 1/3] feat: add ActiveScope test with Product model in workbench --- .../Models/Scopes/ActiveScopeTest.php | 34 +++++++++++++++++++ workbench/app/Models/Product.php | 32 +++++++++++++++++ .../database/factories/ProductFactory.php | 26 ++++++++++++++ ...025_10_08_051243_create_products_table.php | 23 +++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 tests/Unit/Foundation/Models/Scopes/ActiveScopeTest.php create mode 100644 workbench/app/Models/Product.php create mode 100644 workbench/database/factories/ProductFactory.php create mode 100644 workbench/database/migrations/2025_10_08_051243_create_products_table.php diff --git a/tests/Unit/Foundation/Models/Scopes/ActiveScopeTest.php b/tests/Unit/Foundation/Models/Scopes/ActiveScopeTest.php new file mode 100644 index 0000000..de8d96e --- /dev/null +++ b/tests/Unit/Foundation/Models/Scopes/ActiveScopeTest.php @@ -0,0 +1,34 @@ +create(['name' => 'Active Product', 'is_active' => true]); + Product::factory()->create(['name' => 'Inactive Product', 'is_active' => false]); + + expect(Product::count())->toBe(1) + ->and(Product::first()->name)->toBe('Active Product'); +}); + +test('active scope can be disabled with withoutGlobalScope', function () { + Product::factory()->create(['is_active' => true]); + Product::factory()->inactive()->create(); + + expect(Product::count())->toBe(1) + ->and(Product::withoutGlobalScope(ActiveScope::class)->count())->toBe(2); +}); + +test('active scope only returns active records by default', function () { + $activeProduct = Product::factory()->create(['is_active' => true]); + $inactiveProduct = Product::factory()->inactive()->create(); + + expect(Product::where('id', $activeProduct->id)->count())->toBe(1) + ->and(Product::where('id', $inactiveProduct->id)->count())->toBe(0); +}); + +test('active scope allows querying inactive records without scope', function () { + $inactiveProduct = Product::factory()->inactive()->create(); + + expect(Product::withoutGlobalScope(ActiveScope::class)->where('id', $inactiveProduct->id)->count())->toBe(1); +}); diff --git a/workbench/app/Models/Product.php b/workbench/app/Models/Product.php new file mode 100644 index 0000000..3fc0698 --- /dev/null +++ b/workbench/app/Models/Product.php @@ -0,0 +1,32 @@ + 'boolean', + ]; + } + + protected static function newFactory(): ProductFactory + { + return ProductFactory::new(); + } +} diff --git a/workbench/database/factories/ProductFactory.php b/workbench/database/factories/ProductFactory.php new file mode 100644 index 0000000..33291a8 --- /dev/null +++ b/workbench/database/factories/ProductFactory.php @@ -0,0 +1,26 @@ + fake()->word(), + 'is_active' => true, + ]; + } + + public function inactive(): static + { + return $this->state(fn (array $attributes) => [ + 'is_active' => false, + ]); + } +} diff --git a/workbench/database/migrations/2025_10_08_051243_create_products_table.php b/workbench/database/migrations/2025_10_08_051243_create_products_table.php new file mode 100644 index 0000000..6b47a42 --- /dev/null +++ b/workbench/database/migrations/2025_10_08_051243_create_products_table.php @@ -0,0 +1,23 @@ +id(); + $table->string('name'); + $table->boolean('is_active')->default(true); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('products'); + } +}; From 14be2c8afa9952543acf63478a54799596795e75 Mon Sep 17 00:00:00 2001 From: thapacodes4u Date: Mon, 20 Oct 2025 05:52:25 +0545 Subject: [PATCH 2/3] fix: change LocaleResourceTest to use C.UTF-8 for CI compatibility --- tests/Feature/Filament/Resources/LocaleResourceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Filament/Resources/LocaleResourceTest.php b/tests/Feature/Filament/Resources/LocaleResourceTest.php index 476c27b..0e8b87a 100644 --- a/tests/Feature/Filament/Resources/LocaleResourceTest.php +++ b/tests/Feature/Filament/Resources/LocaleResourceTest.php @@ -69,7 +69,7 @@ // Test with valid data $validData = Locale::factory()->definition(); - $validData['system_locale'] = 'en_US.UTF-8'; + $validData['system_locale'] = 'C.UTF-8'; $component->mountAction('create') ->setActionData($validData) ->callMountedAction() @@ -82,7 +82,7 @@ // Remove is_active and is_available_in_panel attributes, since they're not used when creating a locale unset($data['is_active']); unset($data['is_available_in_panel']); - $data['system_locale'] = 'en_US.UTF-8'; + $data['system_locale'] = 'C.UTF-8'; livewire(ListLocales::class) ->mountAction('create') @@ -102,7 +102,7 @@ $locale = Locale::factory()->create(); $new_data = Arr::except(Locale::factory()->definition(), ['id', 'is_active', 'is_available_in_panel']); - $new_data['system_locale'] = 'en_US.UTF-8'; + $new_data['system_locale'] = 'C.UTF-8'; livewire(ListLocales::class) ->callTableAction('edit', $locale, $new_data) From b86a770e4d486f351f3b448978f26c6b6894b027 Mon Sep 17 00:00:00 2001 From: thapacodes4u Date: Mon, 20 Oct 2025 06:04:03 +0545 Subject: [PATCH 3/3] fix: use 'C' locale instead of 'C.UTF-8' in LocaleResourceTest --- tests/Feature/Filament/Resources/LocaleResourceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Filament/Resources/LocaleResourceTest.php b/tests/Feature/Filament/Resources/LocaleResourceTest.php index 0e8b87a..5b187be 100644 --- a/tests/Feature/Filament/Resources/LocaleResourceTest.php +++ b/tests/Feature/Filament/Resources/LocaleResourceTest.php @@ -69,7 +69,7 @@ // Test with valid data $validData = Locale::factory()->definition(); - $validData['system_locale'] = 'C.UTF-8'; + $validData['system_locale'] = 'C'; $component->mountAction('create') ->setActionData($validData) ->callMountedAction() @@ -82,7 +82,7 @@ // Remove is_active and is_available_in_panel attributes, since they're not used when creating a locale unset($data['is_active']); unset($data['is_available_in_panel']); - $data['system_locale'] = 'C.UTF-8'; + $data['system_locale'] = 'C'; livewire(ListLocales::class) ->mountAction('create') @@ -102,7 +102,7 @@ $locale = Locale::factory()->create(); $new_data = Arr::except(Locale::factory()->definition(), ['id', 'is_active', 'is_available_in_panel']); - $new_data['system_locale'] = 'C.UTF-8'; + $new_data['system_locale'] = 'C'; livewire(ListLocales::class) ->callTableAction('edit', $locale, $new_data)