Skip to content

Commit

Permalink
Merge pull request #49 from nunodonato/main
Browse files Browse the repository at this point in the history
Add model and user to all event constructors
  • Loading branch information
cjmellor authored Feb 17, 2024
2 parents b716e4e + 2b0982d commit b484979
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/
Expand Down
1 change: 0 additions & 1 deletion src/Concerns/MustBeApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected static function insertApprovalRequest($model)
if ($noNeedToProceed) {
return false;
}

}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Events/ModelApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelApproved
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
8 changes: 6 additions & 2 deletions src/Events/ModelRejected.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelRejected
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
8 changes: 6 additions & 2 deletions src/Events/ModelSetPending.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelSetPending
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
6 changes: 3 additions & 3 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ protected function addApprove(Builder $builder): void
protected function updateApprovalState(Builder $builder, ApprovalStatus $state): int
{
match ($state) {
ApprovalStatus::Approved => Event::dispatch(new ModelApproved()),
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending()),
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected()),
ApprovalStatus::Approved => Event::dispatch(new ModelApproved($builder->getModel(), auth()->user())),
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending($builder->getModel(), auth()->user())),
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected($builder->getModel(), auth()->user())),
};

$auditedData = ['state' => $state];
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cjmellor\Approval\Models\Approval;
use Cjmellor\Approval\Tests\Models\FakeModel;
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 @@ -201,7 +202,7 @@
});

test(description: 'The model approver is listed correctly', closure: function () {
Schema::create('fake_users', callback: function (\Illuminate\Database\Schema\Blueprint $table) {
Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) {
$table->id();
$table->string(column: 'name');
$table->string(column: 'email')->unique();
Expand All @@ -211,7 +212,9 @@
class FakeUser extends \Illuminate\Foundation\Auth\User
{
protected $guarded = [];

protected $table = 'fake_users';

public $timestamps = false;
}

Expand Down

0 comments on commit b484979

Please sign in to comment.