new: pkg: add test cases to back-end w/ real database connection in it #10
This commit is contained in:
89
tests/Dto/ProfileChartDataDtoTest.php
Normal file
89
tests/Dto/ProfileChartDataDtoTest.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Dto;
|
||||
|
||||
use App\Dto\ProfileChartDataDto;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ProfileChartDataDtoTest
|
||||
*
|
||||
* @package App\Tests\Dto
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 21.
|
||||
*/
|
||||
#[TestDox('Profile Chart Data Dto')]
|
||||
class ProfileChartDataDtoTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
#[TestDox('Json serialize returns all properties')]
|
||||
public function jsonSerializeReturnsAllProperties(): void
|
||||
{
|
||||
$months = ['Jan', 'Feb', 'Mar'];
|
||||
$wins = [5, 8, 10];
|
||||
$losses = [2, 3, 4];
|
||||
$draws = [1, 0, 1];
|
||||
$recentGames = [
|
||||
['id' => 1, 'result' => 'win'],
|
||||
['id' => 2, 'result' => 'loss'],
|
||||
];
|
||||
|
||||
$dto = new ProfileChartDataDto(
|
||||
months: $months,
|
||||
wins: $wins,
|
||||
losses: $losses,
|
||||
draws: $draws,
|
||||
pieWins: 23,
|
||||
pieLosses: 9,
|
||||
pieDraws: 2,
|
||||
recentGames: $recentGames,
|
||||
);
|
||||
|
||||
$json = $dto->jsonSerialize();
|
||||
|
||||
$this->assertSame($months, $json['months']);
|
||||
$this->assertSame($wins, $json['wins']);
|
||||
$this->assertSame($losses, $json['losses']);
|
||||
$this->assertSame($draws, $json['draws']);
|
||||
$this->assertSame(23, $json['pieWins']);
|
||||
$this->assertSame(9, $json['pieLosses']);
|
||||
$this->assertSame(2, $json['pieDraws']);
|
||||
$this->assertSame($recentGames, $json['recentGames']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Constructor with empty arrays')]
|
||||
public function constructorWithEmptyArrays(): void
|
||||
{
|
||||
$dto = new ProfileChartDataDto(
|
||||
months: [],
|
||||
wins: [],
|
||||
losses: [],
|
||||
draws: [],
|
||||
pieWins: 0,
|
||||
pieLosses: 0,
|
||||
pieDraws: 0,
|
||||
recentGames: [],
|
||||
);
|
||||
|
||||
$json = $dto->jsonSerialize();
|
||||
|
||||
$this->assertSame([], $json['months']);
|
||||
$this->assertSame([], $json['wins']);
|
||||
$this->assertSame([], $json['recentGames']);
|
||||
$this->assertSame(0, $json['pieWins']);
|
||||
}
|
||||
}
|
||||
126
tests/Dto/ProfileGameDtoTest.php
Normal file
126
tests/Dto/ProfileGameDtoTest.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Dto;
|
||||
|
||||
use App\Dto\ProfileGameDto;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ProfileGameDtoTest
|
||||
*
|
||||
* @package App\Tests\Dto
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 21.
|
||||
*/
|
||||
#[TestDox('Profile Game Dto')]
|
||||
class ProfileGameDtoTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
#[TestDox('Json serialize returns all properties')]
|
||||
public function jsonSerializeReturnsAllProperties(): void
|
||||
{
|
||||
$dto = new ProfileGameDto(
|
||||
id: 1,
|
||||
uuid: '550e8400-e29b-41d4-a716-446655440000',
|
||||
redName: 'RedPlayer',
|
||||
blueName: 'BluePlayer',
|
||||
redAvatar: '/uploads/avatars/red.png',
|
||||
blueAvatar: '/uploads/avatars/blue.png',
|
||||
redPoints: 10,
|
||||
bluePoints: 8,
|
||||
redExplodedBomb: false,
|
||||
blueExplodedBomb: true,
|
||||
resign: null,
|
||||
created: '2026-04-20 10:00',
|
||||
date: '2026-04-20 10:30',
|
||||
isRed: true,
|
||||
result: 'win',
|
||||
myPoints: 10,
|
||||
oppPoints: 8,
|
||||
redBonusPoints: 5.5,
|
||||
blueBonusPoints: 2.0,
|
||||
redBonusStats: ['blindHits' => 2, 'chainBest' => 3],
|
||||
blueBonusStats: ['blindHits' => 1, 'chainBest' => 2],
|
||||
bothRegistered: true,
|
||||
);
|
||||
|
||||
$json = $dto->jsonSerialize();
|
||||
|
||||
$this->assertSame(1, $json['id']);
|
||||
$this->assertSame('550e8400-e29b-41d4-a716-446655440000', $json['uuid']);
|
||||
$this->assertSame('RedPlayer', $json['redName']);
|
||||
$this->assertSame('BluePlayer', $json['blueName']);
|
||||
$this->assertSame('/uploads/avatars/red.png', $json['redAvatar']);
|
||||
$this->assertSame('/uploads/avatars/blue.png', $json['blueAvatar']);
|
||||
$this->assertSame(10, $json['redPoints']);
|
||||
$this->assertSame(8, $json['bluePoints']);
|
||||
$this->assertFalse($json['redExplodedBomb']);
|
||||
$this->assertTrue($json['blueExplodedBomb']);
|
||||
$this->assertNull($json['resign']);
|
||||
$this->assertSame('2026-04-20 10:00', $json['created']);
|
||||
$this->assertSame('2026-04-20 10:30', $json['date']);
|
||||
$this->assertTrue($json['isRed']);
|
||||
$this->assertSame('win', $json['result']);
|
||||
$this->assertSame(10, $json['myPoints']);
|
||||
$this->assertSame(8, $json['oppPoints']);
|
||||
$this->assertSame(5.5, $json['redBonusPoints']);
|
||||
$this->assertSame(2.0, $json['blueBonusPoints']);
|
||||
$this->assertSame(['blindHits' => 2, 'chainBest' => 3], $json['redBonusStats']);
|
||||
$this->assertSame(['blindHits' => 1, 'chainBest' => 2], $json['blueBonusStats']);
|
||||
$this->assertTrue($json['bothRegistered']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Json serialize with null values')]
|
||||
public function jsonSerializeWithNullValues(): void
|
||||
{
|
||||
$dto = new ProfileGameDto(
|
||||
id: null,
|
||||
uuid: null,
|
||||
redName: 'Guest',
|
||||
blueName: 'Guest',
|
||||
redAvatar: null,
|
||||
blueAvatar: null,
|
||||
redPoints: null,
|
||||
bluePoints: null,
|
||||
redExplodedBomb: null,
|
||||
blueExplodedBomb: null,
|
||||
resign: null,
|
||||
created: null,
|
||||
date: null,
|
||||
isRed: false,
|
||||
result: 'draw',
|
||||
myPoints: null,
|
||||
oppPoints: null,
|
||||
redBonusPoints: 0.0,
|
||||
blueBonusPoints: 0.0,
|
||||
redBonusStats: [],
|
||||
blueBonusStats: [],
|
||||
bothRegistered: false,
|
||||
);
|
||||
|
||||
$json = $dto->jsonSerialize();
|
||||
|
||||
$this->assertNull($json['id']);
|
||||
$this->assertNull($json['uuid']);
|
||||
$this->assertNull($json['redAvatar']);
|
||||
$this->assertNull($json['blueAvatar']);
|
||||
$this->assertNull($json['redPoints']);
|
||||
$this->assertNull($json['bluePoints']);
|
||||
$this->assertSame('draw', $json['result']);
|
||||
$this->assertFalse($json['bothRegistered']);
|
||||
}
|
||||
}
|
||||
134
tests/Dto/ProfileStatsDtoTest.php
Normal file
134
tests/Dto/ProfileStatsDtoTest.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Dto;
|
||||
|
||||
use App\Dto\ProfileStatsDto;
|
||||
use App\Entity\UserStats;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ProfileStatsDtoTest
|
||||
*
|
||||
* @package App\Tests\Dto
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 21.
|
||||
*/
|
||||
#[TestDox('Profile Stats Dto')]
|
||||
class ProfileStatsDtoTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
#[TestDox('From user stats with valid stats')]
|
||||
public function fromUserStatsWithValidStats(): void
|
||||
{
|
||||
$userStats = new UserStats();
|
||||
$userStats->userId = 1;
|
||||
$userStats->totalGames = 100;
|
||||
$userStats->wins = 60;
|
||||
$userStats->losses = 30;
|
||||
$userStats->draws = 10;
|
||||
$userStats->totalMines = 500;
|
||||
$userStats->totalBonusPoints = '150.5';
|
||||
$userStats->avgBonus = '2.5';
|
||||
$userStats->bestChain = 5;
|
||||
$userStats->blindHits = 20;
|
||||
$userStats->edgeMines = 15;
|
||||
$userStats->gamesWithScores = 90;
|
||||
|
||||
$dto = ProfileStatsDto::fromUserStats($userStats);
|
||||
|
||||
$this->assertSame(100, $dto->total);
|
||||
$this->assertSame(60, $dto->wins);
|
||||
$this->assertSame(30, $dto->losses);
|
||||
$this->assertSame(10, $dto->draws);
|
||||
$this->assertSame(500, $dto->minesHit);
|
||||
$this->assertSame(67, $dto->winRate); // 60/90 * 100 = 67
|
||||
$this->assertSame(6, $dto->avgScore); // 500/90 = 5.56 -> 6
|
||||
$this->assertSame(150.5, $dto->bonusPoints);
|
||||
$this->assertSame(2.5, $dto->avgBonus);
|
||||
$this->assertSame(5, $dto->bestChain);
|
||||
$this->assertSame(20, $dto->blindHits);
|
||||
$this->assertSame(15, $dto->edgeMines);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('From user stats with null returns empty')]
|
||||
public function fromUserStatsWithNullReturnsEmpty(): void
|
||||
{
|
||||
$dto = ProfileStatsDto::fromUserStats(null);
|
||||
|
||||
$this->assertSame(0, $dto->total);
|
||||
$this->assertSame(0, $dto->wins);
|
||||
$this->assertSame(0, $dto->losses);
|
||||
$this->assertSame(0, $dto->draws);
|
||||
$this->assertSame(0, $dto->minesHit);
|
||||
$this->assertSame(0, $dto->winRate);
|
||||
$this->assertSame(0, $dto->avgScore);
|
||||
$this->assertSame(0.0, $dto->bonusPoints);
|
||||
$this->assertSame(0.0, $dto->avgBonus);
|
||||
$this->assertSame(0, $dto->bestChain);
|
||||
$this->assertSame(0, $dto->blindHits);
|
||||
$this->assertSame(0, $dto->edgeMines);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Empty returns default values')]
|
||||
public function emptyReturnsDefaultValues(): void
|
||||
{
|
||||
$dto = ProfileStatsDto::empty();
|
||||
|
||||
$this->assertSame(0, $dto->total);
|
||||
$this->assertSame(0, $dto->wins);
|
||||
$this->assertSame(0, $dto->losses);
|
||||
$this->assertSame(0, $dto->draws);
|
||||
$this->assertSame(0, $dto->minesHit);
|
||||
$this->assertSame(0, $dto->winRate);
|
||||
$this->assertSame(0, $dto->avgScore);
|
||||
$this->assertSame(0.0, $dto->bonusPoints);
|
||||
$this->assertSame(0.0, $dto->avgBonus);
|
||||
$this->assertSame(0, $dto->bestChain);
|
||||
$this->assertSame(0, $dto->blindHits);
|
||||
$this->assertSame(0, $dto->edgeMines);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Win rate calculation with no games with scores')]
|
||||
public function winRateCalculationWithNoGamesWithScores(): void
|
||||
{
|
||||
$userStats = new UserStats();
|
||||
$userStats->totalGames = 10;
|
||||
$userStats->wins = 5;
|
||||
$userStats->losses = 5;
|
||||
$userStats->draws = 0;
|
||||
$userStats->gamesWithScores = 0;
|
||||
|
||||
$dto = ProfileStatsDto::fromUserStats($userStats);
|
||||
|
||||
$this->assertSame(0, $dto->winRate);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Avg score calculation with no games with scores')]
|
||||
public function avgScoreCalculationWithNoGamesWithScores(): void
|
||||
{
|
||||
$userStats = new UserStats();
|
||||
$userStats->totalMines = 100;
|
||||
$userStats->gamesWithScores = 0;
|
||||
|
||||
$dto = ProfileStatsDto::fromUserStats($userStats);
|
||||
|
||||
$this->assertSame(0, $dto->avgScore);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user