|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\PhpDoc; |
| 4 | + |
| 5 | +use PHPStan\Testing\PHPStanTestCase; |
| 6 | +use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
| 7 | +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 8 | +use PHPStan\Type\ArrayType; |
| 9 | +use PHPStan\Type\IntersectionType; |
| 10 | +use PHPStan\Type\MixedType; |
| 11 | +use PHPStan\Type\StringType; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | + |
| 15 | +class TypeDescriptionTest extends PHPStanTestCase |
| 16 | +{ |
| 17 | + |
| 18 | + public function dataTest(): iterable |
| 19 | + { |
| 20 | + yield ['string', new StringType()]; |
| 21 | + yield ['array', new ArrayType(new MixedType(), new MixedType())]; |
| 22 | + yield ['literal-string', new IntersectionType([new StringType(), new AccessoryLiteralStringType()])]; |
| 23 | + yield ['non-empty-string', new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()])]; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider dataTest |
| 28 | + */ |
| 29 | + public function testDescriptionToType(string $description, Type $expectedType): void |
| 30 | + { |
| 31 | + $typeStringResolver = self::getContainer()->getByType(TypeStringResolver::class); |
| 32 | + $type = $typeStringResolver->resolve($description); |
| 33 | + $this->assertTrue($expectedType->equals($type), sprintf('Parsing %s did not result in %s, but in %s', $description, $expectedType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value()))); |
| 34 | + |
| 35 | + $newDescription = $type->describe(VerbosityLevel::value()); |
| 36 | + $newType = $typeStringResolver->resolve($newDescription); |
| 37 | + $this->assertTrue($type->equals($newType), sprintf('Parsing %s again did not result in %s, but in %s', $newDescription, $type->describe(VerbosityLevel::value()), $newType->describe(VerbosityLevel::value()))); |
| 38 | + } |
| 39 | + |
| 40 | +} |
0 commit comments