Skip to content

Commit d5a6081

Browse files
committed
Throw InvalidArgumentException when constraint config is invalid
1 parent 2a2a1ab commit d5a6081

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/PackageJsonSynchronizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ private function resolveImportMapPackages($phpPackage): array
154154
$constraint = $constraintConfig;
155155
$package = $importMapName;
156156
$entrypoint = false;
157-
} else {
157+
} elseif (\is_array($constraintConfig)) {
158158
// Matches array constraint, like {"version":"^3.0"} or {"version":"path:%PACKAGE%/script.js","entrypoint":true}
159159
// Note that non-path assets can't be entrypoint
160160
$constraint = $constraintConfig['version'] ?? '';
161161
$package = $constraintConfig['package'] ?? $importMapName;
162162
$entrypoint = $constraintConfig['entrypoint'] ?? false;
163+
} else {
164+
throw new \InvalidArgumentException(sprintf('Invalid constraint config for key "%s": "%s" given, array or string expected.', $importMapName, var_export($constraintConfig, true)));
163165
}
164166

165167
// When "$constraintConfig" matches one of the following cases:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"symfony": {
3+
"importmap": {
4+
"@symfony/test": true
5+
}
6+
}
7+
}

tests/PackageJsonSynchronizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,20 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
455455
],
456456
]);
457457
}
458+
459+
public function testExceptionWhenInvalidImportMapConstraint()
460+
{
461+
file_put_contents($this->tempDir.'/importmap.php', '<?php return [];');
462+
463+
$this->expectException(\InvalidArgumentException::class);
464+
$this->expectExceptionMessage('Invalid constraint config for key "@symfony/test": "true" given, array or string expected.');
465+
466+
$this->synchronizer->synchronize([
467+
[
468+
'name' => 'symfony/importmap-invalid-constraint-package',
469+
'keywords' => ['symfony-ux'],
470+
],
471+
]);
472+
}
473+
458474
}

0 commit comments

Comments
 (0)