Skip to content

Commit

Permalink
Add VersionDoesNotExist exception and update rewind manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jared-cannon committed Jan 12, 2025
1 parent 9ea567d commit 1124a85
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Exceptions/VersionDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace AvocetShores\LaravelRewind\Exceptions;

use AvocetShores\LaravelRewind\Exceptions\LaravelRewindException;

class VersionDoesNotExistException extends LaravelRewindException
{

}
5 changes: 3 additions & 2 deletions src/Services/RewindManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AvocetShores\LaravelRewind\Services;

use AvocetShores\LaravelRewind\Exceptions\LaravelRewindException;
use AvocetShores\LaravelRewind\Exceptions\VersionDoesNotExistException;
use AvocetShores\LaravelRewind\LaravelRewindServiceProvider;
use AvocetShores\LaravelRewind\Traits\Rewindable;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function goToVersion(Model $model, int $version): bool
// Validate the target version
$revision = $model->revisions()->where('version', $version)->first();
if (! $revision) {
return false;
throw new VersionDoesNotExistException('The specified version does not exist.');
}

// Apply the revision
Expand All @@ -101,7 +102,7 @@ protected function applyRevision(Model $model, int $targetVersion): bool

$revisionToApply = $model->revisions()->where('version', $targetVersion)->first();
if (! $revisionToApply) {
throw new LaravelRewindException('Revision not found.');
throw new VersionDoesNotExistException('The specified version does not exist.');
}

// Prepare the new_values to be applied to the model
Expand Down
133 changes: 133 additions & 0 deletions tests/Feature/RewindManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use AvocetShores\LaravelRewind\Exceptions\LaravelRewindException;
use AvocetShores\LaravelRewind\Exceptions\VersionDoesNotExistException;
use AvocetShores\LaravelRewind\Facades\Rewind;
use AvocetShores\LaravelRewind\Tests\Models\Post;
use AvocetShores\LaravelRewind\Tests\Models\User;
Expand Down Expand Up @@ -44,3 +46,134 @@
$this->assertSame('Original Title', $post->title);
$this->assertSame('Original Body', $post->body);
});

it('rewinds a model to the next revision on redo', function () {
// Arrange
$post = Post::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->refresh();

// Assert the model has current_version set to 1
$this->assertSame(1, $post->current_version);

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

$this->assertSame(2, $post->current_version);

// Act: Undo the last revision
Rewind::undo($post);

// Assert: The model should be reverted to the previous revision
$this->assertSame(1, $post->current_version);
$this->assertSame('Original Title', $post->title);
$this->assertSame('Original Body', $post->body);

// Act: Redo the last revision
Rewind::redo($post);

// Assert: The model should be reverted to the next revision
$this->assertSame(2, $post->current_version);
$this->assertSame('Updated Title', $post->title);
$this->assertSame('Updated Body', $post->body);
});

it('creates a new max revision when a model is updated while on a previous version', function () {
// Arrange
$post = Post::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->refresh();

// Assert the model has current_version set to 1
$this->assertSame(1, $post->current_version);

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

$this->assertSame(2, $post->current_version);

// Act: Undo the last revision
Rewind::undo($post);

// Assert: The model should be reverted to the previous revision
$this->assertSame(1, $post->current_version);
$this->assertSame('Original Title', $post->title);
$this->assertSame('Original Body', $post->body);

// Act: Update the model again
$post->update([
'title' => 'Updated Title Again',
'body' => 'Updated Body Again',
]);

// Assert: The model should now be at version 3
$this->assertSame(3, $post->current_version);
$this->assertSame('Updated Title Again', $post->title);
$this->assertSame('Updated Body Again', $post->body);
});

it('can jump to a specified version', function () {
// Arrange
$post = Post::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->refresh();

// Assert the model has current_version set to 1
$this->assertSame(1, $post->current_version);

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

$this->assertSame(2, $post->current_version);

// Act: Jump to version 1
Rewind::goToVersion($post, 1);

// Assert: The model should be reverted to the previous revision
$this->assertSame(1, $post->current_version);
$this->assertSame('Original Title', $post->title);
$this->assertSame('Original Body', $post->body);

// Act: Jump to version 2
Rewind::goToVersion($post, 2);

// Assert: The model should be back to the latest revision
$this->assertSame(2, $post->current_version);
$this->assertSame('Updated Title', $post->title);
$this->assertSame('Updated Body', $post->body);
});

it('throws an exception when jumping to a revision that does not exist', function () {
// Arrange
$post = Post::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->refresh();

// Assert the model has current_version set to 1
$this->assertSame(1, $post->current_version);

// Act: Jump to version 2
Rewind::goToVersion($post, 2);
})->throws(VersionDoesNotExistException::class);

0 comments on commit 1124a85

Please sign in to comment.