81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use App\Actions\JoinWorld;
|
||
|
|
use App\Actions\QueueUpgrade;
|
||
|
|
use App\Models\GameEvent;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
use App\Models\World;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
use Database\Seeders\WorldSeeder;
|
||
|
|
|
||
|
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-11-03 12:00:00')));
|
||
|
|
|
||
|
|
function farmsMigration(): object
|
||
|
|
{
|
||
|
|
return require database_path('migrations/2026_11_03_180000_farms_feed_their_heads.php');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** A dev world village with more soldiers than its farm 1 feeds, as the old rule allowed. */
|
||
|
|
function overgrown(): Village
|
||
|
|
{
|
||
|
|
(new WorldSeeder)->run();
|
||
|
|
$village = app(JoinWorld::class)(User::factory()->create(), World::where('slug', 'dev')->value('id'));
|
||
|
|
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 100]);
|
||
|
|
|
||
|
|
return $village->fresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
function farmOf(Village $village): int
|
||
|
|
{
|
||
|
|
return $village->buildings()->where('type', 'farm')->value('level');
|
||
|
|
}
|
||
|
|
|
||
|
|
it('raises the farm to the lowest level that feeds the soldiers and the workers', function () {
|
||
|
|
// 100 spearmen and 5 workers: farm 7 leaves 80% of 114.4 = 91 heads, farm 8 leaves
|
||
|
|
// 114 for 100 spearmen and 7 workers (the farm needs 4 at level 8).
|
||
|
|
$village = overgrown();
|
||
|
|
$fine = startVillage();
|
||
|
|
|
||
|
|
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 8]);
|
||
|
|
|
||
|
|
$state = shown($village);
|
||
|
|
expect(farmOf($village))->toBe(8)
|
||
|
|
->and($village->fresh()->points)->toBe(10 + 6 + 18)
|
||
|
|
->and($state['population'])->toMatchArray(['villagers' => 14, 'workers' => 7])
|
||
|
|
->and($state['rates']['food'])->toBeGreaterThan(0)
|
||
|
|
->and(farmOf($fine))->toBe(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('cancels orders queued on the raised farm with a full refund', function () {
|
||
|
|
$village = overgrown();
|
||
|
|
$farm = $village->buildings->firstWhere('type', 'farm');
|
||
|
|
app(QueueUpgrade::class)(owner($village), $village->id, $farm->id);
|
||
|
|
expect($village->fresh()->wood)->toBe(443.0);
|
||
|
|
|
||
|
|
farmsMigration()->raiseFarms(now()->toImmutable());
|
||
|
|
|
||
|
|
$village->refresh();
|
||
|
|
expect($village->wood)->toBe(500.0)
|
||
|
|
->and($village->orders()->count())->toBe(0)
|
||
|
|
->and(GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->whereNull('processed_at')->count())->toBe(0)
|
||
|
|
->and(farmOf($village))->toBe(8);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('changes nothing on a second run, and nothing at all on SQLite', function () {
|
||
|
|
$village = overgrown();
|
||
|
|
farmsMigration()->up();
|
||
|
|
expect(farmOf($village))->toBe(1);
|
||
|
|
|
||
|
|
farmsMigration()->raiseFarms(now()->toImmutable());
|
||
|
|
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([])
|
||
|
|
->and(farmOf($village))->toBe(8);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('raises a farm no higher than its max level', function () {
|
||
|
|
$village = overgrown();
|
||
|
|
$village->troops()->update(['count' => 100_000]);
|
||
|
|
|
||
|
|
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 30]);
|
||
|
|
});
|