Skip to content

Commit

Permalink
Merge pull request #70 from Temepest74/fixes-foreign-key
Browse files Browse the repository at this point in the history
Fixes foreign key
  • Loading branch information
cjmellor authored Dec 4, 2024
2 parents c09d09a + f317d16 commit a3bb529
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
17 changes: 11 additions & 6 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public function extend(Builder $builder): void
*/
protected function addWithAnyState(Builder $builder): void
{
$builder->macro('withAnyState', fn (Builder $builder): Builder => $builder->withoutGlobalScope($this));
$builder->macro('withAnyState', fn(Builder $builder): Builder => $builder->withoutGlobalScope($this));
}

/**
* Return only Approval states that are set to 'approved'.
*/
protected function addApproved(Builder $builder): void
{
$builder->macro('approved', fn (Builder $builder): Builder => $builder
$builder->macro('approved', fn(Builder $builder): Builder => $builder
->withAnyState()
->where(column: 'state', operator: ApprovalStatus::Approved));
}
Expand All @@ -72,7 +72,7 @@ protected function addApproved(Builder $builder): void
*/
protected function addPending(Builder $builder): void
{
$builder->macro('pending', fn (Builder $builder): Builder => $builder
$builder->macro('pending', fn(Builder $builder): Builder => $builder
->withAnyState()
->where(column: 'state', operator: ApprovalStatus::Pending));
}
Expand All @@ -82,7 +82,7 @@ protected function addPending(Builder $builder): void
*/
protected function addRejected(Builder $builder): void
{
$builder->macro('rejected', fn (Builder $builder): Builder => $builder
$builder->macro('rejected', fn(Builder $builder): Builder => $builder
->withAnyState()
->where(column: 'state', operator: ApprovalStatus::Rejected));
}
Expand All @@ -107,6 +107,11 @@ protected function addApprove(Builder $builder): void

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

$foreignKey = $builder->getModel()->foreign_key;
if ($foreignKey) {
$newData[$model->getApprovalForeignKeyName()] = $foreignKey;
}

// make sure we cast all attributes
foreach ($newData as $key => $value) {
$newData[$key] = $model->callCastAttribute($key, $value);
Expand Down Expand Up @@ -148,14 +153,14 @@ protected function updateApprovalState(Builder $builder, ApprovalStatus $state):
*/
protected function addPostpone(Builder $builder): void
{
$builder->macro('postpone', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Pending));
$builder->macro('postpone', fn(Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Pending));
}

/**
* Set the state as 'rejected'
*/
protected function addReject(Builder $builder): void
{
$builder->macro('reject', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected));
$builder->macro('reject', fn(Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected));
}
}
45 changes: 23 additions & 22 deletions tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Cjmellor\Approval\Events\ModelSetPending;
use Cjmellor\Approval\Models\Approval;
use Cjmellor\Approval\Tests\Models\FakeModel;
use Cjmellor\Approval\Tests\Models\FakeUser;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;

test('Check if an Approval Model is approved', closure: function (): void {
$this->approvalData = [
Expand Down Expand Up @@ -202,36 +202,37 @@
});

test(description: 'The model approver is listed correctly', closure: function () {
Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) {
$table->id();
$table->string(column: 'name');
$table->string(column: 'email')->unique();
$table->string('password');
});
$user = FakeUser::create($this->fakeUserData);

class FakeUser extends \Illuminate\Foundation\Auth\User
{
protected $guarded = [];
$this->be($user);

FakeModel::create($this->fakeModelData);

protected $table = 'fake_users';
$approval = Approval::first();
$approval->approve();

public $timestamps = false;
}
expect($approval)->fresh()->audited_by->toBe(expected: $user->id);
});

$user = FakeUser::create([
'name' => 'Chris Mellor',
'email' => 'chris@mellor.pizza',
'password' => 'password',
]);
test(description: 'The model foreign key is set correctly', closure: function () {
$user = FakeUser::create($this->fakeUserData);

$this->be($user);

FakeModel::create($this->fakeModelData);
$fakeModelData = [
...$this->fakeModelData,
'user_id' => $user->id
];

FakeModel::create($fakeModelData);

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

expect($approval)->fresh()->audited_by->toBe(expected: $user->id);

Schema::dropIfExists('fake_users');
$this->assertDatabaseHas(
'fake_models',
[
'user_id' => $user->id
]
);
});
12 changes: 12 additions & 0 deletions tests/Models/FakeUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Cjmellor\Approval\Tests\Models;

class FakeUser extends \Illuminate\Foundation\Auth\User
{
protected $guarded = [];

protected $table = 'fake_users';

public $timestamps = false;
}
6 changes: 6 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
'name' => 'Chris',
'meta' => 'red',
];

$this->fakeUserData = [
'name' => 'Chris Mellor',
'email' => 'chris@mellor.pizza',
'password' => 'password',
];
})
->in(__DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) {
$table->id();
$table->string(column: 'name');
$table->string(column: 'email')->unique();
$table->string('password');
});
}
};

0 comments on commit a3bb529

Please sign in to comment.