Skip to content

Commit

Permalink
Add new exception when model does not use Rewindable and add test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
jared-cannon committed Jan 17, 2025
1 parent e97fc63 commit 95ccb61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Exceptions/ModelNotRewindableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace AvocetShores\LaravelRewind\Exceptions;

class ModelNotRewindableException extends LaravelRewindException {}
5 changes: 3 additions & 2 deletions src/Services/RewindManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AvocetShores\LaravelRewind\Enums\ApproachMethod;
use AvocetShores\LaravelRewind\Exceptions\LaravelRewindException;
use AvocetShores\LaravelRewind\Exceptions\ModelNotRewindableException;
use AvocetShores\LaravelRewind\Exceptions\VersionDoesNotExistException;
use AvocetShores\LaravelRewind\Models\RewindVersion;
use AvocetShores\LaravelRewind\Traits\Rewindable;
Expand Down Expand Up @@ -208,12 +209,12 @@ protected function determineCurrentVersion($model): int
/**
* Ensure the model uses the Rewindable trait.
*
* @throws LaravelRewindException
* @throws ModelNotRewindableException
*/
protected function assertRewindable($model): void
{
if (collect(class_uses_recursive($model::class))->doesntContain(Rewindable::class)) {
throw new LaravelRewindException('Model must use the Rewindable trait to be rewound.');
throw new ModelNotRewindableException('Model must use the Rewindable trait to be rewound.');
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/RewindManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use AvocetShores\LaravelRewind\Exceptions\ModelNotRewindableException;
use AvocetShores\LaravelRewind\Exceptions\VersionDoesNotExistException;
use AvocetShores\LaravelRewind\Facades\Rewind;
use AvocetShores\LaravelRewind\Tests\Models\Post;
use AvocetShores\LaravelRewind\Tests\Models\PostThatIsNotRewindable;
use AvocetShores\LaravelRewind\Tests\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -414,3 +416,20 @@
// Act: Fast-forward the last version
Rewind::fastForward($post);
})->throws(VersionDoesNotExistException::class);

it('throws an exception when the model is not rewindable', function () {
// Arrange
$post = PostThatIsNotRewindable::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->update([
'title' => 'Updated Title',
'body' => 'Updated Body',
]);

// Act: Rewind the last version
Rewind::rewind($post);
})->throws(ModelNotRewindableException::class);
18 changes: 18 additions & 0 deletions tests/Models/PostThatIsNotRewindable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace AvocetShores\LaravelRewind\Tests\Models;

use Illuminate\Database\Eloquent\Model;

class PostThatIsNotRewindable extends Model
{
protected $table = 'posts';

protected $fillable = [
'user_id',
'title',
'body',
];
}


0 comments on commit 95ccb61

Please sign in to comment.