Skip to content

Commit

Permalink
test: add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Nov 24, 2024
1 parent c939cb8 commit 75f2b68
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 18 deletions.
112 changes: 112 additions & 0 deletions tests/Feature/Connect/Actions/BuildClientPatchDataActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use App\Connect\Actions\BuildClientPatchDataAction;
use App\Models\Achievement;
use App\Models\Game;
use App\Models\GameAchievementSet;
use App\Models\GameHash;
use App\Models\Leaderboard;
use App\Models\PlayerGame;
use App\Models\System;
use App\Models\User;
use App\Models\UserGameAchievementSetPreference;
use App\Platform\Actions\AssociateAchievementSetToGameAction;
use App\Platform\Actions\UpsertGameCoreAchievementSetFromLegacyFlagsAction;
use App\Platform\Enums\AchievementFlag;
Expand Down Expand Up @@ -312,4 +314,114 @@ public function testItOmitsMultisetDataWhenUsingGameDirectlyLikeLegacyClients():
$this->assertTrue($result['Success']);
$this->assertArrayNotHasKey('Sets', $result['PatchData']);
}

/**
* If the user is globally opted out of subsets and they load a bonus subset
* game's hash, then it's like the user is still living in the pre-multiset
* world. The only set that resolves is the set for the subset game.
*/
public function testGloballyOptedOutOfSubsetsAndLoadedSubsetHash(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 1);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 2);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', publishedCount: 3);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
$this->upsertGameCoreSetAction->execute($bonusGame2);

$this->associateAchievementSetToGameAction->execute($baseGame, $bonusGame, AchievementSetType::Bonus, 'Bonus');
$this->associateAchievementSetToGameAction->execute($baseGame, $bonusGame2, AchievementSetType::Bonus, 'Bonus 2');

$bonusGameHash = GameHash::factory()->create(['game_id' => $bonusGame->id]);

$user = User::factory()->create(['websitePrefs' => self::OPT_IN_TO_ALL_SUBSETS_PREF_DISABLED]);

// Act
$result = (new BuildClientPatchDataAction())->execute(
gameHash: $bonusGameHash,
user: $user
);

// Assert
$this->assertTrue($result['Success']);

$this->assertEquals(2, $result['PatchData']['ID']);
$this->assertEquals('Dragon Quest III [Subset - Bonus]', $result['PatchData']['Title']);
$this->assertCount(2, $result['PatchData']['Achievements']);

$this->assertCount(1, $result['PatchData']['Sets']);
$this->assertEquals('bonus', $result['PatchData']['Sets'][0]['Type']);
$this->assertEquals(2, $result['PatchData']['Sets'][0]['CoreGameID']);
$this->assertCount(2, $result['PatchData']['Sets'][0]['Achievements']);
}

/**
* If the user has multiset enabled, they load a bonus subset game's hash, but are locally
* opted out of that subset, then we treat it like they loaded a core game hash. They'll
* receive the core set and any other bonus sets, but not the set they've opted out of.
*/
public function testLocallyOptedOutOfSubsetsAndLoadedOptedOutSubsetHash(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 1);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 2);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', publishedCount: 3);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
$this->upsertGameCoreSetAction->execute($bonusGame2);

$this->associateAchievementSetToGameAction->execute($baseGame, $bonusGame, AchievementSetType::Bonus, 'Bonus'); // !!
$this->associateAchievementSetToGameAction->execute($baseGame, $bonusGame2, AchievementSetType::Bonus, 'Bonus 2');

$bonusGameHash = GameHash::factory()->create(['game_id' => $bonusGame->id]);

$user = User::factory()->create(['websitePrefs' => self::OPT_IN_TO_ALL_SUBSETS_PREF_ENABLED]);

// They're going to load a hash for $bonusGame, but they're also locally opted out of
// $bonusGame's achievement set.
$optOutSet = GameAchievementSet::firstWhere('title', 'Bonus'); // !!
UserGameAchievementSetPreference::factory()->create([
'user_id' => $user->id,
'game_achievement_set_id' => GameAchievementSet::whereGameId($baseGame->id)
->whereType(AchievementSetType::Bonus)
->whereAchievementSetId($optOutSet->achievement_set_id)
->first()
->id,
'opted_in' => false,
]);

