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'); + } +};