Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
venix12 committed Dec 27, 2024
1 parent d723a85 commit 08b4b34
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/Models/SeasonRoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -16,5 +17,7 @@
*/
class SeasonRoom extends Model
{
use HasFactory;

public $timestamps = false;
}
4 changes: 4 additions & 0 deletions app/Models/SeasonScoreFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* @property int $id
* @property float $factor
* @property int $season_id
*/
class SeasonScoreFactor extends Model
{
use HasFactory;

public $timestamps = false;
}
21 changes: 21 additions & 0 deletions database/factories/SeasonRoomFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories;

use App\Models\SeasonRoom;

class SeasonRoomFactory extends Factory
{
protected $model = SeasonRoom::class;

public function definition(): array
{
// pivot table...
return [];
}
}
22 changes: 22 additions & 0 deletions database/factories/SeasonScoreFactorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories;

use App\Models\SeasonScoreFactor;

class SeasonScoreFactorFactory extends Factory
{
protected $model = SeasonScoreFactor::class;

public function definition(): array
{
return [
'factor' => 1,
];
}
}
202 changes: 202 additions & 0 deletions tests/Models/UserSeasonScoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Tests\Models;

use App\Models\Multiplayer\PlaylistItem;
use App\Models\Multiplayer\Room;
use App\Models\Season;
use App\Models\SeasonRoom;
use App\Models\SeasonScoreFactor;
use App\Models\User;
use App\Models\UserSeasonScore;
use Tests\TestCase;

class UserSeasonScoreTest extends TestCase
{
private Season $season;
private User $user;

public function testAddMultipleScores(): void
{
foreach ([1, 0.75, 0.5] as $factor) {
SeasonScoreFactor::factory()->create([
'factor' => $factor,
'season_id' => $this->season,
]);
}

$this->createRoomWithPlay(10);

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 10); // 10*1

$this->createRoomWithPlay(15);

$userScore->refresh();
$this->assertSame($userScore->total_score, 22.5); // 15*1 + 10*0.75

$this->createRoomWithPlay(25);

$userScore->refresh();
$this->assertSame($userScore->total_score, 41.25); // 25*1 + 15*0.75 + 10*0.5
}

public function testAddMultipleScoresWithChildrenRooms(): void
{
foreach ([1, 0.75, 0.5] as $factor) {
SeasonScoreFactor::factory()->create([
'factor' => $factor,
'season_id' => $this->season,
]);
}

$firstRoom = $this->createRoomWithPlay(10);

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 10); // 10*1

$this->createRoomWithPlay(15, $firstRoom->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, (float) 15); // 15*1

$secondRoom = $this->createRoomWithPlay(20);

$userScore->refresh();
$this->assertSame($userScore->total_score, 31.25); // 20*1 + 15*0.75

$this->createRoomWithPlay(20, $secondRoom->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, 31.25); // 20*1 + 15*0.75

$thirdRoom = $this->createRoomWithPlay(10);

$userScore->refresh();
$this->assertSame($userScore->total_score, 36.25); // 20*1 + 15*0.75 + 10*0.5

$this->createRoomWithPlay(30, $thirdRoom->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, 52.5); // 30*1 + 20*0.75 + 15*0.5
}

public function testAddHigherScoreInChildRoom(): void
{
SeasonScoreFactor::factory()->create(['season_id' => $this->season]);

$room = $this->createRoomWithPlay(10);

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 10);

$this->createRoomWithPlay(15, $room->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, (float) 15);
}

public function testAddHigherScoreInParentRoom(): void
{
SeasonScoreFactor::factory()->create(['season_id' => $this->season]);

$room = $this->createRoomWithPlay(15);

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 15);

$this->createRoomWithPlay(10, $room->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, (float) 15);
}

public function testAddSameScoreInChildAndParentRoom(): void
{
SeasonScoreFactor::factory()->create(['season_id' => $this->season]);

$room = $this->createRoomWithPlay(10);

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 10);

$this->createRoomWithPlay(10, $room->getKey());

$userScore->refresh();
$this->assertSame($userScore->total_score, (float) 10);
}

public function testAddScoreInChildRoomOnly(): void
{
SeasonScoreFactor::factory()->create(['season_id' => $this->season]);

$room = $this->createRoom();
$this->createRoomWithPlay(10, $room->getKey());

$userScore = UserSeasonScore::where('user_id', $this->user->getKey())
->where('season_id', $this->season->getKey())
->first();

$this->assertSame($userScore->total_score, (float) 10);
}

protected function setUp(): void
{
parent::setUp();

$this->season = Season::factory()->create();
$this->user = User::factory()->create();
}

private function createRoom(?int $parentId = null): Room
{
$room = Room::factory()->create([
'category' => 'spotlight',
'parent_id' => $parentId,
]);

SeasonRoom::factory()->create([
'room_id' => $room,
'season_id' => $this->season,
]);

return $room;
}

private function createRoomWithPlay(float $totalScore, ?int $parentId = null): Room
{
$room = $this->createRoom($parentId);

$playlistItem = PlaylistItem::factory()->create([
'owner_id' => $room->host,
'room_id' => $room,
]);

$this->roomAddPlay($this->user, $playlistItem, [
'passed' => true,
'total_score' => $totalScore,
]);

return $room;
}
}

0 comments on commit 08b4b34

Please sign in to comment.