|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Console\Tests\ArgumentResolver\ValueResolver; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Console\ArgumentResolver\ValueResolver\FixedValueResolver; |
| 17 | +use Symfony\Component\Console\Attribute\Reflection\ReflectionMember; |
| 18 | +use Symfony\Component\Console\Input\ArrayInput; |
| 19 | + |
| 20 | +class FixedValueResolverTest extends TestCase |
| 21 | +{ |
| 22 | + public function testMissingTypeAndArgumentName(): void |
| 23 | + { |
| 24 | + $this->expectExceptionMessage('Please specify at least a type or an argument name to resolve.'); |
| 25 | + |
| 26 | + new FixedValueResolver('Some value'); |
| 27 | + } |
| 28 | + |
| 29 | + public function testResolverWithClosureValue(): void |
| 30 | + { |
| 31 | + $resolverFunction = static fn () => 'Value A'; |
| 32 | + $commandFunction = static fn (string $a) => null; |
| 33 | + |
| 34 | + $resolver = new FixedValueResolver($resolverFunction, 'string', 'a'); |
| 35 | + $parameters = new \ReflectionFunction($commandFunction)->getParameters(); |
| 36 | + $member = new ReflectionMember($parameters[0]); |
| 37 | + |
| 38 | + $result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member); |
| 39 | + |
| 40 | + $this->assertSame([$resolverFunction()], $result); |
| 41 | + } |
| 42 | + |
| 43 | + public function testResolverWithClosureValueAndType(): void |
| 44 | + { |
| 45 | + $resolverFunction = static fn () => 'Value A'; |
| 46 | + $commandFunction = static fn (\Closure $a) => null; |
| 47 | + |
| 48 | + $resolver = new FixedValueResolver($resolverFunction, \Closure::class, 'a'); |
| 49 | + $parameters = new \ReflectionFunction($commandFunction)->getParameters(); |
| 50 | + $member = new ReflectionMember($parameters[0]); |
| 51 | + |
| 52 | + $result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member); |
| 53 | + |
| 54 | + $this->assertSame([$resolverFunction], $result); |
| 55 | + } |
| 56 | + |
| 57 | + #[DataProvider('provideTypeNames')] |
| 58 | + public function testResolverWithTypeName(callable $function, ?string $type, ?string $argumentName, mixed $expectedValue): void |
| 59 | + { |
| 60 | + $resolver = new FixedValueResolver($expectedValue, $type, $argumentName); |
| 61 | + $parameters = new \ReflectionFunction($function)->getParameters(); |
| 62 | + $member = new ReflectionMember($parameters[0]); |
| 63 | + |
| 64 | + $result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member); |
| 65 | + |
| 66 | + $this->assertSame([$expectedValue], $result); |
| 67 | + } |
| 68 | + |
| 69 | + public static function provideTypeNames(): iterable |
| 70 | + { |
| 71 | + yield 'only type "string"' => [static fn (string $a) => null, 'string', null, 'Some string']; |
| 72 | + yield 'only type "bool"' => [static fn (bool $a) => null, 'bool', null, true]; |
| 73 | + yield 'only type "int"' => [static fn (int $a) => null, 'int', null, 1]; |
| 74 | + yield 'only type "float"' => [static fn (float $a) => null, 'float', null, 1.0]; |
| 75 | + yield 'only type stdClass' => [static fn (\stdClass $a) => null, \stdClass::class, null, (object) ['a' => true]]; |
| 76 | + |
| 77 | + yield 'only argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 78 | + yield 'only argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 79 | + |
| 80 | + yield 'both type "string" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 81 | + yield 'both type "bool" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 82 | + yield 'both type "int" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 83 | + yield 'both type "float" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 84 | + yield 'both type stdClass and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A']; |
| 85 | + |
| 86 | + yield 'both type "string" and argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 87 | + yield 'both type "bool" and argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 88 | + yield 'both type "int" and argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 89 | + yield 'both type "float" and argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 90 | + yield 'both type stdClass and argument name $πͺ' => [static fn (string $πͺ) => null, null, 'πͺ', 'Value πͺ']; |
| 91 | + } |
| 92 | +} |
0 commit comments