Skip to content

Commit 5118225

Browse files
committed
wip tests
1 parent 748ca54 commit 5118225

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

app/Models/ContestJudge.php

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class ContestJudge extends Model
2626
protected $primaryKey = ':composite';
2727
protected $primaryKeys = ['user_id', 'contest_id'];
2828

29+
public function contest(): BelongsTo
30+
{
31+
return $this->belongsTo(Contest::class, 'contest_id');
32+
}
33+
2934
public function user(): BelongsTo
3035
{
3136
return $this->belongsTo(User::class, 'user_id');

database/factories/ContestFactory.php

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
namespace Database\Factories;
99

1010
use App\Models\Contest;
11+
use App\Models\ContestEntry;
12+
use App\Models\ContestJudge;
13+
use App\Models\ContestScoringCategory;
1114
use Carbon\Carbon;
1215

1316
class ContestFactory extends Factory
@@ -72,4 +75,17 @@ public function voting(): static
7275
'voting_ends_at' => fn() => Carbon::now()->addMonths(1),
7376
]);
7477
}
78+
79+
public function withEntries(int $count = 2)
80+
{
81+
\Log::debug('withEntries');
82+
return $this->has(ContestEntry::factory()->count($count), 'entries');
83+
}
84+
85+
public function withJudges(int $jugdeCount = 2, $categoryCount = 2): static
86+
{
87+
return $this->judged()
88+
->has(ContestJudge::factory()->withVotes()->count($jugdeCount))
89+
->has(ContestScoringCategory::factory()->count($categoryCount), 'scoringCategories');
90+
}
7591
}

database/factories/ContestJudgeFactory.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,43 @@
77

88
namespace Database\Factories;
99

10+
use App\Models\Contest;
11+
use App\Models\ContestEntry;
1012
use App\Models\ContestJudge;
13+
use App\Models\User;
1114

1215
class ContestJudgeFactory extends Factory
1316
{
1417
protected $model = ContestJudge::class;
1518

1619
public function definition(): array
1720
{
18-
// pivot table...
19-
return [];
21+
return [
22+
'contest_id' => Contest::factory()->judged(),
23+
'user_id' => User::factory(),
24+
];
25+
}
26+
27+
public function withVotes(): static
28+
{
29+
\Log::debug('withvotes');
30+
return $this->afterCreating(function (ContestJudge $contestJudge) {
31+
$contest = $contestJudge->contest;
32+
$categories = $contest->scoringCategories;
33+
34+
foreach ($contest->entries as $entry) {
35+
$vote = $entry->judgeVotes()->create([
36+
'comment' => $this->faker->sentence(),
37+
'user_id' => $contestJudge->user_id,
38+
]);
39+
40+
foreach ($categories as $category) {
41+
$vote->scores()->create([
42+
'contest_scoring_category_id' => $category->getKey(),
43+
'value' => rand(1, $category->max_value),
44+
]);
45+
}
46+
}
47+
});
2048
}
2149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
6+
declare(strict_types=1);
7+
8+
namespace Database\Factories;
9+
10+
use App\Models\Contest;
11+
use App\Models\ContestScoringCategory;
12+
13+
class ContestScoringCategoryFactory extends Factory
14+
{
15+
protected $model = ContestScoringCategory::class;
16+
17+
public function definition(): array
18+
{
19+
return [
20+
'contest_id' => Contest::factory(),
21+
'description' => $this->faker->sentence(),
22+
'max_value' => 10,
23+
'name' => $this->faker->word(),
24+
];
25+
}
26+
}

0 commit comments

Comments
 (0)