|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * PHPStan type tests. |
| 5 | + */ |
| 6 | + |
| 7 | +use Nette\PhpGenerator\ClassType; |
| 8 | +use Nette\PhpGenerator\Closure; |
| 9 | +use Nette\PhpGenerator\Constant; |
| 10 | +use Nette\PhpGenerator\EnumCase; |
| 11 | +use Nette\PhpGenerator\EnumType; |
| 12 | +use Nette\PhpGenerator\GlobalFunction; |
| 13 | +use Nette\PhpGenerator\InterfaceType; |
| 14 | +use Nette\PhpGenerator\Method; |
| 15 | +use Nette\PhpGenerator\Parameter; |
| 16 | +use Nette\PhpGenerator\PhpFile; |
| 17 | +use Nette\PhpGenerator\PhpNamespace; |
| 18 | +use Nette\PhpGenerator\Property; |
| 19 | +use Nette\PhpGenerator\PropertyHook; |
| 20 | +use Nette\PhpGenerator\TraitType; |
| 21 | +use Nette\PhpGenerator\TraitUse; |
| 22 | +use Nette\Utils\Type; |
| 23 | +use function PHPStan\Testing\assertType; |
| 24 | + |
| 25 | + |
| 26 | +function testParameterGetType(Parameter $param): void |
| 27 | +{ |
| 28 | + assertType('string|null', $param->getType()); |
| 29 | + assertType('string|null', $param->getType(false)); |
| 30 | + assertType(Type::class . '|null', $param->getType(true)); |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +function testPropertyGetType(Property $prop): void |
| 35 | +{ |
| 36 | + assertType('string|null', $prop->getType()); |
| 37 | + assertType('string|null', $prop->getType(false)); |
| 38 | + assertType(Type::class . '|null', $prop->getType(true)); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +function testMethodGetReturnType(Method $method): void |
| 43 | +{ |
| 44 | + assertType('string|null', $method->getReturnType()); |
| 45 | + assertType('string|null', $method->getReturnType(false)); |
| 46 | + assertType(Type::class . '|null', $method->getReturnType(true)); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +function testEnumGetCases(EnumType $enum): void |
| 51 | +{ |
| 52 | + assertType('array<string, ' . EnumCase::class . '>', $enum->getCases()); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +function testClosureGetUses(Closure $closure): void |
| 57 | +{ |
| 58 | + assertType('list<' . Parameter::class . '>', $closure->getUses()); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +function testPropertyHookGetParameters(PropertyHook $hook): void |
| 63 | +{ |
| 64 | + assertType('array<string, ' . Parameter::class . '>', $hook->getParameters()); |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +function testPropertyGetHooks(Property $prop): void |
| 69 | +{ |
| 70 | + assertType('array<string, ' . PropertyHook::class . '>', $prop->getHooks()); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +function testMethodGetVisibility(Method $method): void |
| 75 | +{ |
| 76 | + assertType("'private'|'protected'|'public'|null", $method->getVisibility()); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +function testPropertyGetVisibility(Property $prop): void |
| 81 | +{ |
| 82 | + assertType("'private'|'protected'|'public'|null", $prop->getVisibility()); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +function testClassTypeCollections(ClassType $class): void |
| 87 | +{ |
| 88 | + assertType('array<string, ' . Method::class . '>', $class->getMethods()); |
| 89 | + assertType('array<string, ' . Property::class . '>', $class->getProperties()); |
| 90 | + assertType('array<string, ' . Constant::class . '>', $class->getConstants()); |
| 91 | + assertType('array<string, ' . TraitUse::class . '>', $class->getTraits()); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +function testPhpFileCollections(PhpFile $file): void |
| 96 | +{ |
| 97 | + assertType('array<string, ' . PhpNamespace::class . '>', $file->getNamespaces()); |
| 98 | + assertType('array<string, ' . ClassType::class . '|' . EnumType::class . '|' . InterfaceType::class . '|' . TraitType::class . '>', $file->getClasses()); |
| 99 | + assertType('array<string, ' . GlobalFunction::class . '>', $file->getFunctions()); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +function testPhpNamespaceCollections(PhpNamespace $ns): void |
| 104 | +{ |
| 105 | + assertType('array<string, ' . ClassType::class . '|' . EnumType::class . '|' . InterfaceType::class . '|' . TraitType::class . '>', $ns->getClasses()); |
| 106 | + assertType('array<string, ' . GlobalFunction::class . '>', $ns->getFunctions()); |
| 107 | + assertType('array<string, string>', $ns->getUses()); |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +function testTraitUseGetResolutions(TraitUse $trait): void |
| 112 | +{ |
| 113 | + assertType('list<string>', $trait->getResolutions()); |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +function testGlobalFunctionGetParameters(GlobalFunction $fn): void |
| 118 | +{ |
| 119 | + assertType('array<string, ' . Parameter::class . '>', $fn->getParameters()); |
| 120 | +} |
0 commit comments