Files
compras/tests/Unit/StandaloneRuntimeTest.php
T
nala 30bfb1a773
tests / ci (push) Canceled after 0s
commit compras3 2911
2026-11-29 16:03:11 +02:00

70 lines
4.0 KiB
PHP

<?php
use App\Services\StandaloneArtifact;
use App\Services\StandaloneRuntime;
use Illuminate\Database\Connection;
test('disabled standalone composition does not inspect the database or package', function () {
$db = Mockery::mock(Connection::class);
$runtime = new StandaloneRuntime([], new StandaloneArtifact);
expect($runtime->enabled())->toBeFalse()
->and($runtime->requested('capital-register'))->toBeFalse()
->and($runtime->feature('capital-register', $db))->toBeFalse();
});
test('standalone feature opt in requires a boolean rather than a truthy setting', function (mixed $value) {
$db = Mockery::mock(Connection::class);
$runtime = new StandaloneRuntime(['enabled' => $value, 'features' => ['capital-register' => true]], new StandaloneArtifact);
expect($runtime->feature('capital-register', $db))->toBeFalse();
})->with([false, 1, 'true', 'false', null]);
test('standalone requested features remain explicit before readiness validation', function () {
$runtime = new StandaloneRuntime(['enabled' => true, 'features' => ['requisition-accounts' => true]], new StandaloneArtifact);
expect($runtime->requested('requisition-accounts'))->toBeTrue()
->and($runtime->requested('private-audit'))->toBeFalse();
expect(fn () => $runtime->artifact())->toThrow(LogicException::class, 'COMPRAS_RUNTIME_CONFIGURATION');
});
test('standalone database binding accepts an ordinary own installation without QA names', function () {
$db = Mockery::mock(Connection::class);
$db->shouldReceive('getDriverName')->once()->andReturn('pgsql');
$db->shouldReceive('selectOne')->once()->andReturn((object) [
'database_name' => 'purchasing_company', 'role_name' => 'purchasing_app', 'database_oid' => '45000',
'server_address' => '192.0.2.10', 'server_port' => 5432, 'elevated' => 0,
]);
expect(StandaloneRuntime::databaseBinding($db))->toBe(hash('sha256', json_encode([
'product' => 'compras', 'database' => 'purchasing_company', 'oid' => '45000', 'role' => 'purchasing_app',
'address' => '192.0.2.10', 'port' => 5432,
], JSON_THROW_ON_ERROR)));
});
test('standalone binding rejects elevated or unavailable database identities', function (string $failure) {
$db = Mockery::mock(Connection::class);
$db->shouldReceive('getDriverName')->once()->andReturn('pgsql');
$identity = (object) ['database_name' => 'own', 'role_name' => 'app', 'database_oid' => '1',
'server_address' => '127.0.0.1', 'server_port' => 5432, 'elevated' => 0];
if ($failure === 'elevated') {
$identity->elevated = 1;
} elseif ($failure === 'address') {
$identity->server_address = null;
} elseif ($failure === 'port') {
$identity->server_port = null;
}
$db->shouldReceive('selectOne')->once()->andReturn($failure === 'missing' ? null : $identity);
expect(fn () => StandaloneRuntime::databaseBinding($db))->toThrow(LogicException::class, 'COMPRAS_RUNTIME_DATABASE_IDENTITY');
})->with(['elevated', 'address', 'port', 'missing']);
test('standalone binding rejects other drivers without querying them', function () {
$db = Mockery::mock(Connection::class);
$db->shouldReceive('getDriverName')->once()->andReturn('sqlite');
expect(fn () => StandaloneRuntime::databaseBinding($db))->toThrow(LogicException::class, 'COMPRAS_RUNTIME_DATABASE_DRIVER');
});
test('standalone data classification remains explicit and defaults to synthetic', function () {
expect((new StandaloneRuntime([], new StandaloneArtifact))->syntheticOnly())->toBeTrue()
->and((new StandaloneRuntime(['enabled' => true], new StandaloneArtifact))->syntheticOnly())->toBeTrue()
->and((new StandaloneRuntime(['enabled' => true, 'synthetic_only' => false], new StandaloneArtifact))->syntheticOnly())->toBeFalse();
expect(fn () => (new StandaloneRuntime(['enabled' => true, 'synthetic_only' => 'false'], new StandaloneArtifact))->syntheticOnly())
->toThrow(LogicException::class, 'COMPRAS_RUNTIME_DATA_CLASSIFICATION');
});