Skip to content

Commit

Permalink
Merge pull request #33 from cjmellor/tests-refactor
Browse files Browse the repository at this point in the history
Refactor Tests
  • Loading branch information
cjmellor committed Aug 23, 2023
2 parents fb6b58d + cb9e4ce commit 8efdc07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
86 changes: 43 additions & 43 deletions tests/Concerns/HasStreaksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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,
Expand All @@ -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: [
Expand All @@ -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);

Expand All @@ -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: [
Expand All @@ -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,
Expand All @@ -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 () {
Expand All @@ -153,29 +153,29 @@
$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: [
'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),
]);
});

Expand All @@ -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,
Expand All @@ -202,43 +202,43 @@

$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);
});

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);
});

0 comments on commit 8efdc07

Please sign in to comment.