game-village-politics-backend/tests/Unit/Game/UnitRulesTest.php

39 lines
1.5 KiB
PHP

<?php
use App\Game\ConfigLint;
use App\Game\Military\UnitRules;
it('knows where each unit is trained', function () {
$units = new UnitRules(gameConfig());
expect($units->trainedAt('barracks'))->toBe(['spear', 'sword', 'archer'])
->and($units->trainedAt('stable'))->toBe(['scout', 'light_cavalry', 'heavy_cavalry'])
->and($units->trainedAt('academy'))->toBe(['nobleman'])
->and($units->trainedAt('farm'))->toBe([]);
});
it('multiplies cost by count and sums upkeep', function () {
$units = new UnitRules(gameConfig());
expect($units->cost('spear', 10))->toBe(['wood' => 500, 'clay' => 300, 'iron' => 100])
->and($units->upkeepOf(['spear' => 10, 'heavy_cavalry' => 2, 'nobleman' => 1]))->toBe(10.0 + 8 + 10);
});
it('trains faster at a higher level and slower when understaffed', function () {
$units = new UnitRules(gameConfig());
$fast = new UnitRules(gameConfig(['world' => ['speed' => 100]]));
expect($units->seconds('spear', 1, 1.0))->toBe(1020)
->and($units->seconds('spear', 11, 1.0))->toBe(680)
->and($units->seconds('spear', 1, 0.5))->toBe(2040)
->and($fast->seconds('spear', 1, 1.0))->toBe(11);
});
it('lints units that point at unknown buildings or eat nothing', function () {
$config = gameConfig(['units' => ['spear' => ['building' => 'tavern', 'upkeep' => 0]]]);
expect(ConfigLint::check($config))
->toContain('[spear] is trained at unknown building [tavern].')
->toContain('[spear] must eat food (upkeep > 0), or armies grow without limit.');
});