Skip to content

Commit

Permalink
dont mock console output by default
Browse files Browse the repository at this point in the history
  • Loading branch information
SjorsO committed Sep 5, 2018
1 parent 6a8ab13 commit b433970
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.');
Expand All @@ -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;
}
}
18 changes: 16 additions & 2 deletions tests/Integration/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}

Expand Down

0 comments on commit b433970

Please sign in to comment.