Skip to content

Commit

Permalink
[Console] Add missing assertCommandIsFaulty assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Nov 7, 2023
1 parent d308e2c commit ed57c3c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG-6.4.md
Expand Up @@ -7,6 +7,9 @@ in 6.4 minor versions.
To get the diff for a specific change, go to https://github.com/symfony/symfony/commit/XXX where XXX is the change hash
To get the diff between two versions, go to https://github.com/symfony/symfony/compare/v6.4.0...v6.4.1

* 6.4.0-BETA3
* feature #52478 [Console] Add missing assertCommandIsFaulty assertion (raphaelstolt)

* 6.4.0-BETA2 (2023-10-29)

* bug #52329 [HttpClient] Psr18Client: parse HTTP Reason Phrase for Response (Hanmac)
Expand Down
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Raphael Stolt <raphael.stolt@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tester\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Console\Command\Command;

final class CommandIsFaulty extends Constraint
{
public function toString(): string
{
return 'is faulty';
}

protected function matches($other): bool
{
return Command::FAILURE === $other || Command::INVALID === $other;
}

protected function failureDescription($other): string
{
return 'the command '.$this->toString();
}

protected function additionalFailureDescription($other): string
{
$mapping = [
Command::SUCCESS => 'Command was successful.'
];

return $mapping[$other] ?? sprintf('Command returned exit status %d.', $other);
}
}
8 changes: 7 additions & 1 deletion src/Symfony/Component/Console/Tester/TesterTrait.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Tester\Constraint\CommandIsFaulty;
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;

/**
Expand Down Expand Up @@ -104,6 +105,11 @@ public function assertCommandIsSuccessful(string $message = ''): void
Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
}

public function assertCommandIsFaulty(string $message = ''): void
{
Assert::assertThat($this->statusCode, new CommandIsFaulty(), $message);
}

/**
* Sets the user inputs.
*
Expand All @@ -130,7 +136,7 @@ public function setInputs(array $inputs): static
*/
private function initOutput(array $options): void
{
$this->captureStreamsIndependently = $options['capture_stderr_separately'] ?? false;
$this->captureStreamsIndependently = \array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
if (!$this->captureStreamsIndependently) {
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
if (isset($options['decorated'])) {
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/Console/Tests/CommandIsFaultyTest.php
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Raphael Stolt <raphael.stolt@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Tester\Constraint;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\Constraint\CommandIsFaulty;

final class CommandIsFaultyTest extends TestCase
{
public function testConstraint()
{
$constraint = new CommandIsFaulty();

$this->assertFalse($constraint->evaluate(Command::SUCCESS, '', true));
$this->assertTrue($constraint->evaluate(Command::FAILURE, '', true));
$this->assertTrue($constraint->evaluate(Command::INVALID, '', true));
}

public function testSuccessfulCommand()
{
$constraint = new CommandIsFaulty();

try {
$constraint->evaluate(Command::SUCCESS);
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('Failed asserting that the command is faulty.', $e->getMessage());

return;
}

$this->fail();
}
}

0 comments on commit ed57c3c

Please sign in to comment.