From 56123603a443c8df57c9de27809cf76aba165824 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 20 Jun 2026 00:59:49 +0200 Subject: [PATCH] Expand WorkerCommandLineFactory tests for the worker command string Cover option mirroring (bool flags, null skip, memory-limit assign form, escaped string values), excluded framework globals, multiple paths, and the project config file position right after the worker command name. --- tests/CommandLine/Source/TestOption.php | 10 +++ .../WorkerCommandLineFactoryTest.php | 78 +++++++++++++++++-- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/tests/CommandLine/Source/TestOption.php b/tests/CommandLine/Source/TestOption.php index 767c00c..0ffcb09 100644 --- a/tests/CommandLine/Source/TestOption.php +++ b/tests/CommandLine/Source/TestOption.php @@ -15,4 +15,14 @@ final class TestOption * @var string */ public const OUTPUT_FORMAT = 'output-format'; + + /** + * @var string + */ + public const FIX = 'fix'; + + /** + * @var string + */ + public const MEMORY_LIMIT = 'memory-limit'; } diff --git a/tests/CommandLine/WorkerCommandLineFactoryTest.php b/tests/CommandLine/WorkerCommandLineFactoryTest.php index bb05e45..e029c3b 100644 --- a/tests/CommandLine/WorkerCommandLineFactoryTest.php +++ b/tests/CommandLine/WorkerCommandLineFactoryTest.php @@ -43,18 +43,82 @@ public function test(array $optionValues, array $paths, string $expectedCommand) public static function provideData(): Iterator { - $expectedCommandLinesString = self::createExpectedCommandLinesString(); + yield 'no options, single path' => [[], ['src'], self::wrap('', "'src'")]; - yield [[], ['src'], $expectedCommandLinesString]; + yield 'output-format is excluded' => [ + [TestOption::OUTPUT_FORMAT => 'console'], + ['src'], + self::wrap('', "'src'"), + ]; - // output-format is excluded, so it must not change the result - yield [[TestOption::OUTPUT_FORMAT => 'console'], ['src'], $expectedCommandLinesString]; + yield 'true bool option becomes a flag' => [ + [TestOption::FIX => true], + ['src'], + self::wrap('--fix', "'src'"), + ]; + + yield 'false bool option is omitted' => [ + [TestOption::FIX => false], + ['src'], + self::wrap('', "'src'"), + ]; + + yield 'null option is omitted' => [ + [TestOption::MEMORY_LIMIT => null], + ['src'], + self::wrap('', "'src'"), + ]; + + yield 'memory-limit uses the assign form' => [ + [TestOption::MEMORY_LIMIT => '-1'], + ['src'], + self::wrap('--memory-limit=-1', "'src'"), + ]; + + yield 'string option keeps the escaped value' => [ + ['some-option' => 'value'], + ['src'], + self::wrap("--some-option 'value'", "'src'"), + ]; + + yield 'multiple paths are all escaped' => [[], ['src', 'tests'], self::wrap('', "'src' 'tests'")]; + + yield 'framework global options are excluded' => [ + ['no-interaction' => true, 'verbose' => true, 'ansi' => true, 'help' => true], + ['src'], + self::wrap('', "'src'"), + ]; } - private static function createExpectedCommandLinesString(): string + public function testProjectConfigFileIsPassedRightAfterWorkerName(): void { - $commandLineString = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "'"; + $workerCommandLine = $this->workerCommandLineFactory->create( + self::DUMMY_MAIN_SCRIPT, + 'worker', + 'ecs.php', + [], + ['src'], + 'identifier', + 2000 + ); + + $expectedCommand = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT + . "' worker --config 'ecs.php' --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi"; + + $this->assertSame($expectedCommand, $workerCommandLine); + } + + /** + * Assemble the expected command line: options sit before --port, paths after --identifier. + */ + private static function wrap(string $options, string $paths): string + { + $command = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "' worker"; + + if ($options !== '') { + $command .= ' ' . $options; + } - return $commandLineString . " worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi"; + return $command . " --port 2000 --identifier 'identifier' " . $paths . " --output-format 'json' --no-ansi"; } }