generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from cjmellor/feat/rollbacks
feat: Rollback Approvals
- Loading branch information
Showing
10 changed files
with
211 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Upgrade Guide | ||
|
||
## v1.3.1 -> v1.4.0 | ||
|
||
To support the new `rollback` functionality, a new migration file is needed | ||
|
||
```bash | ||
2023_10_09_204810_add_rolled_back_at_column_to_approvals_table | ||
``` | ||
|
||
Be sure to migrate your database if you plan on using the `rollback` feature. | ||
|
||
If you'd prefer to do it manually, you can add the following column to your `approvals` table: | ||
|
||
```php | ||
Schema::table('approvals', function (Blueprint $table) { | ||
$table->timestamp(column: 'rolled_back_at')->nullable()->after('original_data'); | ||
}); | ||
``` |
21 changes: 21 additions & 0 deletions
21
database/migrations/2023_10_09_204810_add_rolled_back_at_column_to_approvals_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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::table('approvals', function (Blueprint $table) { | ||
$table->timestamp(column: 'rolled_back_at')->nullable()->after('original_data'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('approvals', function (Blueprint $table) { | ||
$table->dropColumn(columns: 'rolled_back_at'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Cjmellor\Approval\Events; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
|
||
class ModelRolledBackEvent | ||
{ | ||
use Dispatchable; | ||
|
||
public function __construct( | ||
public Model $approval, | ||
public Authenticatable|null $user, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
use Cjmellor\Approval\Enums\ApprovalStatus; | ||
use Cjmellor\Approval\Events\ModelRolledBackEvent; | ||
use Cjmellor\Approval\Tests\Models\FakeModel; | ||
use Illuminate\Support\Facades\Event; | ||
|
||
test(description: 'an Approved Model can be rolled back', closure: function (): void { | ||
// Build a query | ||
$fakeModel = new FakeModel(); | ||
|
||
$fakeModel->name = 'Bob'; | ||
$fakeModel->meta = 'green'; | ||
|
||
// Save the model, bypassing approval | ||
$fakeModel->withoutApproval()->save(); | ||
|
||
// Update a fresh instance of the model | ||
$fakeModel->fresh()->update(['name' => 'Chris']); | ||
|
||
// Approve the new changes | ||
$fakeModel->fresh()->approvals()->first()->approve(); | ||
|
||
// Test for Events | ||
Event::fake(); | ||
|
||
// Rollback the data | ||
$fakeModel->fresh()->approvals()->first()->rollback(); | ||
|
||
// Check the model has been rolled back | ||
expect($fakeModel->fresh()->approvals()->first()) | ||
->state->toBe(expected: ApprovalStatus::Pending) | ||
->new_data->toMatchArray(['name' => 'Bob']) | ||
->original_data->toMatchArray(['name' => 'Chris']) | ||
->rolled_back_at->not->toBeNull(); | ||
|
||
// Assert the Events were fired | ||
Event::assertDispatched(function (ModelRolledBackEvent $event) use ($fakeModel): bool { | ||
return $event->approval->is($fakeModel->fresh()->approvals()->first()) | ||
&& $event->user === null; | ||
}); | ||
}); | ||
|
||
test(description: 'a rolled back Approval can be conditionally set', closure: function () { | ||
// Build a query | ||
$fakeModel = new FakeModel(); | ||
|
||
$fakeModel->name = 'Bob'; | ||
$fakeModel->meta = 'green'; | ||
|
||
// Save the model, bypassing approval | ||
$fakeModel->withoutApproval()->save(); | ||
|
||
// Update a fresh instance of the model | ||
$fakeModel->fresh()->update(['name' => 'Chris']); | ||
|
||
// Approve the new changes | ||
$fakeModel->fresh()->approvals()->first()->approve(); | ||
|
||
// Conditionally rollback the data | ||
$fakeModel->fresh()->approvals()->first()->rollback(fn () => true); | ||
|
||
// Check the model has been rolled back | ||
expect($fakeModel->fresh()->approvals()->first()) | ||
->state->toBe(expected: ApprovalStatus::Pending) | ||
->new_data->toMatchArray(['name' => 'Bob']) | ||
->original_data->toMatchArray(['name' => 'Chris']) | ||
->rolled_back_at->not->toBeNull(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
<?php | ||
|
||
use Cjmellor\Approval\Enums\ApprovalStatus; | ||
use Cjmellor\Approval\Tests\Models\FakeModel; | ||
use Cjmellor\Approval\Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
uses(TestCase::class)->in(__DIR__); | ||
uses(RefreshDatabase::class); | ||
uses(TestCase::class, RefreshDatabase::class) | ||
->beforeEach(hook: function (): void { | ||
$this->approvalData = [ | ||
'approvalable_type' => FakeModel::class, | ||
'approvalable_id' => 1, | ||
'state' => ApprovalStatus::Pending, | ||
'new_data' => json_encode(['name' => 'Chris']), | ||
'original_data' => json_encode(['name' => 'Bob']), | ||
]; | ||
|
||
$this->fakeModelData = [ | ||
'name' => 'Chris', | ||
'meta' => 'red', | ||
]; | ||
}) | ||
->in(__DIR__); |