You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudPublicNetworkTransitio...

200 lines
7.0 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPublicNetworkTransitionCommand;
use App\Services\Cloud\CloudPublicNetworkTransitionContext;
use App\Services\Cloud\CloudPublicNetworkTransitionEvidence;
use App\Services\Cloud\CloudPublicNetworkTransitionSettlement;
use App\Services\Cloud\CloudPublicNetworkTransitionWorkflow;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$publicMethods = static function (string $class): array {
$reflection = new ReflectionClass($class);
$methods = array_values(array_map(
static fn (ReflectionMethod $method): string => $method->getName(),
array_filter(
$reflection->getMethods(ReflectionMethod::IS_PUBLIC),
static fn (ReflectionMethod $method): bool =>
$method->getDeclaringClass()->getName() === $class
)
));
sort($methods, SORT_STRING);
return $methods;
};
$root = dirname(__DIR__);
$productionCallers = static function (string $needle) use ($root): array {
$callers = [];
foreach ([$root . '/app', $root . '/config', $root . '/public']
as $directory) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$directory,
FilesystemIterator::SKIP_DOTS
)
);
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}
$contents = file_get_contents($file->getPathname());
if (is_string($contents) && str_contains($contents, $needle)) {
$callers[] = realpath($file->getPathname());
}
}
}
$callers = array_values(array_unique($callers));
sort($callers);
return $callers;
};
$paths = static function (array $relative) use ($root): array {
$paths = array_map(
static fn (string $path): string|false => realpath($root . '/' . $path),
$relative
);
if (in_array(false, $paths, true)) {
throw new RuntimeException('Expected transition caller is missing.');
}
sort($paths);
return $paths;
};
foreach ([
CloudPublicNetworkTransitionWorkflow::class => [
'begin',
'beginPoolReplacementBatch',
'markDispatched',
'markJobUncertain',
'recordEvidence',
'finishJobLocked',
'publishPreparedLocked',
'retryPendingFinalization',
],
CloudPublicNetworkTransitionContext::class => [
'lockHostBoundary',
'assertAssociation',
'assertInvoiceAssociation',
'lockLedger',
],
CloudPublicNetworkTransitionEvidence::class => [
'markDispatched',
'markJobUncertain',
'record',
'recoveryPayload',
],
CloudPublicNetworkTransitionSettlement::class => [
'finishJobLocked',
'publishPreparedLocked',
'retryPendingFinalization',
'terminalMatches',
],
] as $class => $expected) {
$reflection = new ReflectionClass($class);
sort($expected, SORT_STRING);
$assert(
$reflection->isFinal() && $publicMethods($class) === $expected,
'Public transition module API drifted: ' . $class
);
}
$lockLedger = new ReflectionMethod(
CloudPublicNetworkTransitionContext::class,
'lockLedger'
);
$assert(
array_map(
static fn (ReflectionParameter $parameter): string =>
$parameter->getName(),
$lockLedger->getParameters()
) === ['instance', 'transition', 'source', 'target'],
'Transition ledger lock API exposes unrelated context parameters.'
);
$command = new ReflectionClass(CloudPublicNetworkTransitionCommand::class);
foreach (['userPaidAutomatic', 'adminExact', 'poolReplacement'] as $factory) {
$assert(
$command->hasMethod($factory)
&& $command->getMethod($factory)->isStatic(),
'Transition command factory is missing: ' . $factory
);
}
$assert(
$productionCallers('CloudPublicNetworkTransitionSettlement') === $paths([
'app/Services/Cloud/CloudPublicNetworkTransitionSettlement.php',
'app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php',
]),
'Production code bypasses the transition workflow settlement facade.'
);
$assert(
$productionCallers('CloudPublicNetworkTransitionEvidence') === $paths([
'app/Services/Cloud/CloudPublicNetworkTransitionEvidence.php',
'app/Services/Cloud/CloudPublicNetworkTransitionSettlement.php',
'app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php',
]),
'Production code bypasses the transition workflow evidence facade.'
);
$assert(
$productionCallers('CloudPublicNetworkTransitionContext') === $paths([
'app/Services/Cloud/CloudPublicNetworkTransitionContext.php',
'app/Services/Cloud/CloudPublicNetworkTransitionEvidence.php',
'app/Services/Cloud/CloudPublicNetworkTransitionSettlement.php',
'app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php',
]),
'Production code bypasses the transition context boundary.'
);
$assert(
$productionCallers('CloudPublicNetworkTransitionWorkflow') === $paths([
'app/Controllers/CloudAdminInstanceController.php',
'app/Controllers/CloudApiController.php',
'app/Services/Cloud/CloudAutomaticOperationRecoveryService.php',
'app/Services/Cloud/CloudOperationRecoveryService.php',
'app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php',
'app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php',
'app/Services/Cloud/CloudWorkerService.php',
]),
'Production transition workflow callers escaped the approved facades.'
);
$assert(
$productionCallers('CloudPublicNetworkTransitionWorkflow::begin(')
=== $paths([
'app/Controllers/CloudAdminInstanceController.php',
'app/Controllers/CloudApiController.php',
]),
'Transition begin callers escaped the tenant and administrator seams.'
);
$tenantController = file_get_contents(
$root . '/app/Controllers/CloudApiController.php'
);
$adminController = file_get_contents(
$root . '/app/Controllers/CloudAdminInstanceController.php'
);
$assert(
is_string($tenantController) && is_string($adminController)
&& str_contains(
$tenantController,
'CloudPublicNetworkTransitionCommand::'
)
&& str_contains($tenantController, 'userPaidAutomatic(')
&& str_contains(
$tenantController,
'CloudPublicNetworkTransitionWorkflow::begin($command)'
)
&& str_contains(
$adminController,
'CloudPublicNetworkTransitionCommand::adminExact('
)
&& str_contains(
$adminController,
'CloudPublicNetworkTransitionWorkflow::begin($command)'
),
'A controller bypasses the typed command plus transition facade.'
);
echo "Public-network transition workflow contract tests passed.\n";