|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace unit\phpDocumentor\Reflection\Php\Expression; |
| 6 | + |
| 7 | +use phpDocumentor\Reflection\Fqsen; |
| 8 | +use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter; |
| 9 | +use phpDocumentor\Reflection\PseudoTypes\True_; |
| 10 | +use phpDocumentor\Reflection\Types\Null_; |
| 11 | +use PhpParser\ParserFactory; |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +#[CoversClass(ExpressionPrinter::class)] |
| 17 | +final class ExpressionPrinterTest extends TestCase |
| 18 | +{ |
| 19 | + /** @param array<string, Type> $expectedParts */ |
| 20 | + #[DataProvider('argumentProvider')] |
| 21 | + public function testArgumentIsParsed(string $code, string $expectedExpression, array $expectedParts): void |
| 22 | + { |
| 23 | + $parser = (new ParserFactory())->createForHostVersion(); |
| 24 | + $node = $parser->parse($code); |
| 25 | + $printer = new ExpressionPrinter(); |
| 26 | + |
| 27 | + $expression = $printer->prettyPrintExpr($node[0]->params[0]->default); |
| 28 | + |
| 29 | + self::assertSame($expectedExpression, $expression); |
| 30 | + self::assertEquals($expectedParts, $printer->getParts()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return array<string, array{ |
| 35 | + * 'code': string, |
| 36 | + * 'expectedExpression': string, |
| 37 | + * 'expectedParts': array<string, Type> |
| 38 | + * }> |
| 39 | + */ |
| 40 | + public static function argumentProvider(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + 'myClassDefault' => [ |
| 44 | + 'code' => '<?php function foo(MyClass $arg = new MyClass()) {}', |
| 45 | + 'expectedExpression' => 'new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}()', |
| 46 | + 'expectedParts' => [ |
| 47 | + '{{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}' => new Fqsen('\MyClass'), |
| 48 | + ], |
| 49 | + ], |
| 50 | + // Enum case default. |
| 51 | + // After first run, replace ENUM_PLACEHOLDER below with the actual placeholder shown in the failure output. |
| 52 | + 'enumCaseDefault' => [ |
| 53 | + 'code' => '<?php function foo(MyEnum $arg = MyEnum::CaseA) {}', |
| 54 | + 'expectedExpression' => '{{ PHPDOC8844445ee68bb81ea3fd9529f906598b }}', |
| 55 | + 'expectedParts' => [ |
| 56 | + '{{ PHPDOC8844445ee68bb81ea3fd9529f906598b }}' => new Fqsen('\MyEnum::CaseA'), |
| 57 | + ], |
| 58 | + ], |
| 59 | + 'classConstantDefault' => [ |
| 60 | + 'code' => '<?php function foo(MyClass $arg = MyClass::SOME_CONST) {}', |
| 61 | + 'expectedExpression' => '{{ PHPDOCe54b7c24dd0f847c3193039223751b3d }}', |
| 62 | + 'expectedParts' => [ |
| 63 | + '{{ PHPDOCe54b7c24dd0f847c3193039223751b3d }}' => new Fqsen('\MyClass::SOME_CONST'), |
| 64 | + ], |
| 65 | + ], |
| 66 | + 'stringDefault' => [ |
| 67 | + 'code' => '<?php function foo(string $arg = \'hello\') {}', |
| 68 | + 'expectedExpression' => "'hello'", |
| 69 | + 'expectedParts' => [], |
| 70 | + ], |
| 71 | + 'intDefault' => [ |
| 72 | + 'code' => '<?php function foo(int $arg = 42) {}', |
| 73 | + 'expectedExpression' => '42', |
| 74 | + 'expectedParts' => [], |
| 75 | + ], |
| 76 | + 'booleanDefault' => [ |
| 77 | + 'code' => '<?php function foo(bool $arg = true) {}', |
| 78 | + 'expectedExpression' => '{{ PHPDOCb326b5062b2f0e69046810717534cb09 }}', |
| 79 | + 'expectedParts' => [ |
| 80 | + '{{ PHPDOCb326b5062b2f0e69046810717534cb09 }}' => new True_(), |
| 81 | + ], |
| 82 | + ], |
| 83 | + 'nullDefault' => [ |
| 84 | + 'code' => '<?php function foo(bool|null $arg = null) {}', |
| 85 | + 'expectedExpression' => '{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}', |
| 86 | + 'expectedParts' => [ |
| 87 | + '{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}' => new Null_(), |
| 88 | + ], |
| 89 | + ], |
| 90 | + 'emptyArrayDefault' => [ |
| 91 | + 'code' => '<?php function foo(array $arg = []) {}', |
| 92 | + 'expectedExpression' => '[]', |
| 93 | + 'expectedParts' => [], |
| 94 | + ], |
| 95 | + 'intArrayDefault' => [ |
| 96 | + 'code' => '<?php function foo(array $arg = [1, 2]) {}', |
| 97 | + 'expectedExpression' => '[1, 2]', |
| 98 | + 'expectedParts' => [], |
| 99 | + ], |
| 100 | + 'stringArrayDefault' => [ |
| 101 | + 'code' => '<?php function foo(array $arg = [\'hello\', \'world\']) {}', |
| 102 | + 'expectedExpression' => "['hello', 'world']", |
| 103 | + 'expectedParts' => [], |
| 104 | + ], |
| 105 | + 'objectArrayDefault' => [ |
| 106 | + 'code' => '<?php function foo(array $arg = [new MyClass(), new MyClass()]) {}', |
| 107 | + 'expectedExpression' => '[new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}(), new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}()]', |
| 108 | + 'expectedParts' => [ |
| 109 | + '{{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}' => new Fqsen('\MyClass'), |
| 110 | + ], |
| 111 | + ], |
| 112 | + ]; |
| 113 | + } |
| 114 | +} |
0 commit comments