Skip to content

Commit

Permalink
Adds a __toString() method that proxies to the decorated input
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jan 2, 2024
1 parent 87374d1 commit 78b5b71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Input/AbstractParamAwareInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,11 @@ static function (mixed $value) use ($originalValidator) {
return $originalValidator;
}

/**
* Returns a stringified representation of the args passed to the command.
*/
public function __toString(): string
{
return '';
return $this->input->__toString();
}
}
21 changes: 21 additions & 0 deletions test/Input/ParamAwareInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ReflectionMethod;
use Symfony\Component\Console\Formatter\NullOutputFormatter;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StreamableInputInterface;
Expand Down Expand Up @@ -636,4 +637,24 @@ public function testGetParamAllowsEmptyValuesForParamsWithValidationIfParamIsNot

$this->assertNull($input->getParam('non-required'));
}

public function testThatCastingToAStringProxiesToTheUnderlyingInputImplementation(): void
{
/**
* Mocking a concrete class here because `__toString` is not part of the InputInterface contract
*/
$decoratedInput = $this->createMock(ArrayInput::class);
$decoratedInput->expects(self::atLeastOnce())
->method('__toString')
->willReturn('something');

$input = new $this->class(
$decoratedInput,
$this->output,
$this->helper,
$this->params,
);

self::assertSame('something', $input->__toString());
}
}

0 comments on commit 78b5b71

Please sign in to comment.