Skip to content

Commit

Permalink
[6.x] Allow commands to overwrite their input and output handlers (#3…
Browse files Browse the repository at this point in the history
…0706)

* allow commands to overwrite their input and output handler

* add tests

* fix phpdoc

* fix return type phpdoc spaces

* Update Command.php
  • Loading branch information
gocanto authored and taylorotwell committed Nov 29, 2019
1 parent 87bde76 commit afb1669
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
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');
}
}

0 comments on commit afb1669

Please sign in to comment.