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

222
bin/cfg
View file

@ -22,51 +22,50 @@ foreach ($autoloadFiles as $autoloadFile) {
} }
} }
// Pre-parse site options so they can be passed after positional args. function readJsonInputFile(string $path): array
{
if (!is_readable($path)) {
throw new \RuntimeException("Input file is not readable: $path");
}
$content = file_get_contents($path);
if ($content === false) {
throw new \RuntimeException("Can not read input file: $path");
}
try {
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $ex) {
throw new \RuntimeException(sprintf(
'Input JSON is invalid (%s): %s',
(string)$ex->getCode(),
$ex->getMessage()
));
}
if (!is_array($data)) {
throw new \RuntimeException('Input JSON must decode to an object/array');
}
return $data;
}
// Pre-parse siteDir so it can be passed after positional args.
// The input helper library does not handle this case reliably. // The input helper library does not handle this case reliably.
$preparsedSiteOptions = []; $preparsedSiteDir = null;
$rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null; $rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null;
if (is_array($rawArgv) && !empty($rawArgv)) { if (is_array($rawArgv) && !empty($rawArgv)) {
$filteredArgv = [$rawArgv[0]]; $filteredArgv = [$rawArgv[0]];
for ($idx = 1; $idx < count($rawArgv); $idx++) { for ($idx = 1; $idx < count($rawArgv); $idx++) {
$arg = $rawArgv[$idx]; $arg = $rawArgv[$idx];
if ($arg === '-s' || $arg === '--site') { if ($arg === '--siteDir') {
$preparsedSiteOptions[] = [ $preparsedSiteDir = $rawArgv[$idx + 1] ?? '';
'name' => 'site',
'value' => $rawArgv[$idx + 1] ?? '',
];
if (isset($rawArgv[$idx + 1])) $idx++; if (isset($rawArgv[$idx + 1])) $idx++;
continue; continue;
} }
if (str_starts_with($arg, '--site=')) { if (str_starts_with($arg, '--siteDir=')) {
$preparsedSiteOptions[] = [ $preparsedSiteDir = substr($arg, strlen('--siteDir='));
'name' => 'site',
'value' => substr($arg, strlen('--site=')),
];
continue;
}
if ($arg === '--directory-name' || $arg === '--directoryName') {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => $rawArgv[$idx + 1] ?? '',
];
if (isset($rawArgv[$idx + 1])) $idx++;
continue;
}
if (str_starts_with($arg, '--directory-name=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directory-name=')),
];
continue;
}
if (str_starts_with($arg, '--directoryName=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directoryName=')),
];
continue; continue;
} }
@ -92,12 +91,7 @@ $collection = (new Input\InputCollection())
->add( Input\InputTypeFactory::build('LongOption')->name('in')->short('i') // {{{ ->add( Input\InputTypeFactory::build('LongOption')->name('in')->short('i') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path to a json data file to to read') ->description('Path to a JSON data file to read (for write)')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('out')->short('o') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path to a json file to to write to')
) // }}} ) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('mode')->short('m') // {{{ ->add( Input\InputTypeFactory::build('LongOption')->name('mode')->short('m') // {{{
@ -115,14 +109,9 @@ $collection = (new Input\InputCollection())
->description('Path where the config/ directory of the package conf files is located, defaults to the working dir') ->description('Path where the config/ directory of the package conf files is located, defaults to the working dir')
) // }}} ) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('site')->short('s') // {{{ ->add( Input\InputTypeFactory::build('LongOption')->name('siteDir') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Site/instance directory below config/ (target: config/<site>/<prefix>.conf.php), e.g. owner_xyz') ->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('directoryName') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Alias for --site. Accepts owner_xyz or config/owner_xyz (also supports --directory-name)')
) // }}} ) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{ ->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{
@ -172,13 +161,17 @@ $collection = (new Input\InputCollection())
{ {
$setting = $context->find('setting'); $setting = $context->find('setting');
$action = $context->find('action'); $action = $context->find('action');
if ($setting === null || $setting === '') {
return ['key' => '', 'value' => null];
}
$setting = explode('=', $setting); $setting = explode('=', $setting);
$settings['key'] = $setting[0]; $settings['key'] = $setting[0];
if ($action === 'write') if ($action === 'write')
{ {
$value = $setting[1]; $value = $setting[1] ?? null;
if (! (isset($value) || $context->find('data'))) { if (! (isset($value) || $context->find('in'))) {
throw new \Exception('You need a value to write'); throw new \Exception('You need a value to write');
} }
$specialValues = [ 'true', 'false', 'null' ]; $specialValues = [ 'true', 'false', 'null' ];
@ -204,21 +197,29 @@ $collection = (new Input\InputCollection())
$usage = Cli\manpage( basename(__FILE__), $version, $usage = Cli\manpage( basename(__FILE__), $version,
'read write settings', 'read write settings',
$collection, Colour::FG_GREEN, Colour::FG_WHITE, $collection, Colour::FG_GREEN, Colour::FG_WHITE,
[ [
'Examples' => 'Examples' =>
'cfg write myCEESV \'auth:projectId="218523"\'' 'cfg write extension_name \'module:enabled=true\''
.PHP_EOL .PHP_EOL
.'# writes local config: config/myCEESV.conf.php' .'# writes local config: config/extension_name.conf.php'
.PHP_EOL .PHP_EOL
.'cfg write myCEESV \'auth:projectId="218523"\' --directory-name=owner_xyz' .'cfg write extension_name -i /tmp/extension.json --siteDir=owner_xyz'
.PHP_EOL .PHP_EOL
.'# writes instance config: config/owner_xyz/myCEESV.conf.php' .'# writes multiple settings from JSON to config/owner_xyz/extension_name.conf.php'
.PHP_EOL .PHP_EOL
.'cfg show myCEESV auth:projectId --site=owner_xyz' .'cfg write extension_name \'feature_example:enabled=true\' --siteDir=owner_xyz'
.PHP_EOL .PHP_EOL
.'# reads merged config including config/owner_xyz/myCEESV.conf.php' .'# writes instance config: config/owner_xyz/extension_name.conf.php'
.PHP_EOL .PHP_EOL
] .'cfg write extension_name \'feature_example={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz'
.PHP_EOL
.'# writes multiple keys from one JSON string'
.PHP_EOL
.'cfg show extension_name module:enabled --siteDir=owner_xyz'
.PHP_EOL
.'# reads merged config including config/owner_xyz/extension_name.conf.php'
.PHP_EOL
]
).PHP_EOL; ).PHP_EOL;
// Get the supplied input. Passing the collection will make the handler bind values // Get the supplied input. Passing the collection will make the handler bind values
@ -275,56 +276,38 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/');
$site = null; $site = null;
$siteFlag = 0x01; $siteFlag = 0x01;
// Only one site selector may be provided to avoid ambiguous targets. $siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir');
$providedSiteOptions = []; if ($siteInput !== null && $siteInput !== false) {
foreach ($preparsedSiteOptions as $option) { $siteInput = trim((string)$siteInput);
$providedSiteOptions[$option['name']][] = $option['value'] ?? '';
} // Accept both "owner_xyz" and "config/owner_xyz" and normalize to site key.
foreach (['site', 'directoryName'] as $optionName) { if (str_starts_with($siteInput, $appPath.'config/')) {
$parsedValue = $argv->find($optionName); $siteInput = substr($siteInput, strlen($appPath.'config/'));
if ($parsedValue !== null && $parsedValue !== '' && $parsedValue !== false) { } elseif (str_starts_with($siteInput, 'config/')) {
$providedSiteOptions[$optionName][] = $parsedValue; $siteInput = substr($siteInput, strlen('config/'));
} }
}
if (count($providedSiteOptions) > 1) { $siteInput = trim($siteInput, '/');
fwrite(STDERR, 'Please use only one of --site or --directoryName (alias: --directory-name).'.PHP_EOL); if ($siteInput === '') {
exit(1); fwrite(STDERR, 'Option --siteDir is empty.'.PHP_EOL);
} exit(1);
if (!empty($providedSiteOptions)) { }
$optionName = array_key_first($providedSiteOptions); // Block directory traversal and hidden-dot segments.
$siteInput = trim((string)end($providedSiteOptions[$optionName])); if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) {
fwrite(STDERR, "Invalid directory in --siteDir: '$siteInput'.".PHP_EOL);
exit(1);
}
// Allow only predictable path segments for instance directories.
foreach (explode('/', $siteInput) as $part) {
if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) {
fwrite(STDERR, "Invalid directory name segment '$part' in --siteDir.".PHP_EOL);
exit(1);
}
}
if ($optionName === 'directoryName') { $site = $siteInput;
// Accept both "owner_xyz" and "config/owner_xyz" and normalize to site key. // Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
if (str_starts_with($siteInput, $appPath.'config/')) { $cfg->site($site);
$siteInput = substr($siteInput, strlen($appPath.'config/'));
} elseif (str_starts_with($siteInput, 'config/')) {
$siteInput = substr($siteInput, strlen('config/'));
}
}
$siteInput = trim($siteInput, '/');
if ($siteInput === '') {
fwrite(STDERR, "Option --$optionName is empty.".PHP_EOL);
exit(1);
}
// Block directory traversal and hidden-dot segments.
if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) {
fwrite(STDERR, "Invalid directory in --$optionName: '$siteInput'.".PHP_EOL);
exit(1);
}
// Allow only predictable path segments for instance directories.
foreach (explode('/', $siteInput) as $part) {
if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) {
fwrite(STDERR, "Invalid directory name segment '$part' in --$optionName.".PHP_EOL);
exit(1);
}
}
$site = $siteInput;
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
$cfg->site($site);
} }
try { try {
@ -368,9 +351,30 @@ case 'show':
break; break;
case 'write': case 'write':
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; $inputFile = $argv->find('in');
// Write needs exactly one payload source:
// either SETTING (key=value) or --in (JSON file).
// This avoids ambiguous input precedence and empty writes.
if ($inputFile && $settings['key'] !== '') {
fwrite(STDERR, 'Please use either SETTING or --in, not both.'.PHP_EOL);
exit(1);
}
if (!$inputFile && $settings['key'] === '') {
fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL);
exit(1);
}
$setting2write = $settings['value']; $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
if ($inputFile) {
try {
$setting2write = readJsonInputFile($inputFile);
} catch (\Throwable $e) {
fwrite(STDERR, $e->getMessage().PHP_EOL);
exit(1);
}
} else {
$setting2write = $settings['value'];
}
while ( ! empty($path)) while ( ! empty($path))
{ {

View file

@ -15,8 +15,8 @@ class CfgTest extends TestCase
mkdir($this->tmpDir.'/config', 0775, true); mkdir($this->tmpDir.'/config', 0775, true);
file_put_contents( file_put_contents(
$this->tmpDir.'/config/myCEESV.default.conf.php', $this->tmpDir.'/config/Extension.default.conf.php',
"<?php return [\n\t'mode' => 'prod',\n\t'auth' => [\n\t\t'projectId' => '',\n\t\t'clientId' => '',\n\t],\n];\n" "<?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 public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void
{ {
$result = $this->runCfg([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
]); ]);
$this->assertSame(0, $result['code'], $result['output']); $this->assertSame(0, $result['code'], $result['output']);
$localFile = $this->tmpDir.'/config/myCEESV.conf.php'; $localFile = $this->tmpDir.'/config/Extension.conf.php';
$this->assertFileExists($localFile); $this->assertFileExists($localFile);
$cfg = require $localFile; $cfg = require $localFile;
$this->assertSame('218523', $cfg['auth']['projectId']); $this->assertSame('X100', $cfg['module']['code']);
} }
public function testWriteWithoutModeFallsBackToDefaultMode(): void public function testWriteWithoutModeFallsBackToDefaultMode(): void
{ {
file_put_contents( file_put_contents(
$this->tmpDir.'/config/myCEESV.default.conf.php', $this->tmpDir.'/config/Extension.default.conf.php',
"<?php return [\n\t'auth' => [\n\t\t'projectId' => '',\n\t],\n];\n" "<?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([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', '--siteDir=owner_xyz',
'-i',
$inFile,
]); ]);
$this->assertSame(0, $result['code'], $result['output']); $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([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:flags:enabled=true',
'--directory-name', '--siteDir=owner_xyz',
'owner_xyz',
]); ]);
$this->assertSame(0, $result['code'], $result['output']); $this->assertSame(0, $result['code'], $result['output']);
$cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php';
$siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; $this->assertTrue($cfg['module']['flags']['enabled']);
$this->assertFileExists($siteFile);
$this->assertFileDoesNotExist($this->tmpDir.'/config/myCEESV.conf.php');
$cfg = require $siteFile;
$this->assertSame('218523', $cfg['auth']['projectId']);
} }
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([ $firstWrite = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name', '--siteDir',
'owner_xyz', 'owner_xyz',
]); ]);
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
$secondWrite = $this->runCfg([ $secondWrite = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:clientId="service-9999-qual@myceesv.ch"', 'module:label="demo-module"',
'--directory-name', '--siteDir',
'owner_xyz', 'owner_xyz',
]); ]);
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']); $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; $cfg = require $siteFile;
$this->assertSame('218523', $cfg['auth']['projectId']); $this->assertSame('X100', $cfg['module']['code']);
$this->assertSame('service-9999-qual@myceesv.ch', $cfg['auth']['clientId']); $this->assertSame('demo-module', $cfg['module']['label']);
} }
public function testWriteWithDirectoryNameUsingConfigPrefixIsNormalized(): void public function testWriteWithSiteDirUsingConfigPrefixIsNormalized(): void
{ {
$result = $this->runCfg([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name=config/owner_xyz', '--siteDir=config/owner_xyz',
]); ]);
$this->assertSame(0, $result['code'], $result['output']); $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');
$this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/myCEESV.conf.php'); $this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/Extension.conf.php');
} }
public function testWriteWithSiteOptionWritesToSiteConfig(): void public function testWriteWithSiteDirWritesToSiteConfig(): void
{ {
$result = $this->runCfg([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--site=owner_xyz', '--siteDir=owner_xyz',
]); ]);
$this->assertSame(0, $result['code'], $result['output']); $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 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');
} }
public function testWriteToExistingSiteConfigCreatesBackup(): void public function testWriteToExistingSiteConfigCreatesBackup(): void
{ {
$firstWrite = $this->runCfg([ $firstWrite = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="first"', 'module:code="first"',
'--directory-name=owner_xyz', '--siteDir=owner_xyz',
]); ]);
$this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']);
$secondWrite = $this->runCfg([ $secondWrite = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="second"', 'module:code="second"',
'--directory-name=owner_xyz', '--siteDir=owner_xyz',
]); ]);
$this->assertSame(0, $secondWrite['code'], $secondWrite['output']); $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'; $backupFile = $siteFile.'.bak';
$this->assertFileExists($backupFile); $this->assertFileExists($backupFile);
$current = require $siteFile; $current = require $siteFile;
$backup = require $backupFile; $backup = require $backupFile;
$this->assertSame('second', $current['auth']['projectId']); $this->assertSame('second', $current['module']['code']);
$this->assertSame('first', $backup['auth']['projectId']); $this->assertSame('first', $backup['module']['code']);
} }
public function testShowWithSiteReturnsSiteSpecificValue(): void public function testShowWithSiteReturnsSiteSpecificValue(): void
{ {
$this->runCfg([ $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name=owner_xyz', '--siteDir=owner_xyz',
]); ]);
$show = $this->runCfg([ $show = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'show', 'show',
'myCEESV', 'Extension',
'auth:projectId', 'module:code',
'--site=owner_xyz', '--siteDir=owner_xyz',
]); ]);
$this->assertSame(0, $show['code'], $show['output']); $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([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name=../owner_xyz', '--siteDir=../owner_xyz',
]); ]);
$this->assertSame(1, $result['code']); $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([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name=owner xyz', '--siteDir=owner xyz',
]); ]);
$this->assertSame(1, $result['code']); $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([ $result = $this->runCfg([
'-a', '-a',
$this->tmpDir, $this->tmpDir,
'write', 'write',
'myCEESV', 'Extension',
'auth:projectId="218523"', 'module:code="X100"',
'--directory-name=', '--siteDir=',
]); ]);
$this->assertSame(1, $result['code']); $this->assertSame(1, $result['code']);
$this->assertStringContainsString('Option --directoryName is empty.', $result['output']); $this->assertStringContainsString('Option --siteDir 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']);
} }
private function runCfg(array $args): array private function runCfg(array $args): array

View file

@ -224,17 +224,17 @@ class SettingsTest extends TestCase
public function testBuildFileNameWithPrefixAndSite(): void public function testBuildFileNameWithPrefixAndSite(): void
{ {
$cfg = new Settings(); $cfg = new Settings();
$cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz'); $cfg->appPath('./')->prefix('Extension')->site('owner_xyz');
$this->assertEquals('./config/owner_xyz/myCEESV.conf.php', $cfg->buildFileName(0x01)); $this->assertEquals('./config/owner_xyz/Extension.conf.php', $cfg->buildFileName(0x01));
} }
public function testBuildFileNameWithPrefixAndNestedSite(): void public function testBuildFileNameWithPrefixAndNestedSite(): void
{ {
$cfg = new Settings(); $cfg = new Settings();
$cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz/sub_a'); $cfg->appPath('./')->prefix('Extension')->site('owner_xyz/sub_a');
$this->assertEquals('./config/owner_xyz/sub_a/myCEESV.conf.php', $cfg->buildFileName(0x01)); $this->assertEquals('./config/owner_xyz/sub_a/Extension.conf.php', $cfg->buildFileName(0x01));
} }
public function fileNameData() public function fileNameData()