Skip to content

Commit b3705b9

Browse files
[8.x] Add success and failure command assertions (#39435)
* Add success and failure command assertions * Rename to `assertSuccessful` and `assertFailed` * Assert that failures are not success codes * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent c0b7719 commit b3705b9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Illuminate/Testing/PendingCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Mockery;
1111
use Mockery\Exception\NoMatchingExpectationException;
1212
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
13+
use Symfony\Component\Console\Command\Command;
1314
use Symfony\Component\Console\Helper\Table;
1415
use Symfony\Component\Console\Input\ArrayInput;
1516
use Symfony\Component\Console\Output\BufferedOutput;
@@ -51,6 +52,13 @@ class PendingCommand
5152
*/
5253
protected $expectedExitCode;
5354

55+
/**
56+
* The unexpected exit code.
57+
*
58+
* @var int
59+
*/
60+
protected $unexpectedExitCode;
61+
5462
/**
5563
* Determine if the command has executed.
5664
*
@@ -192,6 +200,39 @@ public function assertExitCode($exitCode)
192200
return $this;
193201
}
194202

203+
/**
204+
* Assert that the command does not have the given exit code.
205+
*
206+
* @param int $exitCode
207+
* @return $this
208+
*/
209+
public function assertNotExitCode($exitCode)
210+
{
211+
$this->unexpectedExitCode = $exitCode;
212+
213+
return $this;
214+
}
215+
216+
/**
217+
* Assert that the command has the success exit code.
218+
*
219+
* @return $this
220+
*/
221+
public function assertSuccessful()
222+
{
223+
return $this->assertExitCode(Command::SUCCESS);
224+
}
225+
226+
/**
227+
* Assert that the command does not have the success exit code.
228+
*
229+
* @return $this
230+
*/
231+
public function assertFailed()
232+
{
233+
return $this->assertNotExitCode(Command::SUCCESS);
234+
}
235+
195236
/**
196237
* Execute the command.
197238
*
@@ -230,6 +271,11 @@ public function run()
230271
$this->expectedExitCode, $exitCode,
231272
"Expected status code {$this->expectedExitCode} but received {$exitCode}."
232273
);
274+
} elseif (! is_null($this->unexpectedExitCode)) {
275+
$this->test->assertNotEquals(
276+
$this->unexpectedExitCode, $exitCode,
277+
"Unexpected status code {$this->unexpectedExitCode} was received."
278+
);
233279
}
234280

235281
$this->verifyExpectations();

0 commit comments

Comments
 (0)