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

[5.7] Call Pending artisan command immediately #25574

Merged
merged 3 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion src/Illuminate/Foundation/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.');
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down