From 076e2bd08236a97a7043feeec4733b730fa37570 Mon Sep 17 00:00:00 2001 From: Chris Mellor Date: Wed, 23 Aug 2023 00:25:17 +0100 Subject: [PATCH 1/2] Refactor tests - Remove package --- composer.json | 3 +- tests/Concerns/HasStreaksTest.php | 82 +++++++++++++++---------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index 81e7923..c0d1971 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,7 @@ "pestphp/pest-plugin-laravel": "^2.0", "pestphp/pest-plugin-type-coverage": "^2.0", "plannr/laravel-fast-refresh-database": "^1.0.2", - "rector/rector": "^0.17.6", - "spatie/pest-plugin-test-time": "^2.0" + "rector/rector": "^0.17.6" }, "autoload": { "psr-4": { diff --git a/tests/Concerns/HasStreaksTest.php b/tests/Concerns/HasStreaksTest.php index ad6bbce..def90de 100644 --- a/tests/Concerns/HasStreaksTest.php +++ b/tests/Concerns/HasStreaksTest.php @@ -8,7 +8,7 @@ use LevelUp\Experience\Events\StreakUnfroze; use LevelUp\Experience\Models\Activity; -use function Spatie\PestPluginTestTime\testTime; +use function Pest\Laravel\travel; uses()->group('streaks'); @@ -26,7 +26,7 @@ && $event->streak->activity_at->isToday(), ); - expect($this->activity->streaks)->toHaveCount(count: 1); + expect($this->activity)->streaks->toHaveCount(count: 1); $this->assertDatabaseHas(table: 'streaks', data: [ 'user_id' => $this->user->id, @@ -39,13 +39,13 @@ test(description: 'if an activity happens more than once on the same day, nothing will happen', closure: function () { $this->user->recordStreak($this->activity); - expect($this->activity->streaks)->toHaveCount(count: 1); + expect($this->activity)->streaks->toHaveCount(count: 1); // Now, simulate the same activity being recorded $this->user->recordStreak($this->activity); // ... there should still only be one streak record - expect($this->activity->streaks)->toHaveCount(count: 1); + expect($this->activity)->streaks->toHaveCount(count: 1); // Finally, check the data hasn't changed $this->assertDatabaseHas(table: 'streaks', data: [ @@ -61,12 +61,12 @@ $this->user->recordStreak($this->activity); - expect($this->activity->streaks)->toHaveCount(count: 1) - ->and($this->activity->streaks->first()->count)->toBe(expected: 1) - ->and($this->activity->streaks->first()->activity_at->isToday()); + expect($this->activity)->streaks->toHaveCount(count: 1) + ->and($this->activity)->streaks->first()->count->toBe(expected: 1) + ->and($this->activity)->streaks->first()->activity_at->format('U')->toBe(now()->format('U')); // Now, simulate the record happening the next day and instead, been updated - testTime()->addDay(); + travel(1)->day(); $this->user->recordStreak($this->activity); @@ -79,7 +79,7 @@ ); // There should still only be one streak record - expect($this->activity->streaks)->toHaveCount(count: 1); + expect($this->activity)->streaks->toHaveCount(count: 1); // Finally, check the data has been updated $this->assertDatabaseHas(table: 'streaks', data: [ @@ -94,25 +94,25 @@ Event::fake(); $this->user->recordStreak($this->activity); - expect(value: $this->activity->streaks)->toHaveCount(count: 1) - ->and($this->activity->streaks->first()->count)->toBe(expected: 1) - ->and($this->activity->streaks->first()->activity_at->isToday()); + expect(value: $this->activity)->streaks->toHaveCount(count: 1) + ->and($this->activity)->streaks->first()->count->toBe(expected: 1) + ->and($this->activity)->streaks->first()->activity_at->format('U')->toBe(now()->format('U')); // Simulate the activity happening again the next day - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); - expect(value: $this->activity->streaks)->toHaveCount(count: 1) - ->and($this->activity->fresh()->streaks->first()->count)->toBe(expected: 2) - ->and($this->activity->streaks->first()->activity_at->isTomorrow()); + expect(value: $this->activity)->streaks->toHaveCount(count: 1) + ->and($this->activity)->fresh()->streaks->first()->count->toBe(expected: 2) + ->and($this->activity)->fresh()->streaks->first()->activity_at->format('U')->toBe(now()->format('U')); // Simulate the activity happening again the next day - testTime()->addDays(2); + travel(value: 2)->days(); $this->user->recordStreak($this->activity); expect(value: $this->activity->streaks)->toHaveCount(count: 1) - ->and($this->activity->fresh()->streaks->first()->count)->toBe(expected: 1) - ->and($this->activity->fresh()->streaks->first()->activity_at)->toBeCarbon(now()); + ->and($this->activity)->fresh()->streaks->first()->count->toBe(expected: 1) + ->and($this->activity)->fresh()->streaks->first()->activity_at->format('U')->toBe(now()->format('U')); Event::assertDispatched( event: StreakBroken::class, @@ -127,24 +127,24 @@ $this->user->recordStreak($this->activity); expect($this->user->streaks)->toHaveCount(count: 1) - ->and($this->user->getCurrentStreakCount($this->activity))->toBe(1); + ->and($this->user)->getCurrentStreakCount($this->activity)->toBe(1); }); test(description: 'a User has a streak going', closure: function () { $this->user->recordStreak($this->activity); - expect($this->user->hasStreakToday($this->activity))->toBeTrue(); + expect($this->user)->hasStreakToday($this->activity)->toBeTrue(); }); test(description: 'a User\'s streak can be reset', closure: function () { $this->user->recordStreak($this->activity); - expect($this->user->hasStreakToday($this->activity))->toBeTrue(); + expect($this->user)->hasStreakToday($this->activity)->toBeTrue(); - testTime()->addDay(); + travel(value: 1)->day(); $this->user->resetStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 1); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 1); }); test(description: 'when a streak is broken, it is also archived for historical usage', closure: function () { @@ -153,21 +153,21 @@ $this->user->recordStreak($this->activity); // Day 2 - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); // Day 3 - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); - expect($this->user->streaks)->toHaveCount(count: 1) - ->and($this->user->getCurrentStreakCount($this->activity))->toBe(3); + expect($this->user)->streaks->toHaveCount(count: 1) + ->and($this->user)->getCurrentStreakCount($this->activity)->toBe(3); // Now, break the streak - testTime()->addDays(2); + travel(value: 2)->days(); $this->user->recordStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 1); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 1); // Check the streak's history data is correct... $this->assertDatabaseHas(table: 'streak_histories', data: [ @@ -186,7 +186,7 @@ $this->user->freezeStreak($this->activity); - expect($this->user->streaks->first()->frozen_until)->toBeCarbon(now()->addDays()->startOfDay()); + expect($this->user)->streaks->first()->frozen_until->format('U')->toBe(now()->addDays()->startOfDay()->format('U')); Event::assertDispatched( event: StreakFrozen::class, @@ -202,11 +202,11 @@ $this->user->freezeStreak($this->activity); - expect($this->user->streaks->first()->frozen_until)->toBeCarbon(now()->addDays()->startOfDay()); + expect($this->user)->streaks->first()->frozen_until->format('U')->toBe(now()->addDays()->startOfDay()->format('U')); $this->user->unFreezeStreak($this->activity); - expect($this->user->isStreakFrozen($this->activity))->toBeFalse(); + expect($this->user)->isStreakFrozen($this->activity)->toBeFalse(); Event::assertDispatched(event: StreakUnfroze::class); }); @@ -214,31 +214,31 @@ test(description: 'when a streak is frozen, it does not break', closure: function () { $this->user->recordStreak($this->activity); - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 2); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 2); $this->user->freezeStreak($this->activity); - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 3); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 3); }); test('when a streak is frozen and freeze duration has passed, streak count will reset', function () { $this->user->recordStreak($this->activity); - testTime()->addDays(); + travel(value: 1)->day(); $this->user->recordStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 2); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 2); $this->user->freezeStreak($this->activity); - testTime()->addDays(2); + travel(value: 2)->days(); $this->user->recordStreak($this->activity); - expect($this->user->getCurrentStreakCount($this->activity))->toBe(expected: 1); + expect($this->user)->getCurrentStreakCount($this->activity)->toBe(expected: 1); }); From cb9e4cedfaa4c97f83ffa9137b89f0a59b86d971 Mon Sep 17 00:00:00 2001 From: Chris Mellor Date: Wed, 23 Aug 2023 12:26:24 +0100 Subject: [PATCH 2/2] Remove named arguments for prefer-lowest setting --- tests/Concerns/HasStreaksTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Concerns/HasStreaksTest.php b/tests/Concerns/HasStreaksTest.php index def90de..714e566 100644 --- a/tests/Concerns/HasStreaksTest.php +++ b/tests/Concerns/HasStreaksTest.php @@ -174,8 +174,8 @@ 'user_id' => $this->user->id, 'activity_id' => $this->activity->id, 'count' => 3, - 'started_at' => now()->subDays(value: 4), - 'ended_at' => now()->subDays(value: 2), + 'started_at' => now()->subDays(4), + 'ended_at' => now()->subDays(2), ]); });