Skip to content

Commit

Permalink
Merge pull request #34 from cjmellor/helper-methods
Browse files Browse the repository at this point in the history
Add conditional helper methods
  • Loading branch information
cjmellor authored Aug 22, 2023
2 parents 3195bba + 5683592 commit 26bb414
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The config allows you to change the polymorphic pivot name. It should end with `

## Usage

> This packages utilises Enums, so both PHP 8.1 and Laravel 9 must be used.
> The package utilises Enums, so both PHP 8.1 and Laravel 9 must be used.
>
> **Note** This package does not approve/deny the data for you, it just stores the new/amended data into the database. It is up to you to decide how you implement a function to approve or deny the Model.
Expand Down Expand Up @@ -97,7 +97,7 @@ The package comes with some helper methods for the Builder, utilising a custom s

By default, all queries to the `approvals` table will return all the Models' no matter the state.

There are three methods to help you retrieve the state of an Approval.
There are three methods to help you retrieve the state of the Approval.

```php
<?php
Expand All @@ -123,6 +123,20 @@ Approval::where('id', 3)->postpone();

In the event you need to reset a state, you can use the `withAnyState` helper.

### Helpers

Conditional helper methods are used, so you can set the state of an Approval when a condition is met.

```php
$approval->approveIf(true);
$approval->rejectIf(false);
$approval->postponeIf(true);

$approval->approveUnless(false);
$approval->rejectUnless(true);
$approval->postponeUnless(false);
```

### Events

Once a Model's state has been changed, an event will be fired.
Expand Down Expand Up @@ -171,4 +185,4 @@ Please open a PR with as much detail as possible about what you're trying to ach

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT Licence (MIT). Please see [Licence File](LICENSE.md) for more information.
42 changes: 42 additions & 0 deletions src/Models/Approval.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,46 @@ public function approvalable(): MorphTo
{
return $this->morphTo();
}

public function approveIf(bool $boolean): void
{
if ($boolean) {
$this->approve();
}
}

public function approveUnless(bool $boolean): void
{
if (! $boolean) {
$this->approve();
}
}

public function postponeIf(bool $boolean): void
{
if ($boolean) {
$this->postpone();
}
}

public function postponeUnless(bool $boolean): void
{
if (! $boolean) {
$this->postpone();
}
}

public function rejectIf(bool $boolean): void
{
if ($boolean) {
$this->reject();
}
}

public function rejectUnless(bool $boolean): void
{
if (! $boolean) {
$this->reject();
}
}
}
90 changes: 90 additions & 0 deletions tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,93 @@
'postpone' => Event::assertDispatched(ModelSetPending::class),
};
})->with(['approve', 'reject', 'postpone']);

test(description: 'A Model can be Approved if a condition is met', closure: function (): void {
FakeModel::create($this->fakeModelData);

Event::fake();

$approval = Approval::first();
$approval->approveIf(boolean: true);

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

Event::assertDispatched(event: ModelApproved::class);

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

test(description: 'A Model can be Approved unless a condition is met', closure: function (): void {
FakeModel::create($this->fakeModelData);

Event::fake();

$approval = Approval::first();
$approval->approveUnless(boolean: false);

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

Event::assertDispatched(event: ModelApproved::class);

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

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

Event::fake();

$approval = Approval::first();
$approval->rejectIf(boolean: true);

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

Event::assertDispatched(event: ModelRejected::class);

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

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

Event::fake();

$approval = Approval::first();
$approval->rejectUnless(boolean: false);

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

Event::assertDispatched(event: ModelRejected::class);

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

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

Event::fake();

$approval = Approval::first();
$approval->postponeIf(boolean: true);

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

Event::assertDispatched(event: ModelSetPending::class);

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

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

Event::fake();

$approval = Approval::first();
$approval->postponeUnless(boolean: false);

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

Event::assertDispatched(event: ModelSetPending::class);

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

0 comments on commit 26bb414

Please sign in to comment.