From b4339702dbdc5f1f55f30f1e6576450f6277e3ae Mon Sep 17 00:00:00 2001 From: Sjors Date: Wed, 5 Sep 2018 10:35:47 +0200 Subject: [PATCH] dont mock console output by default --- .../Testing/Concerns/InteractsWithConsole.php | 14 ++++++++++++++ .../Console/ConsoleApplicationTest.php | 18 ++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php index 32488e1facc8..2ea3c7d4da5c 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php @@ -2,10 +2,13 @@ namespace Illuminate\Foundation\Testing\Concerns; +use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\PendingCommand; trait InteractsWithConsole { + protected $mockConsoleOutput = false; + /** * All of the expected output lines. * @@ -29,6 +32,10 @@ trait InteractsWithConsole */ public function artisan($command, $parameters = []) { + if (! $this->mockConsoleOutput) { + return $this->app[Kernel::class]->call($command, $parameters); + } + $this->beforeApplicationDestroyed(function () { if (count($this->expectedQuestions)) { $this->fail('Question "'.array_first($this->expectedQuestions)[0].'" was not asked.'); @@ -41,4 +48,11 @@ public function artisan($command, $parameters = []) return new PendingCommand($this, $this->app, $command, $parameters); } + + protected function withMockedConsoleOutput() + { + $this->mockConsoleOutput = true; + + return $this; + } } diff --git a/tests/Integration/Console/ConsoleApplicationTest.php b/tests/Integration/Console/ConsoleApplicationTest.php index 47cbce569ec6..06ec01a1dd71 100644 --- a/tests/Integration/Console/ConsoleApplicationTest.php +++ b/tests/Integration/Console/ConsoleApplicationTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Integration\Console; use Illuminate\Console\Command; +use Illuminate\Foundation\Testing\PendingCommand; use Orchestra\Testbench\TestCase; use Illuminate\Contracts\Console\Kernel; @@ -19,14 +20,27 @@ public function test_artisan_call_using_command_name() { $exitCode = $this->artisan('foo:bar', [ 'id' => 1, - ])->assertExitCode(0); + ]); + + $this->assertSame(0, $exitCode); } public function test_artisan_call_using_command_class() { $exitCode = $this->artisan(FooCommandStub::class, [ 'id' => 1, - ])->assertExitCode(0); + ]); + + $this->assertSame(0, $exitCode); + } + + public function test_artisan_call_using_command_class_with_mocked_output() + { + $pendingCommand = $this->withMockedConsoleOutput()->artisan(FooCommandStub::class, ['id' => 1]); + + $this->assertInstanceOf(PendingCommand::class, $pendingCommand); + + $pendingCommand->assertExitCode(0); } }