@ -1,29 +1,53 @@
<?php
<?php
namespace App\Tests;
namespace App\Tests;
use App\Entity\Character;
use App\Entity\Tournament;
use App\Entity\User;
use App\Factory\CharacterFactory;
use App\Factory\CharacterFactory;
use App\Factory\DojoFactory;
use App\Factory\DojoFactory;
use App\Factory\FightFactory;
use App\Factory\TournamentFactory;
use App\Factory\TournamentFactory;
use App\Factory\UserFactory;
use App\Factory\UserFactory;
use Zenstruck\Foundry\Proxy;
use DateTimeImmutable;
use DateTimeZone;
// @note No need to create tournaments via API Endpoint ... will be done in cronjob
class TournamentTest extends AbstractTest
class TournamentTest extends AbstractTest
{
{
// No need to create tournaments via API Endpoint ... will be done in cronjob
private function createTournament(string $offset, array $characters = array()): Tournament|Proxy
public function testRegisterCharacter(): void
{
{
$tournament = TournamentFactory::createOne();
$tournamentStartDate = new DateTimeImmutable("now", new DateTimeZone("UTC"));
$tournamentStartDate = $tournamentStartDate->sub(\DateInterval::createFromDateString($offset));
return TournamentFactory::createOne([
'startDate' => $tournamentStartDate,
'characters' => $characters
]);
}
private function createCharacter(User|Proxy $user): Character|Proxy
{
$dojo = DojoFactory::createOne([
$dojo = DojoFactory::createOne([
'owner' => UserFactory::createOne()
'owner' => $user
]);
]);
$character = CharacterFactory::createOne([
$character = CharacterFactory::createOne([
'dojo' => $dojo
'dojo' => $dojo
]);
]);
$response = static::createClientWithToken($dojo->getOwner()->authName)->request('POST',
return $character;
'/api/tournament_registrations',
}
public function testRegisterCharacter(): void
{
$tournament = $this->createTournament("-5 min");
$user = UserFactory::createOne();
$character = $this->createCharacter($user);
static::createClientWithToken($user->authName)->request('POST', '/api/tournament_registrations',
[
[
'json' => [
'json' => [
'tournament' => $this->getIri($tournament),
'tournament' => $this->getIri($tournament),
@ -32,40 +56,142 @@ class TournamentTest extends AbstractTest
]);
]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseStatusCodeSame(201);
}
public function testRegisterCharacterDifferentUser(): void
{
$tournament = $this->createTournament("-5 min");
$characterOwner = UserFactory::createOne();
$character = $this->createCharacter($characterOwner);
$user = UserFactory::createOne();
static::createClientWithToken($user->authName)->request('POST', '/api/tournament_registrations',
[
'json' => [
'tournament' => $this->getIri($tournament),
'character' => $this->getIri($character)
]
]);
$this->assertArrayHasKey('id', $response->toArray());
$this->assertResponseStatusCodeSame(422);
}
public function testRegisterCharacterOnPastTournament(): void
{
$tournament = $this->createTournament("5 min");
$user = UserFactory::createOne();
$character = $this->createCharacter($user);
static::createClientWithToken($user->authName)->request('POST', '/api/tournament_registrations',
[
'json' => [
'tournament' => $this->getIri($tournament),
'character' => $this->getIri($character)
]
]);
$this->assertResponseStatusCodeSame(422);
}
}
public function testRegisterCharacterNotPossibleTwice(): void
public function testRegisterCharacterNotPossibleTwice(): void
{}
{
$tournament = $this->createTournament("-5 min");
$user = UserFactory::createOne();
$character = $this->createCharacter($user);
public function testRegisterCharacterOnPastTournament(): void
static::createClientWithToken($user->authName)->request('POST', '/api/tournament_registrations',
{}
[
'json' => [
'tournament' => $this->getIri($tournament),
'character' => $this->getIri($character)
]
]);
static::createClientWithToken($user->authName)->request('POST', '/api/tournament_registrations',
[
'json' => [
'tournament' => $this->getIri($tournament),
'character' => $this->getIri($character)
]
]);
$this->assertEquals(1,
$this->getEntityManager()
->find(Tournament::class, $tournament->getId())
->getCharacters()
->count());
$this->assertResponseStatusCodeSame(400);
}
public function testShowRegisteredCharacters(): void
public function testShowRegisteredCharacters(): void
{}
{
$tournament = $this->createTournament("-5 min", CharacterFactory::createMany(4));
$response = static::createClientWithToken()->request('GET', $this->getIri($tournament));
$this->assertResponseStatusCodeSame(200);
$this->assertCount(4, $response->toArray()['characters']);
}
public function testListTournaments(): void
public function testListTournaments(): void
{}
{
TournamentFactory::createMany(5);
$response = static::createClientWithToken()->request('GET', '/api/tournaments');
$this->assertResponseStatusCodeSame(200);
$this->assertCount(5, $response->toArray());
}
/**
/**
* Status is ...
* Status is ...
* meta data like when it is starting, name, "location", Winner (nullable), etc.
* meta data like when it is starting, name, "location", Winner (nullable), etc.
*/
*/
public function testTournamentStatus(): void
public function testTournamentStatus(): void
{}
{
$tournament = TournamentFactory::createOne([
'winner' => CharacterFactory::createOne()
]);
$response = static::createClientWithToken()->request('GET', $this->getIri($tournament));
$this->assertResponseStatusCodeSame(200);
$arrayResponse = $response->toArray();
$this->assertArrayHasKey('winner', $arrayResponse);
}
// /api/tournament/{id}/fights
// readableLink: true -> participant ids and winner
public function testTournamentFights(): void
public function testTournamentFights(): void
{}
{
$tournament = TournamentFactory::createOne([
'fights' => FightFactory::createMany(16)
]);
$response = static::createClientWithToken()->request('GET', $this->getIri($tournament) . '/fights');
$this->assertResponseStatusCodeSame(200);
$this->assertCount(16, $response->toArray());
}
// /api/tournament/{id}/character/{id}/fights
public function testTournamentFightsForCharacter(): void
public function testTournamentFightsForCharacter(): void
{}
{
$characters = CharacterFactory::createMany(16);
$tournament = TournamentFactory::createOne([
'characters' => $characters
]);
// /api/tournament/{id}/ranking
$winner = $characters[0];
public function testTournamentRanking(): void
for ($i = 1; $i < count ( $ characters ) ; + + $ i ) {
{}
$tournament->addFight(
FightFactory::createOne([
'winner' => $winner,
'characters' => array(
$winner,
$characters[$i]
)
])->object());
}
$tournament->save();
$response = static::createClientWithToken()->request('GET',
$this->getIri($tournament) . '/characters/' . $winner->getId()
->toBase32() . '/fights');
$this->assertCount(15, $response->toArray());
}
}
}