CommandIsSuccessfulTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Tester\Constraint;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use PHPUnit\Framework\TestCase;
  13. use PHPUnit\Framework\TestFailure;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
  16. final class CommandIsSuccessfulTest extends TestCase
  17. {
  18. public function testConstraint()
  19. {
  20. $constraint = new CommandIsSuccessful();
  21. $this->assertTrue($constraint->evaluate(Command::SUCCESS, '', true));
  22. $this->assertFalse($constraint->evaluate(Command::FAILURE, '', true));
  23. $this->assertFalse($constraint->evaluate(Command::INVALID, '', true));
  24. }
  25. /**
  26. * @dataProvider providesUnsuccessful
  27. */
  28. public function testUnsuccessfulCommand(string $expectedException, int $exitCode)
  29. {
  30. $constraint = new CommandIsSuccessful();
  31. try {
  32. $constraint->evaluate($exitCode);
  33. } catch (ExpectationFailedException $e) {
  34. $this->assertStringContainsString('Failed asserting that the command is successful.', TestFailure::exceptionToString($e));
  35. $this->assertStringContainsString($expectedException, TestFailure::exceptionToString($e));
  36. return;
  37. }
  38. $this->fail();
  39. }
  40. public static function providesUnsuccessful(): iterable
  41. {
  42. yield 'Failed' => ['Command failed.', Command::FAILURE];
  43. yield 'Invalid' => ['Command was invalid.', Command::INVALID];
  44. yield 'Exit code 3' => ['Command returned exit status 3.', 3];
  45. }
  46. }