Files

64 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Unit\EnvironmentServiceTest;
use Illuminate\Database\Eloquent\Collection;
use InvalidArgumentException;
2026-10-12 00:21:31 -04:00
use Pterodactyl\Models\Egg;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Server;
2026-10-12 00:21:31 -04:00
use Pterodactyl\Models\ServerVariable;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Tests\TestCase;
use stdClass;
uses(TestCase::class);
test('builds scalar environment values from every supported source', function () {
config()->set('pterodactyl.environment_variables', ['P_SERVER_ALLOCATION_LIMIT' => 'allocation_limit', 'CONFIGURED_CALLBACK' => static fn (Server $server): string => $server->uuid]);
$variable = new EggVariable();
2026-10-12 00:21:31 -04:00
$variable->forceFill(['id' => 1, 'env_variable' => 'GAME_MODE', 'default_value' => 'survival']);
$override = new ServerVariable();
$override->forceFill(['variable_id' => 1, 'variable_value' => 'creative']);
$service = new EnvironmentService();
2026-10-05 18:10:18 -04:00
$service->setEnvironmentKey('CUSTOM_FLAG', static fn (Server $server): bool => $server->oom_disabled);
2026-10-12 00:21:31 -04:00
expect($service->forServer(server([$variable], [$override])))->toBe(['GAME_MODE' => 'creative', 'STARTUP' => 'java -jar server.jar', 'P_SERVER_LOCATION' => 'nyc', 'P_SERVER_UUID' => 'server-uuid', 'P_SERVER_ALLOCATION_LIMIT' => 4, 'CONFIGURED_CALLBACK' => 'server-uuid', 'CUSTOM_FLAG' => true]);
2026-10-05 18:10:18 -04:00
});
test('rejects non scalar environment values', function () {
config()->set('pterodactyl.environment_variables', []);
2026-10-12 00:21:31 -04:00
$service = new EnvironmentService();
2026-10-05 18:10:18 -04:00
$service->setEnvironmentKey('INVALID', static fn (Server $server): stdClass => new stdClass());
$this->expectException(InvalidArgumentException::class);
2026-10-12 00:21:31 -04:00
$service->forServer(server());
2026-10-05 18:10:18 -04:00
});
test('rejects invalid configured environment mappings', function (array $configured) {
config()->set('pterodactyl.environment_variables', $configured);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Each configured server environment variable must map a string key to an attribute path or callable.');
2026-10-12 00:21:31 -04:00
(new EnvironmentService())->forServer(server());
2026-10-05 18:10:18 -04:00
})->with([
'numeric key' => [[0 => 'uuid']],
'non-string source' => [['INVALID' => ['uuid']]],
]);
2026-10-12 00:21:31 -04:00
/**
* @param list<EggVariable> $variables
* @param list<ServerVariable> $overrides
*/
function server(array $variables = [], array $overrides = []): Server
2026-10-05 18:10:18 -04:00
{
$location = new Location();
$location->forceFill(['short' => 'nyc']);
2026-10-12 00:21:31 -04:00
$egg = new Egg();
$egg->setRelation('variables', new Collection($variables));
2026-10-05 18:10:18 -04:00
$server = new Server();
$server->forceFill(['startup' => 'java -jar server.jar', 'uuid' => 'server-uuid', 'allocation_limit' => 4, 'oom_disabled' => true]);
$server->setRelation('location', $location);
2026-10-12 00:21:31 -04:00
$server->setRelation('egg', $egg);
$server->setRelation('serverVariables', new Collection($overrides));
2026-10-05 18:10:18 -04:00
return $server;
}