game-village-politics-backend/tests/Pest.php

81 lines
2.5 KiB
PHP

<?php
use App\Actions\JoinWorld;
use App\Actions\ShowVillage;
use App\Game\Buildings\BuildingState;
use App\Game\GameRuleViolation;
use App\Game\WorldConfig;
use App\Models\User;
use App\Models\Village;
use App\Models\World;
use App\Presenters\VillagePresenter;
use Database\Seeders\WorldSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
// Feature tests start without barbarians, their waves, raids and the inactivity rule unless a test asks for them,
// and the seeder makes World 1 at speed 1 instead of its live pace (config/worlds.php).
pest()->extend(TestCase::class)->use(RefreshDatabase::class)
->beforeEach(fn () => config([
'game.world.map.barbarians.per_join' => 0,
'game.world.map.barbarians.waves.enabled' => false,
'game.raids.enabled' => false,
'game.world.inactive_days' => 0,
'worlds.w1.config' => [],
]))
->in('Feature');
/** The default game config without booting Laravel, so unit tests stay pure. */
function gameConfig(array $overrides = []): WorldConfig
{
$dir = __DIR__.'/../config/game';
return WorldConfig::make([
'world' => require "$dir/world.php",
'economy' => require "$dir/economy.php",
'buildings' => require "$dir/buildings.php",
'units' => require "$dir/units.php",
'combat' => require "$dir/combat.php",
'movement' => require "$dir/movement.php",
'raids' => require "$dir/raids.php",
'tribes' => require "$dir/tribes.php",
'quests' => require "$dir/quests.php",
'market' => require "$dir/market.php",
], $overrides);
}
function building(int $id, string $type, int $level = 1, int $priority = 5): BuildingState
{
return new BuildingState($id, $type, $level, $priority);
}
/** A user with a fresh start village in World 1 (speed 1). */
function startVillage(?User $user = null): Village
{
(new WorldSeeder)->run();
return app(JoinWorld::class)($user ?? User::factory()->create(), World::where('slug', 'w1')->value('id'));
}
function owner(Village $village): User
{
return $village->player->user;
}
/** The client payload, as `village.show` would send it now. */
function shown(Village $village): array
{
return VillagePresenter::present(app(ShowVillage::class)(owner($village), $village->id), now()->toImmutable());
}
function rule(Closure $intent): string
{
try {
$intent();
} catch (GameRuleViolation $e) {
return $e->reason;
}
throw new RuntimeException('Expected a GameRuleViolation.');
}