Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,43 @@ function isBlueprintV2Declaration(
function resolveV2PHPVersion(
declaration: BlueprintV2Declaration
): AllPHPVersion {
if (typeof declaration.phpVersion !== 'string') {
return RecommendedPHPVersion;
const phpVersion = declaration.phpVersion;
if (typeof phpVersion === 'string') {
return resolveV2PHPVersionString(phpVersion);
}

if (
(AllPHPVersions as readonly string[]).includes(declaration.phpVersion)
) {
return declaration.phpVersion as AllPHPVersion;
const recommendedVersion = getV2PHPConstraintRecommendedVersion(phpVersion);
if (recommendedVersion) {
return resolveV2PHPVersionString(recommendedVersion);
}

return RecommendedPHPVersion;
}

function resolveV2PHPVersionString(phpVersion: string): AllPHPVersion {
if ((AllPHPVersions as readonly string[]).includes(phpVersion)) {
return phpVersion as AllPHPVersion;
}
throw new Error(
`Unsupported Blueprint v2 PHP version "${declaration.phpVersion}". ` +
`Unsupported Blueprint v2 PHP version "${phpVersion}". ` +
`Supported versions: ${AllPHPVersions.join(', ')}.`
);
}

function getV2PHPConstraintRecommendedVersion(
phpVersion: BlueprintV2Declaration['phpVersion']
): string | undefined {
if (!phpVersion || typeof phpVersion !== 'object') {
return undefined;
}
if (!('recommended' in phpVersion)) {
return undefined;
}
return typeof phpVersion.recommended === 'string'
? phpVersion.recommended
: undefined;
}

function resolveV2WordPressVersion(
declaration: BlueprintV2Declaration
): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('Blueprint v2 runtime configuration', () => {
});
});

it('uses the default PHP version for constraint objects', async () => {
it('resolves recommended PHP versions from constraint objects', async () => {
await expect(
resolveRuntimeConfiguration({
version: 2,
Expand All @@ -122,6 +122,20 @@ describe('Blueprint v2 runtime configuration', () => {
max: '8.4',
},
})
).resolves.toMatchObject({
phpVersion: '8.2',
});
});

it('uses the default PHP version for constraints without a recommended version', async () => {
await expect(
resolveRuntimeConfiguration({
version: 2,
phpVersion: {
min: '8.1',
max: '8.4',
},
})
).resolves.toMatchObject({
phpVersion: RecommendedPHPVersion,
});
Expand Down
Loading