Skip to content

Commit b7286bd

Browse files
test: add ActiveScope test
* feat: add ActiveScope test with Product model in workbench --------- Co-authored-by: Omer Sabic <[email protected]>
1 parent 0523425 commit b7286bd

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
4+
use Workbench\App\Models\Product;
5+
6+
test('active scope filters inactive records', function () {
7+
Product::factory()->create(['name' => 'Active Product', 'is_active' => true]);
8+
Product::factory()->create(['name' => 'Inactive Product', 'is_active' => false]);
9+
10+
expect(Product::count())->toBe(1)
11+
->and(Product::first()->name)->toBe('Active Product');
12+
});
13+
14+
test('active scope can be disabled with withoutGlobalScope', function () {
15+
Product::factory()->create(['is_active' => true]);
16+
Product::factory()->inactive()->create();
17+
18+
expect(Product::count())->toBe(1)
19+
->and(Product::withoutGlobalScope(ActiveScope::class)->count())->toBe(2);
20+
});
21+
22+
test('active scope only returns active records by default', function () {
23+
$activeProduct = Product::factory()->create(['is_active' => true]);
24+
$inactiveProduct = Product::factory()->inactive()->create();
25+
26+
expect(Product::where('id', $activeProduct->id)->count())->toBe(1)
27+
->and(Product::where('id', $inactiveProduct->id)->count())->toBe(0);
28+
});
29+
30+
test('active scope allows querying inactive records without scope', function () {
31+
$inactiveProduct = Product::factory()->inactive()->create();
32+
33+
expect(Product::withoutGlobalScope(ActiveScope::class)->where('id', $inactiveProduct->id)->count())->toBe(1);
34+
});

workbench/app/Models/Product.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
6+
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Workbench\Database\Factories\ProductFactory;
10+
11+
#[ScopedBy([ActiveScope::class])]
12+
class Product extends Model
13+
{
14+
use HasFactory;
15+
16+
protected $fillable = [
17+
'name',
18+
'is_active',
19+
];
20+
21+
protected function casts(): array
22+
{
23+
return [
24+
'is_active' => 'boolean',
25+
];
26+
}
27+
28+
protected static function newFactory(): ProductFactory
29+
{
30+
return ProductFactory::new();
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Workbench\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Workbench\App\Models\Product;
7+
8+
class ProductFactory extends Factory
9+
{
10+
protected $model = Product::class;
11+
12+
public function definition(): array
13+
{
14+
return [
15+
'name' => fake()->word(),
16+
'is_active' => true,
17+
];
18+
}
19+
20+
public function inactive(): static
21+
{
22+
return $this->state(fn (array $attributes) => [
23+
'is_active' => false,
24+
]);
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('products', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name');
14+
$table->boolean('is_active')->default(true);
15+
$table->timestamps();
16+
});
17+
}
18+
19+
public function down(): void
20+
{
21+
Schema::dropIfExists('products');
22+
}
23+
};

0 commit comments

Comments
 (0)