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

[8.x] Add success and failure command assertions #39435

Merged
merged 4 commits into from
Nov 2, 2021
Merged
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
46 changes: 46 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mockery;
use Mockery\Exception\NoMatchingExpectationException;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -51,6 +52,13 @@ class PendingCommand
*/
protected $expectedExitCode;

/**
* The unexpected exit code.
*
* @var int
*/
protected $unexpectedExitCode;

/**
* Determine if the command has executed.
*
Expand Down Expand Up @@ -192,6 +200,39 @@ public function assertExitCode($exitCode)
return $this;
}

/**
* Assert that the command does not have the given exit code.
*
* @param int $exitCode
* @return $this
*/
public function assertNotExitCode($exitCode)
{
$this->unexpectedExitCode = $exitCode;

return $this;
}

/**
* Assert that the command has the success exit code.
*
* @return $this
*/
public function assertSuccessful()
{
return $this->assertExitCode(Command::SUCCESS);
}

/**
* Assert that the command does not have the success exit code.
*
* @return $this
*/
public function assertFailed()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if this should assert exit code not equal to zero. Commands can fail with exit codes other than 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right, @GrahamCampbell, I updated the implementation based on this 👍

{
return $this->assertNotExitCode(Command::SUCCESS);
}

/**
* Execute the command.
*
Expand Down Expand Up @@ -230,6 +271,11 @@ public function run()
$this->expectedExitCode, $exitCode,
"Expected status code {$this->expectedExitCode} but received {$exitCode}."
);
} elseif (! is_null($this->unexpectedExitCode)) {
$this->test->assertNotEquals(
$this->unexpectedExitCode, $exitCode,
"Unexpected status code {$this->unexpectedExitCode} was received."
);
}

$this->verifyExpectations();
Expand Down