8000 feature #42881 [Console] Add more context when CommandIsSuccessful fa… · symfony/symfony@c4df3a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4df3a7

Browse files
committed
feature #42881 [Console] Add more context when CommandIsSuccessful fails (yoannrenard)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Console] Add more context when CommandIsSuccessful fails | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Give some more context when CommandIsSuccessful::assertCommandIsSuccessful() fails Commits ------- 695cd9d [Console] Add more context when CommandIsSuccessful fails
2 parents 2aa388e + 695cd9d commit c4df3a7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Symfony/Component/Console/Tester/Constraint/CommandIsSuccessful.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ protected function failureDescription($other): string
3939
{
4040
return 'the command '.$this->toString();
4141
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function additionalFailureDescription($other): string
47+
{
48+
$mapping = [
49+
Command::FAILURE => 'Command failed.',
50+
Command::INVALID => 'Command was invalid.',
51+
];
52+
53+
return $mapping[$other] ?? sprintf('Command returned exit status %d.', $other);
54+
}
4255
}

src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsSuccessfulTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,31 @@ public function testConstraint()
2626
$this->assertTrue($constraint->evaluate(Command::SUCCESS, '', true));
2727
$this->assertFalse($constraint->evaluate(Command::FAILURE, '', true));
2828
$this->assertFalse($constraint->evaluate(Command::INVALID, '', true));
29+
}
30+
31+
/**
32+
* @dataProvider providesUnsuccessful
33+
*/
34+
public function testUnsuccessfulCommand(string $expectedException, int $exitCode)
35+
{
36+
$constraint = new CommandIsSuccessful();
2937

3038
try {
31-
$constraint->evaluate(Command::FAILURE);
39+
$constraint->evaluate($exitCode);
3240
} catch (ExpectationFailedException $e) {
3341
$this->assertStringContainsString('Failed asserting that the command is successful.', TestFailure::exceptionToString($e));
42+
$this->assertStringContainsString($expectedException, TestFailure::exceptionToString($e));
3443

3544
return;
3645
}
3746

3847
$this->fail();
3948
}
49+
50+
public function providesUnsuccessful(): iterable
51+
{
52+
yield 'Failed' => ['Command failed.', Command::FAILURE];
53+
yield 'Invalid' => ['Command was invalid.', Command::INVALID];
54+
yield 'Exit code 3' => ['Command returned exit status 3.', 3];
55+
}
4056
}

0 commit comments

Comments
 (0)
0