Skip to content

Commit

Permalink
Merge pull request #25
Browse files Browse the repository at this point in the history
fix: Rejected and Postponed State Updating
  • Loading branch information
cjmellor authored Apr 4, 2023
2 parents 1ac254a + c797548 commit 8bdfe48
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
34 changes: 16 additions & 18 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@ class ApprovalStateScope implements Scope
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model)
public function apply(Builder $builder, Model $model): void
{
$builder->withAnyState();
}

/**
* Extend the query builder with the needed functions.
*/
public function extend(Builder $builder)
public function extend(Builder $builder): void
{
foreach ($this->extensions as $extension) {
$this->{"add{$extension}"}($builder);
$this->{"add$extension"}($builder);
}
}

/**
* Return all query results with no state.
*/
protected function addWithAnyState(Builder $builder)
protected function addWithAnyState(Builder $builder): void
{
$builder->macro('withAnyState', fn (Builder $builder): Builder => $builder->withoutGlobalScope($this));
}

/**
* Return only Approval states that are set to 'approved'.
*/
protected function addApproved(Builder $builder)
protected function addApproved(Builder $builder): void
{
$builder->macro('approved', fn (Builder $builder): Builder => $builder
->withAnyState()
Expand All @@ -64,7 +64,7 @@ protected function addApproved(Builder $builder)
/**
* Return only Approval states that are set to 'pending'.
*/
protected function addPending(Builder $builder)
protected function addPending(Builder $builder): void
{
$builder->macro('pending', fn (Builder $builder): Builder => $builder
->withAnyState()
Expand All @@ -74,7 +74,7 @@ protected function addPending(Builder $builder)
/**
* Return only Approval states that are set to 'rejected'.
*/
protected function addRejected(Builder $builder)
protected function addRejected(Builder $builder): void
{
$builder->macro('rejected', fn (Builder $builder): Builder => $builder
->withAnyState()
Expand All @@ -84,7 +84,7 @@ protected function addRejected(Builder $builder)
/**
* Set state as 'approved'.
*/
protected function addApprove(Builder $builder)
protected function addApprove(Builder $builder): void
{
$builder->macro('approve', function (Builder $builder, bool $persist = true): int {
if ($persist) {
Expand All @@ -103,26 +103,22 @@ protected function addApprove(Builder $builder)
$model->withoutApproval()->save();
}

return $builder
->find(id: $builder->getModel()->id)
->update([
'state' => ApprovalStatus::Approved,
]);
return $this->updateApprovalState($builder, state: ApprovalStatus::Approved);
});
}

/**
* Set state as 'pending' (default).
*/
protected function addPostpone(Builder $builder)
protected function addPostpone(Builder $builder): void
{
$builder->macro('postpone', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Pending));
}

/**
* Set state as 'rejected'
*/
protected function addReject(Builder $builder)
protected function addReject(Builder $builder): void
{
$builder->macro('reject', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected));
}
Expand All @@ -132,8 +128,10 @@ protected function addReject(Builder $builder)
*/
protected function updateApprovalState(Builder $builder, $state): int
{
return $builder->update([
'state' => $state,
]);
return $builder
->find(id: $builder->getModel()->id)
->update([
'state' => $state,
]);
}
}
40 changes: 36 additions & 4 deletions tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,40 @@
$approval = Approval::first();
$approval->approve();

$this->assertDatabaseHas(table: 'fake_models', data: [
'name' => 'Chris',
'meta' => 'red',
]);
expect($approval)->fresh()->state->toBe(ApprovalStatus::Approved);

$this->assertDatabaseHas(table: 'fake_models', data: $this->fakeModelData);
});

test(description: 'A Model can be Rejected', closure: function (): void {
FakeModel::create($this->fakeModelData);

$approval = Approval::first();
$approval->reject();

expect($approval)->fresh()->state->toBe(ApprovalStatus::Rejected);

$this->assertDatabaseMissing(table: 'fake_models', data: $this->fakeModelData);
});

test(description: 'A Model can be Postponed', closure: function (): void {
FakeModel::create($this->fakeModelData);

$approval = Approval::first();
$approval->postpone();

expect($approval)->fresh()->state->toBe(ApprovalStatus::Pending);

$this->assertDatabaseMissing(table: 'fake_models', data: $this->fakeModelData);
});

it(description: 'only changes the status of the requested model', closure: function () {
FakeModel::create($this->fakeModelData);
FakeModel::create(['name' => 'Bob', 'meta' => 'green']);

$modelOneApproval = Approval::first();
$modelOneApproval->approve();

expect($modelOneApproval)->fresh()->state->toBe(expected: ApprovalStatus::Approved)
->and(Approval::find(id: 2))->state->toBe(expected: ApprovalStatus::Pending);
});

0 comments on commit 8bdfe48

Please sign in to comment.