Files
Space-Theme/server/tests/Integration/BuildStartTest.php

90 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Integration;
use App\Shared\Clock\FixedTimeProvider;
use App\Tests\Support\TestAppFactory;
use App\Tests\Support\TestDatabase;
use PHPUnit\Framework\TestCase;
use Slim\Psr7\Factory\ServerRequestFactory;
final class BuildStartTest extends TestCase
{
public function testBuildStartAndFinalize(): void
{
$pdo = TestDatabase::create();
TestDatabase::reset($pdo);
$seed = TestDatabase::seedMinimal($pdo);
$userId = (int)$seed['user_id'];
$resources = [
'metal' => 1000.0,
'alloy' => 0.0,
'crystals' => 1000.0,
'energy' => 0.0,
'credits' => 0.0,
'population' => 0.0,
'water' => 0.0,
'deuterium' => 0.0,
'food' => 0.0,
];
$stmt = $pdo->prepare(
'INSERT INTO planets (user_id, name, class_key, planet_seed, temperature_c, modifiers, resources, last_resource_update_at)
VALUES (:user_id, :name, :class_key, :planet_seed, :temperature_c, :modifiers, :resources, :last_update)
RETURNING id'
);
$stmt->execute([
'user_id' => $userId,
'name' => 'Testworld',
'class_key' => 'temperate',
'planet_seed' => 7,
'temperature_c' => 18,
'modifiers' => json_encode(['metal' => 0.0], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'resources' => json_encode($resources, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'last_update' => '2026-02-03 00:00:00',
]);
$planetId = (int)$stmt->fetchColumn();
$stmt = $pdo->prepare(
'INSERT INTO planet_buildings (planet_id, building_key, count, level)
VALUES (:planet_id, :building_key, :count, 0)'
);
$stmt->execute(['planet_id' => $planetId, 'building_key' => 'build_center', 'count' => 1]);
$time = new FixedTimeProvider(new \DateTimeImmutable('2026-02-03 00:00:00'));
$app = TestAppFactory::create($pdo, $time);
$factory = new ServerRequestFactory();
$request = $factory->createServerRequest('POST', '/build/start')
->withHeader('Content-Type', 'application/json')
->withHeader('X-User-Id', (string)$userId);
$request->getBody()->write(json_encode([
'building_key' => 'ore_mine',
'amount' => 1,
'planet_id' => $planetId,
]));
$request->getBody()->rewind();
$response = $app->handle($request);
self::assertSame(201, $response->getStatusCode());
$body = json_decode((string)$response->getBody(), true);
self::assertSame(880.0, $body['resources']['metal']);
self::assertSame(940.0, $body['resources']['crystals']);
$time->setNow(new \DateTimeImmutable('2026-02-03 00:02:00'));
$jobsRequest = $factory->createServerRequest('GET', '/build/jobs')
->withHeader('X-User-Id', (string)$userId);
$jobsResponse = $app->handle($jobsRequest);
self::assertSame(200, $jobsResponse->getStatusCode());
$count = (int)$pdo->query("SELECT count FROM planet_buildings WHERE planet_id = {$planetId} AND building_key = 'ore_mine'")->fetchColumn();
self::assertSame(1, $count);
}
}