|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Traits; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Database\Schema\Blueprint; |
| 7 | +use Illuminate\Support\Facades\Config; |
| 8 | +use Illuminate\Support\Facades\Schema; |
| 9 | +use LaravelReady\ModelSupport\Traits\HasActive; |
| 10 | + |
| 11 | +beforeEach(function () { |
| 12 | + // Create a test table |
| 13 | + Schema::create('test_active_models', function (Blueprint $table) { |
| 14 | + $table->id(); |
| 15 | + $table->boolean('is_active')->default(true); |
| 16 | + $table->string('name'); |
| 17 | + $table->timestamps(); |
| 18 | + }); |
| 19 | + |
| 20 | + // Create test model class |
| 21 | + $this->modelClass = new class extends Model { |
| 22 | + use HasActive; |
| 23 | + |
| 24 | + protected $table = 'test_active_models'; |
| 25 | + protected $fillable = ['name']; |
| 26 | + }; |
| 27 | + |
| 28 | + // Create test data |
| 29 | + $this->modelClass::create(['is_active' => true, 'name' => 'Active Item 1']); |
| 30 | + $this->modelClass::create(['is_active' => true, 'name' => 'Active Item 2']); |
| 31 | + $this->modelClass::create(['is_active' => false, 'name' => 'Inactive Item 1']); |
| 32 | + $this->modelClass::create(['is_active' => false, 'name' => 'Inactive Item 2']); |
| 33 | +}); |
| 34 | + |
| 35 | +afterEach(function () { |
| 36 | + Schema::dropIfExists('test_active_models'); |
| 37 | +}); |
| 38 | + |
| 39 | +test('initializeHasActive adds is_active to fillable', function () { |
| 40 | + $model = $this->modelClass::create(['is_active' => true, 'name' => 'Test']); |
| 41 | + |
| 42 | + expect($model->getFillable())->toContain('is_active'); |
| 43 | +}); |
| 44 | + |
| 45 | +test('initializeHasActive adds is_active to casts as boolean', function () { |
| 46 | + $model = $this->modelClass::create(['is_active' => true, 'name' => 'Test']); |
| 47 | + |
| 48 | + expect($model->getCasts())->toHaveKey('is_active') |
| 49 | + ->and($model->getCasts()['is_active'])->toBe('boolean'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('scopeActive returns only active items', function () { |
| 53 | + $results = $this->modelClass::active()->get(); |
| 54 | + |
| 55 | + expect($results)->toHaveCount(2) |
| 56 | + ->and($results->every(fn($item) => $item->is_active === true))->toBeTrue(); |
| 57 | +}); |
| 58 | + |
| 59 | +test('scopeInactive returns only inactive items', function () { |
| 60 | + $results = $this->modelClass::inactive()->get(); |
| 61 | + |
| 62 | + expect($results)->toHaveCount(2) |
| 63 | + ->and($results->every(fn($item) => $item->is_active === false))->toBeTrue(); |
| 64 | +}); |
| 65 | + |
| 66 | +test('scopeStatus filters by status true', function () { |
| 67 | + $results = $this->modelClass::status(true)->get(); |
| 68 | + |
| 69 | + expect($results)->toHaveCount(2) |
| 70 | + ->and($results->every(fn($item) => $item->is_active === true))->toBeTrue(); |
| 71 | +}); |
| 72 | + |
| 73 | +test('scopeStatus filters by status false', function () { |
| 74 | + $results = $this->modelClass::status(false)->get(); |
| 75 | + |
| 76 | + expect($results)->toHaveCount(2) |
| 77 | + ->and($results->every(fn($item) => $item->is_active === false))->toBeTrue(); |
| 78 | +}); |
| 79 | + |
| 80 | +test('is_active field is properly cast to boolean', function () { |
| 81 | + $model = $this->modelClass::create(['is_active' => 1, 'name' => 'Test']); |
| 82 | + |
| 83 | + expect($model->is_active)->toBeTrue() |
| 84 | + ->and($model->is_active)->toBeBool(); |
| 85 | + |
| 86 | + $model = $this->modelClass::create(['is_active' => 0, 'name' => 'Test 2']); |
| 87 | + |
| 88 | + expect($model->is_active)->toBeFalse() |
| 89 | + ->and($model->is_active)->toBeBool(); |
| 90 | +}); |
| 91 | + |
| 92 | +test('scopeActive respects custom is_active field from config', function () { |
| 93 | + Config::set('has_active.is_active', 'status'); |
| 94 | + |
| 95 | + Schema::create('test_custom_active_models', function (Blueprint $table) { |
| 96 | + $table->id(); |
| 97 | + $table->boolean('status')->default(true); |
| 98 | + $table->string('name'); |
| 99 | + $table->timestamps(); |
| 100 | + }); |
| 101 | + |
| 102 | + $customModel = new class extends Model { |
| 103 | + use HasActive; |
| 104 | + |
| 105 | + protected $table = 'test_custom_active_models'; |
| 106 | + protected $fillable = ['name']; |
| 107 | + }; |
| 108 | + |
| 109 | + $customModel::create(['status' => true, 'name' => 'Active']); |
| 110 | + $customModel::create(['status' => false, 'name' => 'Inactive']); |
| 111 | + |
| 112 | + $results = $customModel::active()->get(); |
| 113 | + |
| 114 | + expect($results)->toHaveCount(1) |
| 115 | + ->and($results->first()->status)->toBeTrue(); |
| 116 | + |
| 117 | + Schema::dropIfExists('test_custom_active_models'); |
| 118 | + Config::set('has_active.is_active', 'is_active'); |
| 119 | +}); |
| 120 | + |
| 121 | +test('scopes can be chained with other query methods', function () { |
| 122 | + $this->modelClass::create(['is_active' => true, 'name' => 'Active Special']); |
| 123 | + |
| 124 | + $results = $this->modelClass::active() |
| 125 | + ->where('name', 'like', '%Special%') |
| 126 | + ->get(); |
| 127 | + |
| 128 | + expect($results)->toHaveCount(1) |
| 129 | + ->and($results->first()->name)->toBe('Active Special'); |
| 130 | +}); |
0 commit comments