From 78b5b71b94f5fe432c9fa865b88308431361c780 Mon Sep 17 00:00:00 2001 From: George Steel Date: Tue, 2 Jan 2024 14:49:47 +0000 Subject: [PATCH] Adds a `__toString()` method that proxies to the decorated input Signed-off-by: George Steel --- src/Input/AbstractParamAwareInput.php | 5 ++++- test/Input/ParamAwareInputTest.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Input/AbstractParamAwareInput.php b/src/Input/AbstractParamAwareInput.php index bec5ba4..88f69e7 100644 --- a/src/Input/AbstractParamAwareInput.php +++ b/src/Input/AbstractParamAwareInput.php @@ -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(); } } diff --git a/test/Input/ParamAwareInputTest.php b/test/Input/ParamAwareInputTest.php index 870a29e..0bc111a 100644 --- a/test/Input/ParamAwareInputTest.php +++ b/test/Input/ParamAwareInputTest.php @@ -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; @@ -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()); + } }