Skip to content

Commit

Permalink
Merge pull request #47 from gerb-ster/bugfix_array-attributes
Browse files Browse the repository at this point in the history
Bugfix array attributes
  • Loading branch information
cjmellor committed Nov 13, 2023
2 parents d7a1300 + d623d43 commit c772e36
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Concerns/MustBeApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected static function insertApprovalRequest($model)
if ($noNeedToProceed) {
return false;
}
return;

}

/**
Expand Down Expand Up @@ -127,4 +127,20 @@ public function withoutApproval(): static

return $this;
}

/**
* Wrapper to access the castAttribute function
*
* @param $key
* @param $value
* @return mixed
*/
public function callCastAttribute($key, $value): mixed
{
if (array_key_exists($key, $this->casts)) {
return $this->castAttribute($key, $value);
}

return $value;
}
}
10 changes: 9 additions & 1 deletion src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ protected function addApprove(Builder $builder): void
$model = $model->find($modelId);
}

$model->forceFill($builder->getModel()->new_data->toArray());
$newData = $builder->getModel()->new_data->toArray();

// make sure we cast all attributes
foreach ($newData as $key => $value) {
$newData[$key] = $model->callCastAttribute($key, $value);
}

$model->forceFill($newData);

$model->withoutApproval()->save();
}

Expand Down
58 changes: 58 additions & 0 deletions tests/Feature/MustBeApprovedTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Cjmellor\Approval\Models\Approval;
use Cjmellor\Approval\Tests\Models\FakeModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

it(description: 'stores the data correctly in the database')
->defer(
Expand Down Expand Up @@ -166,3 +168,59 @@
'meta' => 'blue',
]);
});

test(description: 'approve a attribute of the type Array', closure: function () {
Schema::create('fake_models_with_array', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('meta')->nullable();

$table->json('data')->nullable();
});

$model = new class extends Model
{
use MustBeApproved;

protected $table = 'fake_models_with_array';

protected $guarded = [];

public $timestamps = false;

protected $casts = ['data' => 'array'];
};

// create a model
$model->create([
'name' => 'Neo',
'data' => ['foo', 'bar'],
]);

// check if the data is stored correctly in the approval table
$this->assertDatabaseHas(table: Approval::class, data: [
'new_data' => json_encode(['name' => 'Neo', 'data' => json_encode(['foo', 'bar'])]),
'original_data' => json_encode([]),
]);

// nothing should be in the 'fake_models_with_array' table
$this->assertDatabaseCount('fake_models_with_array', 0);

// approve the model
Approval::first()->approve();

// after approval, there should be in an entry in the 'fake_models_with_array' table
$this->assertDatabaseCount('fake_models_with_array', 1);

// After Approval, the contents of the database should look like this
$this->assertDatabaseHas(table: 'fake_models_with_array', data: [
'name' => 'Neo',
'data' => json_encode(['foo', 'bar']),
]);

// double check the model
$modelFromDatabase = $model->firstWhere('name', 'Neo');

expect($modelFromDatabase->data)
->toBe(['foo', 'bar']);
});

0 comments on commit c772e36

Please sign in to comment.