From fa73ba1bec9cd45c4daa71f77d41f9088dff6cc1 Mon Sep 17 00:00:00 2001 From: Kovah Date: Thu, 22 Feb 2024 12:09:43 +0100 Subject: [PATCH] Optimize tests with time travelling between multiple actions within the same test --- tests/Components/History/LinkEntryTest.php | 1 + tests/Components/History/ListEntryTest.php | 1 + tests/Components/History/TagEntryTest.php | 1 + tests/Components/History/UserEntryTest.php | 25 +++++++++++----------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tests/Components/History/LinkEntryTest.php b/tests/Components/History/LinkEntryTest.php index 758bef05..589902c0 100644 --- a/tests/Components/History/LinkEntryTest.php +++ b/tests/Components/History/LinkEntryTest.php @@ -77,6 +77,7 @@ public function testModelDeletion(): void $link = Link::factory()->create(); $link->delete(); + $this->travel(10)->seconds(); $link->restore(); $historyEntries = $link->audits()->get(); diff --git a/tests/Components/History/ListEntryTest.php b/tests/Components/History/ListEntryTest.php index b4ebf13f..45be6c3f 100644 --- a/tests/Components/History/ListEntryTest.php +++ b/tests/Components/History/ListEntryTest.php @@ -73,6 +73,7 @@ public function testModelDeletion(): void $list = LinkList::factory()->create(); $list->delete(); + $this->travel(10)->seconds(); $list->restore(); $historyEntries = $list->audits()->get(); diff --git a/tests/Components/History/TagEntryTest.php b/tests/Components/History/TagEntryTest.php index 11fc294a..ac1d9d8d 100644 --- a/tests/Components/History/TagEntryTest.php +++ b/tests/Components/History/TagEntryTest.php @@ -45,6 +45,7 @@ public function testModelDeletion(): void $tag = Tag::factory()->create(); $tag->delete(); + $this->travel(10)->seconds(); $tag->restore(); $historyEntries = $tag->audits()->get(); diff --git a/tests/Components/History/UserEntryTest.php b/tests/Components/History/UserEntryTest.php index e361a4b5..34c85b5f 100644 --- a/tests/Components/History/UserEntryTest.php +++ b/tests/Components/History/UserEntryTest.php @@ -33,20 +33,20 @@ public function testRegularChange(): void public function testModelDeletion(): void { $user = User::factory()->create(['name' => 'TestUser']); - + $this->travel(10)->seconds(); $user->delete(); + $this->travel(10)->seconds(); + $user->restore(); $historyEntries = $user->audits()->latest()->get(); + $output = (new UserEntry($historyEntries[0]))->render(); + $this->assertStringContainsString('User TestUser was restored', $output); $output = (new UserEntry($historyEntries[1]))->render(); $this->assertStringContainsString('User TestUser was deleted', $output); - $user->restore(); - - $historyEntries = $user->audits()->latest()->get(); - $output = (new UserEntry($historyEntries[2]))->render(); - $this->assertStringContainsString('User TestUser was restored', $output); + $this->assertStringContainsString('User TestUser was created', $output); } public function testModelBlocking(): void @@ -56,19 +56,20 @@ public function testModelBlocking(): void $this->actingAs($admin); $user = User::factory()->create(['name' => 'TestUser']); - + $this->travel(10)->seconds(); $this->patch('system/users/2/block'); + $this->travel(10)->seconds(); + $this->patch('system/users/2/unblock'); $historyEntries = $user->audits()->latest()->get(); + $output = (new UserEntry($historyEntries[0]))->render(); + $this->assertStringContainsString('User TestUser was unblocked', $output); + $output = (new UserEntry($historyEntries[1]))->render(); $this->assertStringContainsString('User TestUser was blocked', $output); - $this->patch('system/users/2/unblock'); - - $historyEntries = $user->audits()->latest()->get(); - $output = (new UserEntry($historyEntries[2]))->render(); - $this->assertStringContainsString('User TestUser was unblocked', $output); + $this->assertStringContainsString('User TestUser was created', $output); } }