the browser-facing portion of osu!
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add class based factory for LegacyMatch related models

nanaya fc94bc77 e128eb33

+118 -96
+6 -6
database/factories/Chat/ChannelFactory.php
··· 25 25 ]; 26 26 } 27 27 28 - public function moderated() 28 + public function moderated(): static 29 29 { 30 30 return $this->state(['moderated' => true]); 31 31 } 32 32 33 - public function pm(User ...$users) 33 + public function pm(User ...$users): static 34 34 { 35 35 if (empty($users)) { 36 36 $users = User::factory()->count(2)->create(); ··· 47 47 ])->withUsers(...$users); 48 48 } 49 49 50 - public function tourney() 50 + public function tourney(): static 51 51 { 52 - $match = factory(LegacyMatch::class)->states('tourney')->create(); 52 + $match = LegacyMatch::factory()->tourney()->create(); 53 53 54 54 return $this->state([ 55 55 'name' => "#mp_{$match->getKey()}", ··· 57 57 ]); 58 58 } 59 59 60 - public function type(string $type, array $users = []) 60 + public function type(string $type, array $users = []): static 61 61 { 62 62 if ($type === 'tourney') { 63 63 return $this->tourney(); ··· 68 68 return $this->state(['type' => Channel::TYPES[$type]])->withUsers(...$users); 69 69 } 70 70 71 - public function withUsers(User ...$users) 71 + public function withUsers(User ...$users): static 72 72 { 73 73 return $this->afterCreating(function (Channel $channel) use ($users) { 74 74 foreach ($users as $user) {
+50 -42
database/factories/LegacyMatch/EventFactory.php
··· 3 3 // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4 4 // See the LICENCE file in the repository root for full licence text. 5 5 6 + declare(strict_types=1); 7 + 8 + namespace Database\Factories\LegacyMatch; 9 + 6 10 use App\Models\LegacyMatch\Event; 7 11 use App\Models\LegacyMatch\Game; 8 12 use App\Models\LegacyMatch\LegacyMatch; 13 + use App\Models\User; 14 + use Carbon\Carbon; 15 + use Database\Factories\Factory; 9 16 10 - $factory->define(Event::class, function (Faker\Generator $faker) { 11 - return [ 12 - 'match_id' => function () { 13 - return factory(LegacyMatch::class)->create()->user_id; 14 - }, 15 - 'user_id' => function () { 16 - return App\Models\User::factory()->create()->user_id; 17 - }, 18 - 'timestamp' => Carbon\Carbon::now(), 19 - ]; 20 - }); 17 + class EventFactory extends Factory 18 + { 19 + protected $model = Event::class; 21 20 22 - $factory->state(Event::class, 'create', function (Faker\Generator $faker) { 23 - return [ 24 - 'user_id' => null, 25 - 'text' => 'CREATE', 26 - ]; 27 - }); 21 + public function definition(): array 22 + { 23 + return [ 24 + 'match_id' => LegacyMatch::factory(), 25 + 'user_id' => User::factory(), 26 + 'timestamp' => fn() => Carbon::now(), 27 + ]; 28 + } 28 29 29 - $factory->state(Event::class, 'disband', function (Faker\Generator $faker) { 30 - return [ 31 - 'user_id' => null, 32 - 'text' => 'DISBAND', 33 - ]; 34 - }); 30 + public function stateCreate(): static 31 + { 32 + return $this->state([ 33 + 'user_id' => null, 34 + 'text' => 'CREATE', 35 + ]); 36 + } 35 37 36 - $factory->state(Event::class, 'join', function (Faker\Generator $faker) { 37 - return [ 38 - 'text' => 'JOIN', 39 - ]; 40 - }); 38 + public function disband(): static 39 + { 40 + return $this->state([ 41 + 'user_id' => null, 42 + 'text' => 'DISBAND', 43 + ]); 44 + } 41 45 42 - $factory->state(Event::class, 'part', function (Faker\Generator $faker) { 43 - return [ 44 - 'text' => 'PART', 45 - ]; 46 - }); 46 + public function join(): static 47 + { 48 + return $this->state(['text' => 'JOIN']); 49 + } 47 50 48 - $factory->state(Event::class, 'game', function (Faker\Generator $faker) { 49 - return [ 50 - 'text' => 'test game', 51 - 'user_id' => null, 52 - 'game_id' => function () { 53 - return factory(Game::class)->states('in_progress')->create()->game_id; 54 - }, 55 - ]; 56 - }); 51 + public function part(): static 52 + { 53 + return $this->state(['text' => 'PART']); 54 + } 55 + 56 + public function game(): static 57 + { 58 + return $this->state([ 59 + 'text' => 'test game', 60 + 'user_id' => null, 61 + 'game_id' => Game::factory()->inProgress(), 62 + ]); 63 + } 64 + }
+28 -20
database/factories/LegacyMatch/GameFactory.php
··· 3 3 // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4 4 // See the LICENCE file in the repository root for full licence text. 5 5 6 + declare(strict_types=1); 7 + 8 + namespace Database\Factories\LegacyMatch; 9 + 10 + use App\Models\Beatmap; 6 11 use App\Models\LegacyMatch\Game; 7 12 use Carbon\Carbon; 13 + use Database\Factories\Factory; 8 14 9 - $factory->define(Game::class, function (Faker\Generator $faker) { 10 - $beatmap = App\Models\Beatmap::inRandomOrder()->first(); 15 + class GameFactory extends Factory 16 + { 17 + protected $model = Game::class; 11 18 12 - return [ 13 - 'beatmap_id' => $beatmap->beatmap_id, 14 - 'start_time' => Carbon::now()->subSeconds($beatmap->total_length), 15 - 'play_mode' => $beatmap->playmode, 16 - 'scoring_type' => $faker->numberBetween(0, 3), 17 - 'team_type' => $faker->numberBetween(0, 3), 18 - ]; 19 - }); 19 + public function definition(): array 20 + { 21 + return [ 22 + 'beatmap_id' => Beatmap::factory(), 23 + 'start_time' => fn(array $attributes) => Carbon::now()->subSeconds(Beatmap::find($attributes['beatmap_id'])->total_length), 24 + 'play_mode' => fn(array $attributes) => Beatmap::find($attributes['beatmap_id'])->playmode, 25 + 'scoring_type' => fn() => $this->faker->numberBetween(0, 3), 26 + 'team_type' => fn() => $this->faker->numberBetween(0, 3), 27 + ]; 28 + } 20 29 21 - $factory->state(Game::class, 'in_progress', function (Faker\Generator $faker) { 22 - return [ 23 - 'end_time' => null, 24 - ]; 25 - }); 30 + public function inProgress(): static 31 + { 32 + return $this->state(['end_time' => null]); 33 + } 26 34 27 - $factory->state(Game::class, 'complete', function (Faker\Generator $faker) { 28 - return [ 29 - 'end_time' => Carbon::now(), 30 - ]; 31 - }); 35 + public function complete(): static 36 + { 37 + return $this->state(['end_time' => fn() => Carbon::now()]); 38 + } 39 + }
+25 -19
database/factories/LegacyMatch/LegacyMatchFactory.php
··· 3 3 // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4 4 // See the LICENCE file in the repository root for full licence text. 5 5 6 + namespace Database\Factories\LegacyMatch; 7 + 6 8 use App\Models\LegacyMatch\LegacyMatch; 9 + use Carbon\Carbon; 10 + use Database\Factories\Factory; 7 11 8 - $factory->define(LegacyMatch::class, function (Faker\Generator $faker) { 9 - return [ 10 - 'name' => function () use ($faker) { 11 - return $faker->sentence(); 12 - }, 13 - 'start_time' => Carbon\Carbon::now(), 14 - 'private' => 0, 15 - ]; 16 - }); 12 + class LegacyMatchFactory extends Factory 13 + { 14 + protected $model = LegacyMatch::class; 17 15 18 - $factory->state(LegacyMatch::class, 'private', function (Faker\Generator $faker) { 19 - return [ 20 - 'private' => 1, 21 - ]; 22 - }); 16 + public function definition(): array 17 + { 18 + return [ 19 + 'name' => fn() => $this->faker->sentence(), 20 + 'start_time' => fn() => Carbon::now(), 21 + 'private' => 0, 22 + ]; 23 + } 23 24 24 - $factory->state(LegacyMatch::class, 'tourney', function (Faker\Generator $faker) { 25 - return [ 26 - 'keep_forever' => 1, 27 - ]; 28 - }); 25 + public function private(): static 26 + { 27 + return $this->state(['private' => 1]); 28 + } 29 + 30 + public function tourney(): static 31 + { 32 + return $this->state(['keep_forever' => 1]); 33 + } 34 + }
+2 -2
tests/Browser/SanityTest.php
··· 239 239 ]); 240 240 241 241 // factory for matches 242 - self::$scaffolding['match'] = factory(LegacyMatch\LegacyMatch::class)->create(); 243 - self::$scaffolding['event'] = factory(LegacyMatch\Event::class)->states('join')->create([ 242 + self::$scaffolding['match'] = LegacyMatch\LegacyMatch::factory()->create(); 243 + self::$scaffolding['event'] = LegacyMatch\Event::factory()->join()->create([ 244 244 'match_id' => self::$scaffolding['match']->getKey(), 245 245 ]); 246 246
+7 -7
tests/Controllers/MatchesControllerTest.php
··· 35 35 36 36 public function testPublicMatchLoggedInParticipated() // OK 37 37 { 38 - factory(Event::class)->states('join')->create([ 38 + Event::factory()->join()->create([ 39 39 'user_id' => $this->user->user_id, 40 40 'match_id' => $this->publicMatch->match_id, 41 41 ]); ··· 64 64 65 65 public function testPrivateMatchLoggedInHost() // OK 66 66 { 67 - factory(Event::class)->states('create')->create([ 67 + Event::factory()->stateCreate()->create([ 68 68 'user_id' => $this->user->user_id, 69 69 'match_id' => $this->privateMatch->match_id, 70 70 ]); ··· 77 77 78 78 public function testPrivateMatchLoggedInParticipated() // OK 79 79 { 80 - factory(Event::class)->states('join')->create([ 80 + Event::factory()->join()->create([ 81 81 'user_id' => $this->user->user_id, 82 82 'match_id' => $this->privateMatch->match_id, 83 83 ]); ··· 94 94 95 95 $this->user = User::factory()->create(); 96 96 97 - $this->publicMatch = factory(LegacyMatch::class)->create(); 98 - factory(Event::class)->states('create')->create([ 97 + $this->publicMatch = LegacyMatch::factory()->create(); 98 + Event::factory()->stateCreate()->create([ 99 99 'match_id' => $this->publicMatch->match_id, 100 100 ]); 101 101 $this->publicMatchRoute = route('matches.show', $this->publicMatch->match_id); 102 102 103 - $this->privateMatch = factory(LegacyMatch::class)->states('private')->create(); 104 - factory(Event::class)->states('create')->create([ 103 + $this->privateMatch = LegacyMatch::factory()->private()->create(); 104 + Event::factory()->stateCreate()->create([ 105 105 'match_id' => $this->privateMatch->match_id, 106 106 ]); 107 107 $this->privateMatchRoute = route('matches.show', $this->privateMatch->match_id);