Skip to content

Commit da54a6a

Browse files
authored
Expand WorkerCommandLineFactory tests for the worker command string (#10)
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.
1 parent 5051c93 commit da54a6a

2 files changed

Lines changed: 81 additions & 7 deletions

File tree

tests/CommandLine/Source/TestOption.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ final class TestOption
1515
* @var string
1616
*/
1717
public const OUTPUT_FORMAT = 'output-format';
18+
19+
/**
20+
* @var string
21+
*/
22+
public const FIX = 'fix';
23+
24+
/**
25+
* @var string
26+
*/
27+
public const MEMORY_LIMIT = 'memory-limit';
1828
}

tests/CommandLine/WorkerCommandLineFactoryTest.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,82 @@ public function test(array $optionValues, array $paths, string $expectedCommand)
4343

4444
public static function provideData(): Iterator
4545
{
46-
$expectedCommandLinesString = self::createExpectedCommandLinesString();
46+
yield 'no options, single path' => [[], ['src'], self::wrap('', "'src'")];
4747

48-
yield [[], ['src'], $expectedCommandLinesString];
48+
yield 'output-format is excluded' => [
49+
[TestOption::OUTPUT_FORMAT => 'console'],
50+
['src'],
51+
self::wrap('', "'src'"),
52+
];
4953

50-
// output-format is excluded, so it must not change the result
51-
yield [[TestOption::OUTPUT_FORMAT => 'console'], ['src'], $expectedCommandLinesString];
54+
yield 'true bool option becomes a flag' => [
55+
[TestOption::FIX => true],
56+
['src'],
57+
self::wrap('--fix', "'src'"),
58+
];
59+
60+
yield 'false bool option is omitted' => [
61+
[TestOption::FIX => false],
62+
['src'],
63+
self::wrap('', "'src'"),
64+
];
65+
66+
yield 'null option is omitted' => [
67+
[TestOption::MEMORY_LIMIT => null],
68+
['src'],
69+
self::wrap('', "'src'"),
70+
];
71+
72+
yield 'memory-limit uses the assign form' => [
73+
[TestOption::MEMORY_LIMIT => '-1'],
74+
['src'],
75+
self::wrap('--memory-limit=-1', "'src'"),
76+
];
77+
78+
yield 'string option keeps the escaped value' => [
79+
['some-option' => 'value'],
80+
['src'],
81+
self::wrap("--some-option 'value'", "'src'"),
82+
];
83+
84+
yield 'multiple paths are all escaped' => [[], ['src', 'tests'], self::wrap('', "'src' 'tests'")];
85+
86+
yield 'framework global options are excluded' => [
87+
['no-interaction' => true, 'verbose' => true, 'ansi' => true, 'help' => true],
88+
['src'],
89+
self::wrap('', "'src'"),
90+
];
5291
}
5392

54-
private static function createExpectedCommandLinesString(): string
93+
public function testProjectConfigFileIsPassedRightAfterWorkerName(): void
5594
{
56-
$commandLineString = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "'";
95+
$workerCommandLine = $this->workerCommandLineFactory->create(
96+
self::DUMMY_MAIN_SCRIPT,
97+
'worker',
98+
'ecs.php',
99+
[],
100+
['src'],
101+
'identifier',
102+
2000
103+
);
104+
105+
$expectedCommand = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT
106+
. "' worker --config 'ecs.php' --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi";
107+
108+
$this->assertSame($expectedCommand, $workerCommandLine);
109+
}
110+
111+
/**
112+
* Assemble the expected command line: options sit before --port, paths after --identifier.
113+
*/
114+
private static function wrap(string $options, string $paths): string
115+
{
116+
$command = "'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "' worker";
117+
118+
if ($options !== '') {
119+
$command .= ' ' . $options;
120+
}
57121

58-
return $commandLineString . " worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi";
122+
return $command . " --port 2000 --identifier 'identifier' " . $paths . " --output-format 'json' --no-ansi";
59123
}
60124
}

0 commit comments

Comments
 (0)