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

[6.x] Allow commands to overwrite their input and output handlers #30706

Merged
merged 5 commits into from
Nov 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function options()
* Confirm a question with the user.
*
* @param string $question
* @param bool $default
* @param bool $default
* @return bool
*/
public function confirm($question, $default = false)
Expand Down Expand Up @@ -433,7 +433,7 @@ public function askWithCompletion($question, $choices, $default = null)
* Prompt the user for input but hide the answer from the console.
*
* @param string $question
* @param bool $fallback
* @param bool $fallback
* @return mixed
*/
public function secret($question, $fallback = true)
Expand All @@ -449,10 +449,10 @@ public function secret($question, $fallback = true)
* Give the user a single choice from an array of answers.
*
* @param string $question
* @param array $choices
* @param array $choices
* @param string|null $default
* @param mixed|null $attempts
* @param bool|null $multiple
* @param mixed|null $attempts
* @param bool|null $multiple
* @return string
*/
public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
Expand All @@ -467,10 +467,10 @@ public function choice($question, array $choices, $default = null, $attempts = n
/**
* Format input to textual table.
*
* @param array $headers
* @param array $headers
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
* @param string $tableStyle
* @param array $columnStyles
* @param array $columnStyles
* @return void
*/
public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
Expand Down Expand Up @@ -588,6 +588,28 @@ public function alert($string)
$this->output->newLine();
}

/**
* Set the input interface implementation.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return void
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
}

/**
* Set the output interface implementation.
*
* @param \Illuminate\Console\OutputStyle $output
* @return void
*/
public function setOutput(OutputStyle $output)
{
$this->output = $output;
}

/**
* Set the verbosity level.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;

Expand Down Expand Up @@ -87,4 +88,28 @@ protected function getOptions()
$this->assertEquals('test-first-option', $command->option('option-one'));
$this->assertEquals('test-second-option', $command->option('option-two'));
}

public function testTheInputSetterOverwrite()
{
$input = m::mock(InputInterface::class);
$input->shouldReceive('hasArgument')->once()->with('foo')->andReturn(false);

$command = new Command;
$command->setInput($input);

$this->assertFalse($command->hasArgument('foo'));
}

public function testTheOutputSetterOverwrite()
{
$output = m::mock(OutputStyle::class);
$output->shouldReceive('writeln')->once()->withArgs(function (...$args) {
return $args[0] === '<info>foo</info>';
});

$command = new Command;
$command->setOutput($output);

$command->info('foo');
}
}