Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing console/tester assertion #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Tester/Constraint/CommandIsFaulty.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 6 additions & 0 deletions Tester/TesterTrait.php
Original file line number Diff line number Diff line change
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 Down
45 changes: 45 additions & 0 deletions Tests/Tester/Constraint/CommandIsFaultyTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}