// Act
$result = (new BuildClientPatchDataAction())->execute(
gameHash: $bonusGameHash,
user: $user
);

// 🔴 JAMIRAS LOOK HERE
// "Sets" only contains $baseGame and $bonusGame2.
// However, PatchData still contains all the pre-multiset stuff, eg
// - "Title" is "Dragon Quest III [Subset - Bonus]"
// - "Achievements" has 2 published achievements
// Is this ok left as-is, or should this data be changed?
// Assert
$this->assertTrue($result['Success']);

$this->assertEquals(2, $result['PatchData']['ID']);
$this->assertEquals('Dragon Quest III [Subset - Bonus]', $result['PatchData']['Title']);
$this->assertCount(2, $result['PatchData']['Achievements']);

$this->assertCount(2, $result['PatchData']['Sets']);

$this->assertEquals('core', $result['PatchData']['Sets'][0]['Type']);
$this->assertEquals(1, $result['PatchData']['Sets'][0]['CoreGameID']);
$this->assertCount(1, $result['PatchData']['Sets'][0]['Achievements']);

$this->assertEquals('bonus', $result['PatchData']['Sets'][1]['Type']);
$this->assertEquals(3, $result['PatchData']['Sets'][1]['CoreGameID']);
$this->assertEquals('Bonus 2', $result['PatchData']['Sets'][1]['Title']);
$this->assertCount(3, $result['PatchData']['Sets'][1]['Achievements']);
}
}
36 changes: 18 additions & 18 deletions tests/Feature/Connect/Actions/ResolveAchievementSetsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function createGameWithAchievements(
System $system,
string $title,
int $publishedCount,
int $unpublishedCount
int $unpublishedCount = 0
): Game {
$game = Game::factory()->create(['Title' => $title, 'ConsoleID' => $system->id]);
Achievement::factory()->published()->count($publishedCount)->create(['GameID' => $game->id]);
Expand Down Expand Up @@ -217,9 +217,9 @@ public function testItReturnsSpecialtySetAndCoreBonusSetsForSpecialtyHash(): voi
public function testItAllowsSpecialtySetPlayersToOptOutOfTheCoreSet(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 5, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 3, 0);
$specialtyGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Specialty]', 1, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 5);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 3);
$specialtyGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Specialty]', 1);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand Down Expand Up @@ -255,9 +255,9 @@ public function testItAllowsSpecialtySetPlayersToOptOutOfTheCoreSet(): void
public function testItAllowsSpecialtySetPlayersToOptOutOfBonusSets(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 5, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 3, 0);
$specialtyGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Specialty]', 1, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 5);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 3);
$specialtyGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Specialty]', publishedCount: 1);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand Down Expand Up @@ -383,9 +383,9 @@ public function testItIncludesSubsetIfUserIsGloballyOptedOutButLocallyOptedIn():
public function testItReturnsExclusiveSetAndNothingElseForExclusiveHash(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 5, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 3, 0);
$exclusiveGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Exclusive]', 6, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 5);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 3);
$exclusiveGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Exclusive]', publishedCount: 6);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand All @@ -411,8 +411,8 @@ public function testItReturnsExclusiveSetAndNothingElseForExclusiveHash(): void
public function testItExcludesAchievementSetIfHashIsIncompatible(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 5, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 3, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 5);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 3);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand Down Expand Up @@ -512,9 +512,9 @@ public function testCoreGameIdIsCorrectlySet(): void
public function testGloballyOptedOutOfSubsetsAndLoadedSubsetHash(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 1, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 2, 0);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', 3, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 1);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 2);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', publishedCount: 3);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand Down Expand Up @@ -545,9 +545,9 @@ public function testGloballyOptedOutOfSubsetsAndLoadedSubsetHash(): void
public function testLocallyOptedOutOfSubsetsAndLoadedOptedOutSubsetHash(): void
{
// Arrange
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', 1, 0);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', 2, 0);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', 3, 0);
$baseGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III', publishedCount: 1);
$bonusGame = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus]', publishedCount: 2);
$bonusGame2 = $this->createGameWithAchievements($this->system, 'Dragon Quest III [Subset - Bonus 2]', publishedCount: 3);

$this->upsertGameCoreSetAction->execute($baseGame);
$this->upsertGameCoreSetAction->execute($bonusGame);
Expand Down

0 comments on commit 75f2b68

Please sign in to comment.