1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Models;
7
8use App\Exceptions\InvariantException;
9use App\Models\Beatmap;
10use App\Models\Tournament;
11use App\Models\User;
12use App\Models\UserStatistics;
13use Carbon\Carbon;
14use Tests\TestCase;
15
16class TournamentTest extends TestCase
17{
18 public function testTournamentUserIsValidRank()
19 {
20 $user = User::factory()->create();
21 $playModeInt = Beatmap::MODES['osu'];
22 $tournament = Tournament::factory()->create([
23 'play_mode' => $playModeInt,
24 'rank_min' => 1,
25 'rank_max' => 100,
26 ]);
27
28 $stats = $user->statisticsOsu()->create([
29 'rank_score_index' => $tournament->rank_max + 1,
30 'rank_score' => 1,
31 ]);
32
33 $this->assertFalse($tournament->isValidRank($user->fresh()));
34
35 $stats->update(['rank_score_index' => $tournament->rank_max]);
36
37 $this->assertTrue($tournament->isValidRank($user->fresh()));
38 }
39
40 public function testTournamentUserIsValidRankWithVariant()
41 {
42 $user = User::factory()->create();
43 $playModeInt = Beatmap::MODES['mania'];
44 $playModeVariant = Beatmap::VARIANTS['mania'][0];
45 $tournament = Tournament::factory()->create([
46 'play_mode' => $playModeInt,
47 'play_mode_variant' => $playModeVariant,
48 'rank_min' => 1,
49 'rank_max' => 100,
50 ]);
51
52 $user->statisticsMania()->create([
53 'rank_score_index' => $tournament->rank_max,
54 'rank_score' => 1,
55 ]);
56
57 $this->assertFalse($tournament->isValidRank($user->fresh()));
58
59 $stats = UserStatistics\Model
60 ::getClass('mania', $playModeVariant)
61 ::create([
62 'user_id' => $user->getKey(),
63 'rank_score_index' => $tournament->rank_max + 1,
64 'rank_score' => 1,
65 ]);
66
67 $this->assertFalse($tournament->isValidRank($user->fresh()));
68
69 $stats->update(['rank_score_index' => $tournament->rank_max]);
70
71 $this->assertTrue($tournament->isValidRank($user->fresh()));
72 }
73
74 public function testRegister()
75 {
76 $user = User::factory()->create();
77 $tournament = Tournament::factory()->create();
78
79 $this->expectCountChange(fn () => $tournament->registrations()->count(), 1);
80
81 $tournament->register($user);
82 }
83
84 public function testRegisterWhenRunning()
85 {
86 $user = User::factory()->create();
87 $tournament = Tournament::factory()->create(['start_date' => Carbon::now()->subDays(1)]);
88
89 $this->expectCountChange(fn () => $tournament->registrations()->count(), 0);
90 $this->expectException(InvariantException::class);
91
92 $tournament->register($user);
93 }
94
95 public function testUnregister()
96 {
97 $user = User::factory()->create();
98 $tournament = Tournament::factory()->create();
99 $tournament->registrations()->create(['user_id' => $user->getKey()]);
100
101 $this->expectCountChange(fn () => $tournament->registrations()->count(), -1);
102
103 $tournament->unregister($user);
104 }
105
106 public function testUnregisterWhenRunning()
107 {
108 $user = User::factory()->create();
109 $tournament = Tournament::factory()->create(['start_date' => Carbon::now()->subDays(1)]);
110 $tournament->registrations()->create(['user_id' => $user->getKey()]);
111
112 $this->expectCountChange(fn () => $tournament->registrations()->count(), 0);
113 $this->expectException(InvariantException::class);
114
115 $tournament->unregister($user);
116 }
117}