Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/Unit/Foundation/Models/Scopes/ActiveScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
use Workbench\App\Models\Product;

test('active scope filters inactive records', function () {
Product::factory()->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);
});
32 changes: 32 additions & 0 deletions workbench/app/Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Workbench\App\Models;

use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Workbench\Database\Factories\ProductFactory;

#[ScopedBy([ActiveScope::class])]
class Product extends Model
{
use HasFactory;

protected $fillable = [
'name',
'is_active',
];

protected function casts(): array
{
return [
'is_active' => 'boolean',
];
}

protected static function newFactory(): ProductFactory
{
return ProductFactory::new();
}
}
26 changes: 26 additions & 0 deletions workbench/database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Workbench\App\Models\Product;

class ProductFactory extends Factory
{
protected $model = Product::class;

public function definition(): array
{
return [
'name' => fake()->word(),
'is_active' => true,
];
}

public function inactive(): static
{
return $this->state(fn (array $attributes) => [
'is_active' => false,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('products');
}
};