Task: #53687 Replace --site/--directory-name with --siteDir and add JSON batch input

- Changed site targeting from --site, --directory-name, --directoryName to --siteDir
- Added write -i <json> for multi-setting input
- Added support for inline JSON string values in write
- Updated help/examples to generic placeholders
- Extended tests for new arguments and validations (all passing)
This commit is contained in:
Alejandro Sosa 2026-03-26 11:28:58 +01:00
commit 227de9ac07
3 changed files with 324 additions and 268 deletions

View file

@ -15,8 +15,8 @@ class CfgTest extends TestCase
mkdir($this->tmpDir.'/config', 0775, true);
file_put_contents(
$this->tmpDir.'/config/myCEESV.default.conf.php',
"<?php return [\n\t'mode' => 'prod',\n\t'auth' => [\n\t\t'projectId' => '',\n\t\t'clientId' => '',\n\t],\n];\n"
$this->tmpDir.'/config/Extension.default.conf.php',
"<?php return [\n\t'mode' => 'prod',\n\t'module' => [\n\t\t'code' => '',\n\t\t'label' => '',\n\t\t'flags' => [\n\t\t\t'enabled' => false,\n\t\t],\n\t],\n\t'feature' => [\n\t\t'endpoint' => '',\n\t],\n];\n"
);
}
@ -28,257 +28,309 @@ class CfgTest extends TestCase
public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
]);
$this->assertSame(0, $result['code'], $result['output']);
$localFile = $this->tmpDir.'/config/myCEESV.conf.php';
$localFile = $this->tmpDir.'/config/Extension.conf.php';
$this->assertFileExists($localFile);
$cfg = require $localFile;
$this->assertSame('218523', $cfg['auth']['projectId']);
$this->assertSame('X100', $cfg['module']['code']);
}
public function testWriteWithoutModeFallsBackToDefaultMode(): void
{
file_put_contents(
$this->tmpDir.'/config/myCEESV.default.conf.php',
"<?php return [\n\t'auth' => [\n\t\t'projectId' => '',\n\t],\n];\n"
$this->tmpDir.'/config/Extension.default.conf.php',
"<?php return [\n\t'module' => [\n\t\t'code' => '',\n\t],\n];\n"
);
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
]);
$this->assertSame(0, $result['code'], $result['output']);
$this->assertFileExists($this->tmpDir.'/config/Extension.conf.php');
}
public function testWriteWithInputFileWritesMultipleSettings(): void
{
$inFile = $this->tmpDir.'/extension-in.json';
file_put_contents(
$inFile,
json_encode([
'module' => [
'code' => 'X100',
'label' => 'demo-module',
],
'feature' => [
'endpoint' => 'https://example.invalid/v1/resource',
],
], JSON_PRETTY_PRINT)
);
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'Extension',
'--siteDir=owner_xyz',
'-i',
$inFile,
]);
$this->assertSame(0, $result['code'], $result['output']);
$this->assertFileExists($this->tmpDir.'/config/myCEESV.conf.php');
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$this->assertSame('X100', $cfg['module']['code']);
$this->assertSame('demo-module', $cfg['module']['label']);
$this->assertSame('https://example.invalid/v1/resource', $cfg['feature']['endpoint']);
}
public function testWriteWithDirectoryNameCreatesAndWritesSiteConfig(): void
public function testWriteAcceptsJsonStringValueInSetting(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'Extension',
'module={"code":"X100","label":"demo-module"}',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$this->assertSame('X100', $cfg['module']['code']);
$this->assertSame('demo-module', $cfg['module']['label']);
}
public function testWriteSupportsNestedPathWithColonNotation(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name',
'owner_xyz',
'Extension',
'module:flags:enabled=true',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php';
$this->assertFileExists($siteFile);
$this->assertFileDoesNotExist($this->tmpDir.'/config/myCEESV.conf.php');
$cfg = require $siteFile;
$this->assertSame('218523', $cfg['auth']['projectId']);
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$this->assertTrue($cfg['module']['flags']['enabled']);
}
public function testWriteWithDirectoryNameMergesIntoExistingSiteConfig(): void
public function testWriteWithInputFileAndSettingReturnsError(): void
{
$inFile = $this->tmpDir.'/extension-in.json';
file_put_contents($inFile, json_encode(['module' => ['code' => 'X100']]));
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="x"',
'-i',
$inFile,
]);
$this->assertSame(1, $result['code']);
$this->assertStringContainsString('Please use either SETTING or --in, not both.', $result['output']);
}
public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir',
'owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$this->assertFileExists($siteFile);
$this->assertFileDoesNotExist($this->tmpDir.'/config/Extension.conf.php');
$cfg = require $siteFile;
$this->assertSame('X100', $cfg['module']['code']);
}
public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void
{
$firstWrite = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name',
'owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir',
'owner_xyz',
]);
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
$secondWrite = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:clientId="service-9999-qual@myceesv.ch"',
'--directory-name',
'owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:label="demo-module"',
'--siteDir',
'owner_xyz',
]);
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']);
$siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php';
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$cfg = require $siteFile;
$this->assertSame('218523', $cfg['auth']['projectId']);
$this->assertSame('service-9999-qual@myceesv.ch', $cfg['auth']['clientId']);
$this->assertSame('X100', $cfg['module']['code']);
$this->assertSame('demo-module', $cfg['module']['label']);
}
public function testWriteWithDirectoryNameUsingConfigPrefixIsNormalized(): void
public function testWriteWithSiteDirUsingConfigPrefixIsNormalized(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name=config/owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=config/owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php');
$this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/myCEESV.conf.php');
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php');
$this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/Extension.conf.php');
}
public function testWriteWithSiteOptionWritesToSiteConfig(): void
public function testWriteWithSiteDirWritesToSiteConfig(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--site=owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php');
}
public function testWriteWithDirectoryNameCamelCaseOptionWritesToSiteConfig(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directoryName=owner_xyz',
]);
$this->assertSame(0, $result['code'], $result['output']);
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php');
$this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php');
}
public function testWriteToExistingSiteConfigCreatesBackup(): void
{
$firstWrite = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="first"',
'--directory-name=owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="first"',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
$secondWrite = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="second"',
'--directory-name=owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="second"',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']);
$siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php';
$siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$backupFile = $siteFile.'.bak';
$this->assertFileExists($backupFile);
$current = require $siteFile;
$backup = require $backupFile;
$this->assertSame('second', $current['auth']['projectId']);
$this->assertSame('first', $backup['auth']['projectId']);
$this->assertSame('second', $current['module']['code']);
$this->assertSame('first', $backup['module']['code']);
}
public function testShowWithSiteReturnsSiteSpecificValue(): void
{
$this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name=owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=owner_xyz',
]);
$show = $this->runCfg([
'-a',
$this->tmpDir,
'show',
'myCEESV',
'auth:projectId',
'--site=owner_xyz',
]);
'-a',
$this->tmpDir,
'show',
'Extension',
'module:code',
'--siteDir=owner_xyz',
]);
$this->assertSame(0, $show['code'], $show['output']);
$this->assertStringContainsString('218523', $show['output']);
$this->assertStringContainsString('X100', $show['output']);
}
public function testDirectoryNameRejectsTraversal(): void
public function testSiteDirRejectsTraversal(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name=../owner_xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=../owner_xyz',
]);
$this->assertSame(1, $result['code']);
$this->assertStringContainsString('Invalid directory in --directoryName', $result['output']);
$this->assertStringContainsString('Invalid directory in --siteDir', $result['output']);
}
public function testDirectoryNameRejectsInvalidSegment(): void
public function testSiteDirRejectsInvalidSegment(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name=owner xyz',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=owner xyz',
]);
$this->assertSame(1, $result['code']);
$this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --directoryName.", $result['output']);
$this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --siteDir.", $result['output']);
}
public function testDirectoryNameRejectsEmptyValue(): void
public function testSiteDirRejectsEmptyValue(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--directory-name=',
]);
'-a',
$this->tmpDir,
'write',
'Extension',
'module:code="X100"',
'--siteDir=',
]);
$this->assertSame(1, $result['code']);
$this->assertStringContainsString('Option --directoryName is empty.', $result['output']);
}
public function testUsingSiteAndDirectoryNameTogetherReturnsError(): void
{
$result = $this->runCfg([
'-a',
$this->tmpDir,
'write',
'myCEESV',
'auth:projectId="218523"',
'--site',
'owner_xyz',
'--directory-name',
'owner_xyz',
]);
$this->assertSame(1, $result['code']);
$this->assertStringContainsString('Please use only one of --site or --directoryName (alias: --directory-name).', $result['output']);
$this->assertStringContainsString('Option --siteDir is empty.', $result['output']);
}
private function runCfg(array $args): array