Skip to content

Commit

Permalink
Merge pull request #33 from cjmellor/add-events
Browse files Browse the repository at this point in the history
Add Events
  • Loading branch information
cjmellor authored Aug 20, 2023
2 parents 26d273d + 6b0b9fc commit 1fc8a93
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 102 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/run-pest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Run Pest"

on: pull_request

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [ 8.1, 8.2 ]
stability: [ prefer-lowest, prefer-stable ]

name: "PHP: v${{ matrix.php }} [${{ matrix.stability }}]"

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/pest
54 changes: 0 additions & 54 deletions .github/workflows/run-tests.yml

This file was deleted.

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ Approval::where('id', 3)->postpone();

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

### Events

Once a Model's state has been changed, an event will be fired.

```php
ModelApproved::class
ModelPostponed::class
ModelRejected::class
```

### Persisting data

By default, once you approve a Model, it will be inserted into the database.
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0",
"nunomaduro/collision": "^7.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpunit/phpunit": "^9.5"
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-type-coverage": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
37 changes: 6 additions & 31 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Cjmellor Test Suite">
<directory>tests</directory>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</source>
</phpunit>
39 changes: 39 additions & 0 deletions phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="Cjmellor Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
14 changes: 14 additions & 0 deletions src/Events/ModelApproved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Cjmellor\Approval\Events;

use Illuminate\Foundation\Events\Dispatchable;

class ModelApproved
{
use Dispatchable;

public function __construct()
{
}
}
14 changes: 14 additions & 0 deletions src/Events/ModelRejected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Cjmellor\Approval\Events;

use Illuminate\Foundation\Events\Dispatchable;

class ModelRejected
{
use Dispatchable;

public function __construct()
{
}
}
14 changes: 14 additions & 0 deletions src/Events/ModelSetPending.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Cjmellor\Approval\Events;

use Illuminate\Foundation\Events\Dispatchable;

class ModelSetPending
{
use Dispatchable;

public function __construct()
{
}
}
2 changes: 1 addition & 1 deletion src/Models/Approval.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Approval extends Model
'state' => ApprovalStatus::class,
];

public static function booted()
public static function booted(): void
{
static::addGlobalScope(new ApprovalStateScope());
}
Expand Down
34 changes: 22 additions & 12 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Cjmellor\Approval\Scopes;

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Events\ModelApproved;
use Cjmellor\Approval\Events\ModelRejected;
use Cjmellor\Approval\Events\ModelSetPending;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Event;

class ApprovalStateScope implements Scope
{
Expand Down Expand Up @@ -107,6 +111,24 @@ protected function addApprove(Builder $builder): void
});
}

/**
* A helper method for updating the approvals state.
*/
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()),
};

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

/**
* Set state as 'pending' (default).
*/
Expand All @@ -122,16 +144,4 @@ protected function addReject(Builder $builder): void
{
$builder->macro('reject', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected));
}

/**
* A helper method for updating the approvals state.
*/
protected function updateApprovalState(Builder $builder, $state): int
{
return $builder
->find(id: $builder->getModel()->id)
->update([
'state' => $state,
]);
}
}
19 changes: 19 additions & 0 deletions tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Events\ModelApproved;
use Cjmellor\Approval\Events\ModelRejected;
use Cjmellor\Approval\Events\ModelSetPending;
use Cjmellor\Approval\Models\Approval;
use Cjmellor\Approval\Tests\Models\FakeModel;
use Illuminate\Support\Facades\Event;

beforeEach(closure: function (): void {
$this->fakeModelData = [
Expand Down Expand Up @@ -97,3 +101,18 @@
expect($modelOneApproval)->fresh()->state->toBe(expected: ApprovalStatus::Approved)
->and(Approval::find(id: 2))->state->toBe(expected: ApprovalStatus::Pending);
});

test(description: 'An event is fired when a Model\'s state is changed', closure: function (string $state): void {
FakeModel::create($this->fakeModelData);

Event::fake();

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

match ($state) {
'approve' => Event::assertDispatched(ModelApproved::class),
'reject' => Event::assertDispatched(ModelRejected::class),
'postpone' => Event::assertDispatched(ModelSetPending::class),
};
})->with(['approve', 'reject', 'postpone']);

0 comments on commit 1fc8a93

Please sign in to comment.