Skip to content

Commit

Permalink
Be more flexible in help test output comparison to account for variat…
Browse files Browse the repository at this point in the history
…ions in Symfony version
  • Loading branch information
greg-1-anderson committed Dec 13, 2024
1 parent 52733ae commit b9754a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
10 changes: 5 additions & 5 deletions tests/AnnotatedCommandFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,14 +667,14 @@ function testCommandWithNoArguments()
$this->assertInstanceOf('\Symfony\Component\Console\Command\Command', $command);
$this->assertEquals('command:with-no-arguments', $command->getName());
$this->assertEquals('This command has no arguments--only options', $command->getDescription());
$this->assertEquals("Return a result only if not silent.", $command->getHelp());
$this->assertEquals('command:with-no-arguments [-s|--silent]', $command->getSynopsis());
$this->assertEquals("Return a result only if not silence.", $command->getHelp());
$this->assertEquals('command:with-no-arguments [-s|--silence]', $command->getSynopsis());

$input = new StringInput('command:with-no-arguments');
$this->assertRunCommandViaApplicationEquals($command, $input, 'Hello, world');
$input = new StringInput('command:with-no-arguments -s');
$this->assertRunCommandViaApplicationEquals($command, $input, '');
$input = new StringInput('command:with-no-arguments --silent');
$input = new StringInput('command:with-no-arguments --silence');
$this->assertRunCommandViaApplicationEquals($command, $input, '');
}

Expand All @@ -690,13 +690,13 @@ function testCommandWithShortcutOnAnnotation()
$this->assertEquals('shortcut:on-annotation', $command->getName());
$this->assertEquals('Shortcut on annotation', $command->getDescription());
$this->assertEquals("This command defines the option shortcut on the annotation instead of in the options array.", $command->getHelp());
$this->assertEquals('shortcut:on-annotation [-s|--silent]', $command->getSynopsis());
$this->assertEquals('shortcut:on-annotation [-s|--silence]', $command->getSynopsis());

$input = new StringInput('shortcut:on-annotation');
$this->assertRunCommandViaApplicationEquals($command, $input, 'Hello, world');
$input = new StringInput('shortcut:on-annotation -s');
$this->assertRunCommandViaApplicationEquals($command, $input, '');
$input = new StringInput('shortcut:on-annotation --silent');
$input = new StringInput('shortcut:on-annotation --silence');
$this->assertRunCommandViaApplicationEquals($command, $input, '');
}

Expand Down
41 changes: 18 additions & 23 deletions tests/HelpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ public function addDiscoveredCommands($factory, $commandFiles) {
}
}

function assertRunCommandViaApplicationEquals($cmd, $expectedOutput, $expectedStatusCode = 0)
function assertRunCommandViaApplicationContains($cmd, $containsList, $expectedStatusCode = 0)
{
$input = new StringInput($cmd);
$output = new BufferedOutput();

$statusCode = $this->application->run($input, $output);
$commandOutput = trim($output->fetch());
$commandOutput = $this->simplifyWhitespace($output->fetch());

$expectedOutput = $this->simplifyWhitespace($expectedOutput);
$commandOutput = $this->simplifyWhitespace($commandOutput);

$this->assertEquals($expectedOutput, $commandOutput);
foreach ($containsList as $contains) {
$contains = $this->simplifyWhitespace($contains);
$this->assertStringContainsString($contains, $commandOutput);
}
$this->assertEquals($expectedStatusCode, $statusCode);
}

Expand Down Expand Up @@ -120,7 +120,8 @@ function testHelp()
$expectedFieldMessage = "Select just one field, and force format to 'string'.";
}

$expectedXML = <<<EOT
$expectedXML = [
<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<command id="example:table" name="example:table">
<usages>
Expand Down Expand Up @@ -165,9 +166,8 @@ function testHelp()
<option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
<description>$htmlEncodedHelpMessage</description>\n
</option>
<option name="--quiet" shortcut="-q" accept_value="0" is_value_required="0" is_multiple="0">
<description>Do not output any message</description>
</option>
EOT,
<<<EOT
<option name="--verbose" shortcut="-v" shortcuts="-v|-vv|-vvv" accept_value="0" is_value_required="0" is_multiple="0">
<description>Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug</description>
</option>
Expand All @@ -192,16 +192,17 @@ function testHelp()
<topic>docs-tables</topic>
</topics>
</command>
EOT;
EOT];

$this->assertRunCommandViaApplicationEquals('my-help --format=xml example:table', $expectedXML);
$this->assertRunCommandViaApplicationContains('my-help --format=xml example:table', $expectedXML);

$encodedAnsiMessage = json_encode($expectedAnsiMessage);
$encodedNoAnsiMessage = json_encode($expectedNoAnsiMessage);
$encodedHelpMessage = json_encode(strip_tags($expectedHelpMessage));
$encodedFieldMessage = json_encode($expectedFieldMessage);

$expectedJSON = <<<EOT
$expectedJSON = [
<<<EOT
{
"id": "example:table",
"name": "example:table",
Expand Down Expand Up @@ -267,14 +268,8 @@ function testHelp()
"is_multiple": "0",
"description": $encodedHelpMessage
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": "0",
"is_value_required": "0",
"is_multiple": "0",
"description": "Do not output any message"
},
EOT,
<<<EOT
"verbose": {
"name": "--verbose",
"shortcut": "-v",
Expand Down Expand Up @@ -325,7 +320,7 @@ function testHelp()
"docs-tables"
]
}
EOT;
$this->assertRunCommandViaApplicationEquals('my-help --format=json example:table', $expectedJSON);
EOT];
$this->assertRunCommandViaApplicationContains('my-help --format=json example:table', $expectedJSON);
}
}
14 changes: 7 additions & 7 deletions tests/src/ExampleCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ public function commandWithIOParameters(InputInterface $input, OutputInterface $
/**
* This command has no arguments--only options
*
* Return a result only if not silent.
* Return a result only if not silence.
*
* @option silent Supress output.
* @option silence Supress output.
*/
public function commandWithNoArguments(array $opts = ['silent|s' => false])
public function commandWithNoArguments(array $opts = ['silence|s' => false])
{
if (!$opts['silent']) {
if (!$opts['silence']) {
return "Hello, world";
}
}
Expand All @@ -290,11 +290,11 @@ public function commandWithNoArguments(array $opts = ['silent|s' => false])
* This command defines the option shortcut on the annotation instead of in the options array.
*
* @param $opts The options
* @option silent|s Supress output.
* @option silence|s Supress output.
*/
public function shortcutOnAnnotation(array $opts = ['silent' => false])
public function shortcutOnAnnotation(array $opts = ['silence' => false])
{
if (!$opts['silent']) {
if (!$opts['silence']) {
return "Hello, world";
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/alpha/AlphaCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ public function withoutAnnotations()
*
* Return a result only if not silent.
*
* @option silent Supress output.
* @option silence Supress output.
*/
public function commandWithOneOptionalArgument($who = 'world', $opts = ['silent|s' => false])
public function commandWithOneOptionalArgument($who = 'world', $opts = ['silence|s' => false])
{
if (!$opts['silent']) {
if (!$opts['silence']) {
return "Hello, $who";
}
}
Expand Down

0 comments on commit b9754a2

Please sign in to comment.