diff --git a/tests/Browser/SanityTest.php b/tests/Browser/SanityTest.php index 454f17409aa..f0141acc11a 100644 --- a/tests/Browser/SanityTest.php +++ b/tests/Browser/SanityTest.php @@ -75,44 +75,44 @@ private static function cleanup() return; } - (new static())->createApplication(); + static::withDbAccess(function () { + // Tear down in reverse-order so that dependants get destroyed before their dependencies. + $nukingOrder = array_reverse(self::$scaffolding); - // Tear down in reverse-order so that dependants get destroyed before their dependencies. - $nukingOrder = array_reverse(self::$scaffolding); + foreach ($nukingOrder as $name => $scaffold) { + static::output("TEARDOWN: $name (".get_class($scaffold).")\n"); - foreach ($nukingOrder as $name => $scaffold) { - static::output("TEARDOWN: $name (".get_class($scaffold).")\n"); - - if ($name === 'order' || $name === 'invoice') { - // we need to perform custom deletion for orders to bypass their immutability protections - Store\Order::withTrashed()->forceDelete(); - } else { - $scaffold->forceDelete(); + if ($name === 'order' || $name === 'invoice') { + // we need to perform custom deletion for orders to bypass their immutability protections + Store\Order::withTrashed()->forceDelete(); + } else { + $scaffold->forceDelete(); + } } - } - // Clean up extra things that get created (i.e. as side-effects, etc) - Beatmap::truncate(); - Beatmapset::truncate(); - Channel::truncate(); - Count::truncate(); - AuthOption::truncate(); - Authorize::truncate(); - TopicTrack::truncate(); - Genre::truncate(); - Group::truncate(); - Language::truncate(); - LoginAttempt::truncate(); - NewsPost::truncate(); - Notification::truncate(); - User::truncate(); - UserGroup::truncate(); - UserGroupEvent::truncate(); - UserNotification::truncate(); - UserProfileCustomization::truncate(); - UserStatistics\Osu::truncate(); - - app('groups')->resetMemoized(); + // Clean up extra things that get created (i.e. as side-effects, etc) + Beatmap::truncate(); + Beatmapset::truncate(); + Channel::truncate(); + Count::truncate(); + AuthOption::truncate(); + Authorize::truncate(); + TopicTrack::truncate(); + Genre::truncate(); + Group::truncate(); + Language::truncate(); + LoginAttempt::truncate(); + NewsPost::truncate(); + Notification::truncate(); + User::truncate(); + UserGroup::truncate(); + UserGroupEvent::truncate(); + UserNotification::truncate(); + UserProfileCustomization::truncate(); + UserStatistics\Osu::truncate(); + + app('groups')->resetMemoized(); + }); } private static function createScaffolding() @@ -121,144 +121,145 @@ private static function createScaffolding() return; } - (new static())->createApplication(); - self::$scaffolding['country'] = Country::first() ?? Country::factory()->create(); - // user to login as and to use for requests - self::$scaffolding['user'] = User::factory()->create([ - 'country_acronym' => self::$scaffolding['country']->acronym, - ]); - - // factories for /beatmapsets/* - self::$scaffolding['beatmap_mirror'] = BeatmapMirror::factory()->create(); - self::$scaffolding['genre'] = Genre::factory()->create(); - self::$scaffolding['language'] = Language::factory()->create(); - self::$scaffolding['beatmapset'] = Beatmapset::factory()->create([ - 'genre_id' => self::$scaffolding['genre'], - 'language_id' => self::$scaffolding['language'], - 'user_id' => self::$scaffolding['user'], - ]); - self::$scaffolding['beatmap'] = Beatmap::factory()->create([ - 'beatmapset_id' => self::$scaffolding['beatmapset'], - ]); - self::$scaffolding['beatmap_discussion'] = BeatmapDiscussion::factory()->create([ - 'beatmapset_id' => self::$scaffolding['beatmapset'], - 'beatmap_id' => self::$scaffolding['beatmap'], - 'user_id' => self::$scaffolding['user'], - ]); - self::$scaffolding['beatmap_discussion_post'] = BeatmapDiscussionPost::factory()->timeline()->create([ - 'beatmap_discussion_id' => self::$scaffolding['beatmap_discussion'], - 'user_id' => self::$scaffolding['user'], - ]); - self::$scaffolding['beatmap_discussion_reply'] = BeatmapDiscussionPost::factory()->create([ - 'beatmap_discussion_id' => self::$scaffolding['beatmap_discussion'], - 'user_id' => self::$scaffolding['user'], - ]); - self::$scaffolding['pack'] = BeatmapPack::factory()->create(); - - // factories for /community/contests/* - self::$scaffolding['contest'] = Contest::factory()->entry()->create(); - - // factories for /community/tournaments/* - self::$scaffolding['tournament'] = Tournament::factory()->create(); - - // factories for /beatmaps/artists/* - self::$scaffolding['artist'] = Artist::factory()->create(); - self::$scaffolding['track'] = ArtistTrack::factory()->create([ - 'artist_id' => self::$scaffolding['artist']->getKey(), - ]); - - // factories for /store/* - self::$scaffolding['product'] = Store\Product::factory()->masterTshirt()->create(); - self::$scaffolding['order'] = Store\Order::factory()->checkout()->create([ - 'user_id' => self::$scaffolding['user'], - ]); - self::$scaffolding['checkout'] = new ScaffoldDummy(self::$scaffolding['order']->getKey()); - self::$scaffolding['invoice'] = Store\Order::factory()->paid()->create([ - 'user_id' => self::$scaffolding['user'], - ]); - - // factories for /community/forums/* - self::$scaffolding['forum_parent'] = Forum::factory()->closed()->create(); - self::$scaffolding['forum'] = Forum::factory()->create([ - 'parent_id' => self::$scaffolding['forum_parent'], - ]); - // satisfy group permissions required for posting in forum - self::$scaffolding['_group'] = app('groups')->byIdentifier('default'); - self::$scaffolding['_forum_acl_post'] = Authorize::factory()->post()->create([ - 'forum_id' => self::$scaffolding['forum'], - 'group_id' => self::$scaffolding['_group'], - ]); - self::$scaffolding['_forum_acl_reply'] = Authorize::factory()->reply()->create([ - 'forum_id' => self::$scaffolding['forum'], - 'group_id' => self::$scaffolding['_group'], - ]); - self::$scaffolding['_user_group'] = UserGroup::first(); - - // satisfy minimum playcount for forum posting - UserStatistics\Osu::factory()->create([ - 'playcount' => config('osu.forum.minimum_plays'), - 'user_id' => self::$scaffolding['user'], - ]); - - self::$scaffolding['topic'] = Topic::factory()->create([ - 'forum_id' => self::$scaffolding['forum'], - 'topic_poster' => self::$scaffolding['user'], - ]); - self::$scaffolding['post'] = Post::factory()->create([ - 'poster_id' => self::$scaffolding['user'], - 'topic_id' => self::$scaffolding['topic'], - ]); - - // factories for /community/chat/* - self::$scaffolding['channel'] = Channel::factory()->type('public')->create(); - self::$scaffolding['user_channel'] = UserChannel::factory()->create([ - 'channel_id' => self::$scaffolding['channel']->getKey(), - 'user_id' => self::$scaffolding['user']->getKey(), - ]); - - // dummy for game mode param - self::$scaffolding['mode'] = new ScaffoldDummy('osu'); - - // factory for /seasons/* - self::$scaffolding['season'] = Season::factory()->create(); - - // factory for /home/changelog/* - self::$scaffolding['stream'] = UpdateStream::factory()->create(); - self::$scaffolding['changelog'] = Changelog::factory()->create([ - 'stream_id' => self::$scaffolding['stream']->stream_id, - ]); - self::$scaffolding['build'] = Build::factory()->create([ - 'stream_id' => self::$scaffolding['stream']->stream_id, - ]); - - // factory for /g/* - self::$scaffolding['group'] = Group::factory()->create(); - - // factory for comments - self::$scaffolding['comment'] = Comment::factory()->create([ - 'commentable_id' => self::$scaffolding['build'], - 'commentable_type' => 'build', - 'user_id' => self::$scaffolding['user'], - ]); - - // factory for matches - self::$scaffolding['match'] = LegacyMatch\LegacyMatch::factory()->create(); - self::$scaffolding['event'] = LegacyMatch\Event::factory()->join()->create([ - 'match_id' => self::$scaffolding['match']->getKey(), - ]); - - // dummy for wiki page - self::$scaffolding['page'] = new ScaffoldDummy('Welcome'); - - // dummy for news - self::$scaffolding['news'] = new ScaffoldDummy('2014-06-21-meet-yuzu'); - - // score factory - self::$scaffolding['score'] = Score\Best\Osu::factory()->withReplay()->create(); - - self::$scaffolding['room'] = Room::factory()->create(['category' => 'spotlight']); - - app('groups')->resetMemoized(); + static::withDbAccess(function () { + self::$scaffolding['country'] = Country::first() ?? Country::factory()->create(); + // user to login as and to use for requests + self::$scaffolding['user'] = User::factory()->create([ + 'country_acronym' => self::$scaffolding['country']->acronym, + ]); + + // factories for /beatmapsets/* + self::$scaffolding['beatmap_mirror'] = BeatmapMirror::factory()->create(); + self::$scaffolding['genre'] = Genre::factory()->create(); + self::$scaffolding['language'] = Language::factory()->create(); + self::$scaffolding['beatmapset'] = Beatmapset::factory()->create([ + 'genre_id' => self::$scaffolding['genre'], + 'language_id' => self::$scaffolding['language'], + 'user_id' => self::$scaffolding['user'], + ]); + self::$scaffolding['beatmap'] = Beatmap::factory()->create([ + 'beatmapset_id' => self::$scaffolding['beatmapset'], + ]); + self::$scaffolding['beatmap_discussion'] = BeatmapDiscussion::factory()->create([ + 'beatmapset_id' => self::$scaffolding['beatmapset'], + 'beatmap_id' => self::$scaffolding['beatmap'], + 'user_id' => self::$scaffolding['user'], + ]); + self::$scaffolding['beatmap_discussion_post'] = BeatmapDiscussionPost::factory()->timeline()->create([ + 'beatmap_discussion_id' => self::$scaffolding['beatmap_discussion'], + 'user_id' => self::$scaffolding['user'], + ]); + self::$scaffolding['beatmap_discussion_reply'] = BeatmapDiscussionPost::factory()->create([ + 'beatmap_discussion_id' => self::$scaffolding['beatmap_discussion'], + 'user_id' => self::$scaffolding['user'], + ]); + self::$scaffolding['pack'] = BeatmapPack::factory()->create(); + + // factories for /community/contests/* + self::$scaffolding['contest'] = Contest::factory()->entry()->create(); + + // factories for /community/tournaments/* + self::$scaffolding['tournament'] = Tournament::factory()->create(); + + // factories for /beatmaps/artists/* + self::$scaffolding['artist'] = Artist::factory()->create(); + self::$scaffolding['track'] = ArtistTrack::factory()->create([ + 'artist_id' => self::$scaffolding['artist']->getKey(), + ]); + + // factories for /store/* + self::$scaffolding['product'] = Store\Product::factory()->masterTshirt()->create(); + self::$scaffolding['order'] = Store\Order::factory()->checkout()->create([ + 'user_id' => self::$scaffolding['user'], + ]); + self::$scaffolding['checkout'] = new ScaffoldDummy(self::$scaffolding['order']->getKey()); + self::$scaffolding['invoice'] = Store\Order::factory()->paid()->create([ + 'user_id' => self::$scaffolding['user'], + ]); + + // factories for /community/forums/* + self::$scaffolding['forum_parent'] = Forum::factory()->closed()->create(); + self::$scaffolding['forum'] = Forum::factory()->create([ + 'parent_id' => self::$scaffolding['forum_parent'], + ]); + // satisfy group permissions required for posting in forum + self::$scaffolding['_group'] = app('groups')->byIdentifier('default'); + self::$scaffolding['_forum_acl_post'] = Authorize::factory()->post()->create([ + 'forum_id' => self::$scaffolding['forum'], + 'group_id' => self::$scaffolding['_group'], + ]); + self::$scaffolding['_forum_acl_reply'] = Authorize::factory()->reply()->create([ + 'forum_id' => self::$scaffolding['forum'], + 'group_id' => self::$scaffolding['_group'], + ]); + self::$scaffolding['_user_group'] = UserGroup::first(); + + // satisfy minimum playcount for forum posting + UserStatistics\Osu::factory()->create([ + 'playcount' => config('osu.forum.minimum_plays'), + 'user_id' => self::$scaffolding['user'], + ]); + + self::$scaffolding['topic'] = Topic::factory()->create([ + 'forum_id' => self::$scaffolding['forum'], + 'topic_poster' => self::$scaffolding['user'], + ]); + self::$scaffolding['post'] = Post::factory()->create([ + 'poster_id' => self::$scaffolding['user'], + 'topic_id' => self::$scaffolding['topic'], + ]); + + // factories for /community/chat/* + self::$scaffolding['channel'] = Channel::factory()->type('public')->create(); + self::$scaffolding['user_channel'] = UserChannel::factory()->create([ + 'channel_id' => self::$scaffolding['channel']->getKey(), + 'user_id' => self::$scaffolding['user']->getKey(), + ]); + + // dummy for game mode param + self::$scaffolding['mode'] = new ScaffoldDummy('osu'); + + // factory for /seasons/* + self::$scaffolding['season'] = Season::factory()->create(); + + // factory for /home/changelog/* + self::$scaffolding['stream'] = UpdateStream::factory()->create(); + self::$scaffolding['changelog'] = Changelog::factory()->create([ + 'stream_id' => self::$scaffolding['stream']->stream_id, + ]); + self::$scaffolding['build'] = Build::factory()->create([ + 'stream_id' => self::$scaffolding['stream']->stream_id, + ]); + + // factory for /g/* + self::$scaffolding['group'] = Group::factory()->create(); + + // factory for comments + self::$scaffolding['comment'] = Comment::factory()->create([ + 'commentable_id' => self::$scaffolding['build'], + 'commentable_type' => 'build', + 'user_id' => self::$scaffolding['user'], + ]); + + // factory for matches + self::$scaffolding['match'] = LegacyMatch\LegacyMatch::factory()->create(); + self::$scaffolding['event'] = LegacyMatch\Event::factory()->join()->create([ + 'match_id' => self::$scaffolding['match']->getKey(), + ]); + + // dummy for wiki page + self::$scaffolding['page'] = new ScaffoldDummy('Welcome'); + + // dummy for news + self::$scaffolding['news'] = new ScaffoldDummy('2014-06-21-meet-yuzu'); + + // score factory + self::$scaffolding['score'] = Score\Best\Osu::factory()->withReplay()->create(); + + self::$scaffolding['room'] = Room::factory()->create(['category' => 'spotlight']); + + app('groups')->resetMemoized(); + }); } private static function filterLog(array $log) diff --git a/tests/Controllers/BeatmapsControllerSoloScoresTest.php b/tests/Controllers/BeatmapsControllerSoloScoresTest.php index 145001e5677..2886fa10f40 100644 --- a/tests/Controllers/BeatmapsControllerSoloScoresTest.php +++ b/tests/Controllers/BeatmapsControllerSoloScoresTest.php @@ -33,147 +33,151 @@ class BeatmapsControllerSoloScoresTest extends TestCase public static function setUpBeforeClass(): void { - (new static())->refreshApplication(); - static::$user = User::factory()->create(['osu_subscriber' => true]); - static::$otherUser = User::factory()->create(['country_acronym' => Country::factory()]); - $friend = User::factory()->create(['country_acronym' => Country::factory()]); - static::$beatmap = Beatmap::factory()->qualified()->create(); - - $countryAcronym = static::$user->country_acronym; - - static::$scores = []; - $scoreFactory = SoloScore::factory(); - foreach (['solo' => 0, 'legacy' => null] as $type => $buildId) { - $defaultData = ['build_id' => $buildId]; - - static::$scores = array_merge(static::$scores, [ - "{$type}:user" => $scoreFactory->withData($defaultData, ['total_score' => 1100])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$user, - ]), - "{$type}:userMods" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1050, - 'mods' => static::defaultMods(['DT', 'HD']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$user, - ]), - "{$type}:userModsNC" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1050, - 'mods' => static::defaultMods(['NC']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$user, - ]), - "{$type}:otherUserModsNCPFHigherScore" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1010, - 'mods' => static::defaultMods(['NC', 'PF']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$otherUser, - ]), - "{$type}:userModsLowerScore" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1000, - 'mods' => static::defaultMods(['DT', 'HD']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$user, - ]), - "{$type}:friend" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => $friend, - ]), - // With preference mods - "{$type}:otherUser" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1000, - 'mods' => static::defaultMods(['PF']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$otherUser, - ]), - "{$type}:otherUserMods" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1000, - 'mods' => static::defaultMods(['HD', 'PF', 'NC']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$otherUser, - ]), - "{$type}:otherUserModsExtraNonPreferences" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1000, - 'mods' => static::defaultMods(['DT', 'HD', 'HR']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$otherUser, - ]), - "{$type}:otherUserModsUnrelated" => $scoreFactory->withData($defaultData, [ - 'total_score' => 1000, - 'mods' => static::defaultMods(['FL']), - ])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => static::$otherUser, - ]), - // Same total score but achieved later so it should come up after earlier score - "{$type}:otherUser2Later" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), - ]), - "{$type}:otherUser3SameCountry" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => true, - 'user_id' => User::factory()->state(['country_acronym' => $countryAcronym]), - ]), - // Non-preserved score should be filtered out - "{$type}:nonPreserved" => $scoreFactory->withData($defaultData)->create([ - 'beatmap_id' => static::$beatmap, - 'preserve' => false, - 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), - ]), - // Unrelated score - "{$type}:unrelated" => $scoreFactory->withData($defaultData)->create([ - 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), - ]), + static::withDbAccess(function () { + static::$user = User::factory()->create(['osu_subscriber' => true]); + static::$otherUser = User::factory()->create(['country_acronym' => Country::factory()]); + $friend = User::factory()->create(['country_acronym' => Country::factory()]); + static::$beatmap = Beatmap::factory()->qualified()->create(); + + $countryAcronym = static::$user->country_acronym; + + static::$scores = []; + $scoreFactory = SoloScore::factory(); + foreach (['solo' => 0, 'legacy' => null] as $type => $buildId) { + $defaultData = ['build_id' => $buildId]; + + static::$scores = array_merge(static::$scores, [ + "{$type}:user" => $scoreFactory->withData($defaultData, ['total_score' => 1100])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$user, + ]), + "{$type}:userMods" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1050, + 'mods' => static::defaultMods(['DT', 'HD']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$user, + ]), + "{$type}:userModsNC" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1050, + 'mods' => static::defaultMods(['NC']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$user, + ]), + "{$type}:otherUserModsNCPFHigherScore" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1010, + 'mods' => static::defaultMods(['NC', 'PF']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$otherUser, + ]), + "{$type}:userModsLowerScore" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1000, + 'mods' => static::defaultMods(['DT', 'HD']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$user, + ]), + "{$type}:friend" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => $friend, + ]), + // With preference mods + "{$type}:otherUser" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1000, + 'mods' => static::defaultMods(['PF']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$otherUser, + ]), + "{$type}:otherUserMods" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1000, + 'mods' => static::defaultMods(['HD', 'PF', 'NC']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$otherUser, + ]), + "{$type}:otherUserModsExtraNonPreferences" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1000, + 'mods' => static::defaultMods(['DT', 'HD', 'HR']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$otherUser, + ]), + "{$type}:otherUserModsUnrelated" => $scoreFactory->withData($defaultData, [ + 'total_score' => 1000, + 'mods' => static::defaultMods(['FL']), + ])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => static::$otherUser, + ]), + // Same total score but achieved later so it should come up after earlier score + "{$type}:otherUser2Later" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), + ]), + "{$type}:otherUser3SameCountry" => $scoreFactory->withData($defaultData, ['total_score' => 1000])->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => true, + 'user_id' => User::factory()->state(['country_acronym' => $countryAcronym]), + ]), + // Non-preserved score should be filtered out + "{$type}:nonPreserved" => $scoreFactory->withData($defaultData)->create([ + 'beatmap_id' => static::$beatmap, + 'preserve' => false, + 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), + ]), + // Unrelated score + "{$type}:unrelated" => $scoreFactory->withData($defaultData)->create([ + 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), + ]), + ]); + } + + UserRelation::create([ + 'friend' => true, + 'user_id' => static::$user->getKey(), + 'zebra_id' => $friend->getKey(), ]); - } - UserRelation::create([ - 'friend' => true, - 'user_id' => static::$user->getKey(), - 'zebra_id' => $friend->getKey(), - ]); - - Artisan::call('es:index-scores:queue', [ - '--all' => true, - '--no-interaction' => true, - ]); - (new ScoreSearch())->indexWait(); + Artisan::call('es:index-scores:queue', [ + '--all' => true, + '--no-interaction' => true, + ]); + (new ScoreSearch())->indexWait(); + }); } public static function tearDownAfterClass(): void { - (new static())->refreshApplication(); - Beatmap::truncate(); - Beatmapset::truncate(); - Country::truncate(); - Genre::truncate(); - Group::truncate(); - Language::truncate(); - SoloScore::truncate(); - User::truncate(); - UserGroup::truncate(); - UserGroupEvent::truncate(); - UserRelation::truncate(); - (new ScoreSearch())->deleteAll(); + parent::tearDownAfterClass(); + + static::withDbAccess(function () { + Beatmap::truncate(); + Beatmapset::truncate(); + Country::truncate(); + Genre::truncate(); + Group::truncate(); + Language::truncate(); + SoloScore::truncate(); + User::truncate(); + UserGroup::truncate(); + UserGroupEvent::truncate(); + UserRelation::truncate(); + (new ScoreSearch())->deleteAll(); + }); } private static function defaultMods(array $modNames): array diff --git a/tests/Models/Solo/ScoreEsIndexTest.php b/tests/Models/Solo/ScoreEsIndexTest.php index b3e4aa8b162..ba42e4fe3c5 100644 --- a/tests/Models/Solo/ScoreEsIndexTest.php +++ b/tests/Models/Solo/ScoreEsIndexTest.php @@ -34,84 +34,86 @@ class ScoreEsIndexTest extends TestCase public static function setUpBeforeClass(): void { - (new static())->refreshApplication(); - static::$user = User::factory()->create(['osu_subscriber' => true]); - $otherUser = User::factory()->create(['country_acronym' => Country::factory()]); - static::$beatmap = Beatmap::factory()->qualified()->create(); - - $scoreFactory = Score::factory()->state(['preserve' => true]); - $defaultData = ['build_id' => 1]; - - $mods = [ - ['acronym' => 'DT', 'settings' => []], - ['acronym' => 'HD', 'settings' => []], - ]; - $unrelatedMods = [ - ['acronym' => 'NC', 'settings' => []], - ]; - - static::$scores = [ - 'otherUser' => $scoreFactory->withData($defaultData, [ - 'total_score' => 1150, - 'mods' => $unrelatedMods, - ])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => $otherUser, - ]), - 'otherUserMods' => $scoreFactory->withData($defaultData, [ - 'total_score' => 1140, - 'mods' => $mods, - ])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => $otherUser, - ]), - 'otherUser2' => $scoreFactory->withData($defaultData, [ - 'total_score' => 1150, - 'mods' => $mods, - ])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), - ]), - 'otherUser3SameCountry' => $scoreFactory->withData($defaultData, [ - 'total_score' => 1130, - 'mods' => $unrelatedMods, - ])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => User::factory()->state(['country_acronym' => static::$user->country_acronym]), - ]), - 'user' => $scoreFactory->withData($defaultData, ['total_score' => 1100])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => static::$user, - ]), - 'userMods' => $scoreFactory->withData($defaultData, [ - 'total_score' => 1050, - 'mods' => $mods, - ])->create([ - 'beatmap_id' => static::$beatmap, - 'user_id' => static::$user, - ]), - ]; - - static::reindexScores(); + static::withDbAccess(function () { + static::$user = User::factory()->create(['osu_subscriber' => true]); + $otherUser = User::factory()->create(['country_acronym' => Country::factory()]); + static::$beatmap = Beatmap::factory()->qualified()->create(); + + $scoreFactory = Score::factory()->state(['preserve' => true]); + $defaultData = ['build_id' => 1]; + + $mods = [ + ['acronym' => 'DT', 'settings' => []], + ['acronym' => 'HD', 'settings' => []], + ]; + $unrelatedMods = [ + ['acronym' => 'NC', 'settings' => []], + ]; + + static::$scores = [ + 'otherUser' => $scoreFactory->withData($defaultData, [ + 'total_score' => 1150, + 'mods' => $unrelatedMods, + ])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => $otherUser, + ]), + 'otherUserMods' => $scoreFactory->withData($defaultData, [ + 'total_score' => 1140, + 'mods' => $mods, + ])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => $otherUser, + ]), + 'otherUser2' => $scoreFactory->withData($defaultData, [ + 'total_score' => 1150, + 'mods' => $mods, + ])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => User::factory()->state(['country_acronym' => Country::factory()]), + ]), + 'otherUser3SameCountry' => $scoreFactory->withData($defaultData, [ + 'total_score' => 1130, + 'mods' => $unrelatedMods, + ])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => User::factory()->state(['country_acronym' => static::$user->country_acronym]), + ]), + 'user' => $scoreFactory->withData($defaultData, ['total_score' => 1100])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => static::$user, + ]), + 'userMods' => $scoreFactory->withData($defaultData, [ + 'total_score' => 1050, + 'mods' => $mods, + ])->create([ + 'beatmap_id' => static::$beatmap, + 'user_id' => static::$user, + ]), + ]; + + static::reindexScores(); + }); } public static function tearDownAfterClass(): void { parent::tearDownAfterClass(); - (new static())->refreshApplication(); - Beatmap::truncate(); - Beatmapset::truncate(); - Country::truncate(); - Genre::truncate(); - Group::truncate(); - Language::truncate(); - Score::truncate(); - User::truncate(); - UserGroup::truncate(); - UserGroupEvent::truncate(); - UserRelation::truncate(); - (new ScoreSearch())->deleteAll(); + static::withDbAccess(function () { + Beatmap::truncate(); + Beatmapset::truncate(); + Country::truncate(); + Genre::truncate(); + Group::truncate(); + Language::truncate(); + Score::truncate(); + User::truncate(); + UserGroup::truncate(); + UserGroupEvent::truncate(); + UserRelation::truncate(); + (new ScoreSearch())->deleteAll(); + }); } /** diff --git a/tests/TestCase.php b/tests/TestCase.php index 727c6fa0a01..4d7bfd07d64 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,6 +16,7 @@ use Artisan; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use Firebase\JWT\JWT; +use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; @@ -44,6 +45,25 @@ protected static function reindexScores() $search->indexWait(); } + protected static function resetAppDb(DatabaseManager $database): void + { + foreach (array_keys(config('database.connections')) as $name) { + $connection = $database->connection($name); + + $connection->rollBack(); + $connection->disconnect(); + } + } + + protected static function withDbAccess(callable $callback): void + { + $db = (new static())->createApplication()->make('db'); + + $callback(); + + static::resetAppDb($db); + } + protected $connectionsToTransact = [ 'mysql', 'mysql-chat', @@ -82,15 +102,8 @@ protected function setUp(): void // Force connections to reset even if transactional tests were not used. // Should fix tests going wonky when different queue drivers are used, or anything that // breaks assumptions of object destructor timing. - $database = $this->app->make('db'); - $this->beforeApplicationDestroyed(function () use ($database) { - foreach (array_keys(config('database.connections')) as $name) { - $connection = $database->connection($name); - - $connection->rollBack(); - $connection->disconnect(); - } - }); + $db = $this->app->make('db'); + $this->beforeApplicationDestroyed(fn () => static::resetAppDb($db)); app(BroadcastsPendingForTests::class)->reset(); }