Skip to content

Commit

Permalink
Add tests for UUID and allow for UUID in broken Versionable trait (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiggott authored Dec 4, 2024
1 parent f3e581e commit aad410e
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function versionAt($time = null, $tz = null): ?Version
->first();
}

public function getVersion(int $id): ?Version
public function getVersion(int|string $id): ?Version
{
return $this->versions()->find($id);
}
Expand All @@ -163,12 +163,12 @@ public function getTrashedVersions()
return $this->versions()->onlyTrashed()->get();
}

public function restoreTrashedVersion(int $id)
public function restoreTrashedVersion(int|string $id)
{
return $this->versions()->onlyTrashed()->whereId($id)->restore();
}

public function revertToVersion(int $id): bool
public function revertToVersion(int|string $id): bool
{
return $this->versions()->findOrFail($id)->revert();
}
Expand All @@ -191,7 +191,7 @@ public function removeVersions(array $ids)
return $this->versions()->findMany($ids)->each->delete();
}

public function removeVersion(int $id)
public function removeVersion(int|string $id)
{
if ($this->forceDeleteVersion) {
return $this->forceRemoveVersion($id);
Expand All @@ -209,7 +209,7 @@ public function removeAllVersions(): void
$this->versions->each->delete();
}

public function forceRemoveVersion(int $id)
public function forceRemoveVersion(int|string $id)
{
return $this->versions()->findOrFail($id)->forceDelete();
}
Expand Down
115 changes: 115 additions & 0 deletions tests/VersionWithUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,119 @@ public function testUuid()

$this->assertIsString($version->id);
}

public function testUuidGetVersion()
{
$user = User::create(['name' => 'overtrue']);
$this->actingAs($user);

$post = Post::create(['title' => 'Hello world!', 'content' => 'Hello world!']);
$original_version = $post->versions()->first();

//Confirms we are using UUID
$this->assertIsString($original_version->id);

//Breaks in v5.3.2 and earlier.
$version = $post->getVersion($original_version->id);

$this->assertEquals($original_version->id, $version->id);
}

public function testUuidRestoreToVersion()
{
$user = User::create(['name' => 'overtrue']);
$this->actingAs($user);

$post = Post::create(['title' => 'Hello world!', 'content' => 'Hello world!']);
$original_version = $post->versions()->first();

//Confirms we are using UUID
$this->assertIsString($original_version->id);

//Update the Title to get a new version
$post->update(['title' => 'A New World!']);

$this->assertCount(2, $post->refresh()->versions);

$new_version = $post->latestVersion;
$this->assertNotEquals($new_version->id, $original_version->id);

//Breaks with v5.3.2
$post->revertToVersion($original_version->id);

$this->assertCount(3, $post->refresh()->versions);
$this->assertEquals('Hello world!', $post->title);
}

public function testUuidRemoveVersion()
{
$user = User::create(['name' => 'overtrue']);
$this->actingAs($user);

$post = Post::create(['title' => 'Hello world!', 'content' => 'Hello world!']);
$original_version = $post->versions()->first();

//Confirms we are using UUID
$this->assertIsString($original_version->id);

//Update the Title to get a new version
$post->update(['title' => 'A New World!']);

$this->assertCount(2, $post->refresh()->versions);

//Breaks in v5.3.2 and earlier.
$post->removeVersion($original_version->id);

$this->assertCount(1, $post->refresh()->versions);

}

public function testUuidForceRemoveVersion()
{
$user = User::create(['name' => 'overtrue']);
$this->actingAs($user);

$post = Post::create(['title' => 'Hello world!', 'content' => 'Hello world!']);
$original_version = $post->versions()->first();

//Confirms we are using UUID
$this->assertIsString($original_version->id);

//Update the Title to get a new version
$post->update(['title' => 'A New World!']);

$this->assertCount(2, $post->refresh()->versions);

//Breaks in v5.3.2 and earlier.
$post->forceRemoveVersion($original_version->id);

$this->assertCount(1, $post->refresh()->versions);
}

public function testUuidRestoreTrashedVersion()
{
$user = User::create(['name' => 'overtrue']);
$this->actingAs($user);

$post = Post::create(['title' => 'Hello world!', 'content' => 'Hello world!']);
$original_version = $post->versions()->first();

//Confirms we are using UUID
$this->assertIsString($original_version->id);

//Update the Title to get a new version
$post->update(['title' => 'A New World!']);

$this->assertCount(2, $post->refresh()->versions);

//Breaks in v5.3.2 and earlier.
$post->removeVersion($original_version->id);

$this->assertCount(1, $post->refresh()->versions);

//Breaks in v5.3.2 and earlier.
$post->restoreTrashedVersion($original_version->id);

$this->assertCount(2, $post->refresh()->versions);
}
}

0 comments on commit aad410e

Please sign in to comment.