diff --git a/src/Illuminate/Foundation/Testing/PendingCommand.php b/src/Illuminate/Foundation/Testing/PendingCommand.php index dd54e6f9780e..cc3a8181ef4b 100644 --- a/src/Illuminate/Foundation/Testing/PendingCommand.php +++ b/src/Illuminate/Foundation/Testing/PendingCommand.php @@ -47,6 +47,13 @@ class PendingCommand */ protected $expectedExitCode; + /** + * Determine if command was called. + * + * @var bool + */ + private $isCalled = false; + /** * Create a new pending console command run. * @@ -104,6 +111,18 @@ public function assertExitCode($exitCode) return $this; } + /** + * Call the given command immediately. + * + * @return int + */ + public function callNow() + { + $this->isCalled = true; + + return $this->app[Kernel::class]->call($this->command, $this->parameters); + } + /** * Mock the application's console output. * @@ -165,10 +184,14 @@ private function createABufferedOutputMock() */ public function __destruct() { + if ($this->isCalled) { + return; + } + $this->mockConsoleOutput(); try { - $exitCode = $this->app[Kernel::class]->call($this->command, $this->parameters); + $exitCode = $this->callNow(); } catch (NoMatchingExpectationException $e) { if ($e->getMethodName() == 'askQuestion') { $this->test->fail('Unexpected question "'.$e->getActualArguments()[0]->getQuestion().'" was asked.'); diff --git a/tests/Integration/Console/ConsoleApplicationTest.php b/tests/Integration/Console/ConsoleApplicationTest.php index b5424ed960ab..ddcba2e8c2e3 100644 --- a/tests/Integration/Console/ConsoleApplicationTest.php +++ b/tests/Integration/Console/ConsoleApplicationTest.php @@ -28,6 +28,28 @@ public function test_artisan_call_using_command_class() 'id' => 1, ])->assertExitCode(0); } + + public function test_artisan_call_now() + { + $outputWithoutMock = $this->artisan('foo:bar', [ + 'id' => 1, + ])->callNow(); + + $this->assertSame(0, $outputWithoutMock); + } + + public function test_artisan_with_mock_call_after_call_now() + { + $outputWithoutMock = $this->artisan('foo:bar', [ + 'id' => 1, + ])->callNow(); + $outputWithMock = $this->artisan('foo:bar', [ + 'id' => 1, + ]); + + $this->assertSame(0, $outputWithoutMock); + $outputWithMock->assertExitCode(0); + } } class FooCommandStub extends Command