128 lines
5.6 KiB
PHP
128 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BattleForge\Tests\Unit\Application;
|
|
|
|
use BattleForge\Application\ScenarioDraft;
|
|
use BattleForge\Domain\Archetype;
|
|
use BattleForge\Domain\Battlefield;
|
|
use BattleForge\Domain\DeploymentZone;
|
|
use BattleForge\Domain\Position;
|
|
use BattleForge\Domain\Scenario;
|
|
use BattleForge\Domain\Terrain;
|
|
use BattleForge\Domain\UnitState;
|
|
use BattleForge\Domain\VictoryCondition;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ScenarioDraftTest extends TestCase
|
|
{
|
|
public function testItBuildsAScenarioFromFormFields(): void
|
|
{
|
|
$post = [
|
|
'id' => 'demo',
|
|
'name' => 'Demo',
|
|
'battlefieldWidth' => '8',
|
|
'battlefieldHeight' => '8',
|
|
'battlefieldTerrain' => [
|
|
'0:0' => 'forest',
|
|
],
|
|
'teamA' => [
|
|
'units' => [
|
|
[
|
|
'id' => 'a1',
|
|
'x' => '0',
|
|
'y' => '0',
|
|
'archetype' => 'defender',
|
|
'maxHealth' => '12',
|
|
'attack' => '3',
|
|
'defense' => '4',
|
|
'speed' => '2',
|
|
'abilities' => ['buff'],
|
|
'image' => '/assets/placeholders/defender.png',
|
|
],
|
|
['id' => 'a2', 'x' => '1', 'y' => '0', 'archetype' => 'defender', 'maxHealth' => '12', 'attack' => '3', 'defense' => '4', 'speed' => '2', 'abilities' => [], 'image' => ''],
|
|
['id' => 'a3', 'x' => '2', 'y' => '0', 'archetype' => 'defender', 'maxHealth' => '12', 'attack' => '3', 'defense' => '4', 'speed' => '2', 'abilities' => [], 'image' => ''],
|
|
],
|
|
],
|
|
'teamB' => [
|
|
'units' => [
|
|
['id' => 'b1', 'x' => '7', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => ['area_damage'], 'image' => ''],
|
|
['id' => 'b2', 'x' => '6', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => [], 'image' => ''],
|
|
['id' => 'b3', 'x' => '5', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => [], 'image' => ''],
|
|
],
|
|
],
|
|
'victoryCondition' => 'eliminate_all',
|
|
'holdRoundsRequired' => '1',
|
|
];
|
|
|
|
$draft = ScenarioDraft::fromPost($post);
|
|
$scenario = $draft->toScenario();
|
|
|
|
self::assertSame('demo', $scenario->id);
|
|
self::assertSame(8, $scenario->battlefield->width);
|
|
self::assertSame(Terrain::Forest, $scenario->battlefield->terrainAt(new Position(0, 0)));
|
|
self::assertCount(6, $scenario->units);
|
|
self::assertSame('a1', $scenario->units[0]->id);
|
|
self::assertSame(Archetype::Defender, $scenario->units[0]->archetype);
|
|
self::assertSame(['buff'], $scenario->units[0]->abilities);
|
|
self::assertSame(VictoryCondition::EliminateAll, $scenario->victoryCondition);
|
|
}
|
|
|
|
public function testItRejectsUnknownArchetype(): void
|
|
{
|
|
$post = $this->validPost();
|
|
$post['teamA']['units'][0]['archetype'] = 'rogue';
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
ScenarioDraft::fromPost($post)->toScenario();
|
|
}
|
|
|
|
public function testItRejectsUnknownTerrain(): void
|
|
{
|
|
$post = $this->validPost();
|
|
$post['battlefieldTerrain'] = ['0:0' => 'lava'];
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
ScenarioDraft::fromPost($post)->toScenario();
|
|
}
|
|
|
|
public function testItRejectsUnknownVictoryCondition(): void
|
|
{
|
|
$post = $this->validPost();
|
|
$post['victoryCondition'] = 'first_blood';
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
ScenarioDraft::fromPost($post)->toScenario();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function validPost(): array
|
|
{
|
|
return [
|
|
'id' => 'demo',
|
|
'name' => 'Demo',
|
|
'battlefieldWidth' => '8',
|
|
'battlefieldHeight' => '8',
|
|
'teamA' => [
|
|
'units' => [
|
|
['id' => 'a1', 'x' => '0', 'y' => '0', 'archetype' => 'defender', 'maxHealth' => '12', 'attack' => '3', 'defense' => '4', 'speed' => '2', 'abilities' => [], 'image' => ''],
|
|
['id' => 'a2', 'x' => '1', 'y' => '0', 'archetype' => 'defender', 'maxHealth' => '12', 'attack' => '3', 'defense' => '4', 'speed' => '2', 'abilities' => [], 'image' => ''],
|
|
['id' => 'a3', 'x' => '2', 'y' => '0', 'archetype' => 'defender', 'maxHealth' => '12', 'attack' => '3', 'defense' => '4', 'speed' => '2', 'abilities' => [], 'image' => ''],
|
|
],
|
|
],
|
|
'teamB' => [
|
|
'units' => [
|
|
['id' => 'b1', 'x' => '7', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => [], 'image' => ''],
|
|
['id' => 'b2', 'x' => '6', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => [], 'image' => ''],
|
|
['id' => 'b3', 'x' => '5', 'y' => '7', 'archetype' => 'striker', 'maxHealth' => '8', 'attack' => '5', 'defense' => '2', 'speed' => '3', 'abilities' => [], 'image' => ''],
|
|
],
|
|
],
|
|
'victoryCondition' => 'eliminate_all',
|
|
'holdRoundsRequired' => '1',
|
|
];
|
|
}
|
|
}
|