|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Tests\Fixtures\Type; |
| 15 | + |
| 16 | +use CodeIgniter\Commands\Utilities\ConfigCheck; |
| 17 | +use CodeIgniter\Commands\Utilities\Environment; |
| 18 | +use CodeIgniter\PHPStan\NodeVisitor\ModelReturnTypeTransformVisitor; |
| 19 | +use CodeIgniter\PHPStan\Type\FactoriesFunctionReturnTypeExtension; |
| 20 | +use CodeIgniter\PHPStan\Type\ServicesFunctionReturnTypeExtension; |
| 21 | +use CodeIgniter\Test\CIUnitTestCase; |
| 22 | + |
| 23 | +use function PHPStan\Testing\assertType; |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +final class ReflectionHelperGetPrivateMethodInvokerTest extends CIUnitTestCase |
| 29 | +{ |
| 30 | + public function testOnFirstClassCallable(): void |
| 31 | + { |
| 32 | + assertType( |
| 33 | + 'Closure(object|string, string): (Closure(mixed ...$args=): mixed)', |
| 34 | + self::getPrivateMethodInvoker(...), |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + public function testObjectAsObjectType(): void |
| 39 | + { |
| 40 | + assertType('Closure(): void', self::getPrivateMethodInvoker($this, 'testOnFirstClassCallable')); |
| 41 | + |
| 42 | + $object = new ModelReturnTypeTransformVisitor(); |
| 43 | + assertType('Closure(PhpParser\Node): null', self::getPrivateMethodInvoker($object, 'enterNode')); |
| 44 | + assertType( |
| 45 | + 'Closure(array<PhpParser\Node>): (array<PhpParser\Node>|null)', |
| 46 | + self::getPrivateMethodInvoker($object, 'afterTraverse'), |
| 47 | + ); |
| 48 | + |
| 49 | + $object = new Environment(service('logger'), service('commands')); |
| 50 | + assertType( |
| 51 | + 'Closure(array<int|string, string|null>): (int|void)', |
| 52 | + self::getPrivateMethodInvoker($object, 'run'), |
| 53 | + ); |
| 54 | + assertType('Closure(string): bool', self::getPrivateMethodInvoker($object, 'writeNewEnvironmentToEnvFile')); |
| 55 | + |
| 56 | + $object = new ConfigCheck(service('logger'), service('commands')); |
| 57 | + assertType( |
| 58 | + 'Closure(array<int|string, string|null>): (int|void)', |
| 59 | + self::getPrivateMethodInvoker($object, 'run'), |
| 60 | + ); |
| 61 | + assertType('Closure(object): string', self::getPrivateMethodInvoker($object, 'getVarDump')); |
| 62 | + assertType('Closure(object): string', self::getPrivateMethodInvoker($object, 'getKIntD')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testClassStringAsObjectType(): void |
| 66 | + { |
| 67 | + assertType('Closure(): void', self::getPrivateMethodInvoker(self::class, 'testOnFirstClassCallable')); |
| 68 | + |
| 69 | + $object = ModelReturnTypeTransformVisitor::class; |
| 70 | + assertType('Closure(PhpParser\Node): null', self::getPrivateMethodInvoker($object, 'enterNode')); |
| 71 | + assertType( |
| 72 | + 'Closure(array<PhpParser\Node>): (array<PhpParser\Node>|null)', |
| 73 | + self::getPrivateMethodInvoker($object, 'afterTraverse'), |
| 74 | + ); |
| 75 | + |
| 76 | + $object = FactoriesFunctionReturnTypeExtension::class; |
| 77 | + assertType( |
| 78 | + 'Closure(CodeIgniter\PHPStan\Type\FactoriesReturnTypeHelper): void', |
| 79 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 80 | + ); |
| 81 | + assertType( |
| 82 | + 'Closure(PHPStan\Reflection\FunctionReflection): bool', |
| 83 | + self::getPrivateMethodInvoker($object, 'isFunctionSupported'), |
| 84 | + ); |
| 85 | + assertType( |
| 86 | + 'Closure(PHPStan\Reflection\FunctionReflection, PhpParser\Node\Expr\FuncCall, PHPStan\Analyser\Scope): (PHPStan\Type\Type|null)', |
| 87 | + self::getPrivateMethodInvoker($object, 'getTypeFromFunctionCall'), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + public function testOnNamedArgumentCall(): void |
| 92 | + { |
| 93 | + $object = new ModelReturnTypeTransformVisitor(); |
| 94 | + assertType( |
| 95 | + 'Closure(PhpParser\Node): null', |
| 96 | + self::getPrivateMethodInvoker(method: 'enterNode', obj: $object), |
| 97 | + ); |
| 98 | + assertType( |
| 99 | + 'Closure(array<PhpParser\Node>): (array<PhpParser\Node>|null)', |
| 100 | + self::getPrivateMethodInvoker(obj: $object, method: 'afterTraverse'), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + public function testReturnIsNever(): void |
| 105 | + { |
| 106 | + assertType('*NEVER*', self::getPrivateMethodInvoker('NotClass', 'foo')); |
| 107 | + assertType('*NEVER*', self::getPrivateMethodInvoker($this, 'inexistentMethod')); |
| 108 | + } |
| 109 | + |
| 110 | + public function testOnString(string $object): void |
| 111 | + { |
| 112 | + assertType( |
| 113 | + 'Closure(mixed ...): mixed', |
| 114 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param class-string $object |
| 120 | + */ |
| 121 | + public function testOnClassString(string $object): void |
| 122 | + { |
| 123 | + assertType( |
| 124 | + 'Closure(mixed ...): mixed', |
| 125 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param class-string<ConfigCheck> $class |
| 131 | + */ |
| 132 | + public function testOnGenericClassString(string $class): void |
| 133 | + { |
| 134 | + assertType( |
| 135 | + 'Closure(Psr\Log\LoggerInterface, CodeIgniter\CLI\Commands): void', |
| 136 | + self::getPrivateMethodInvoker($class, '__construct'), |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + public function testOnObject(object $object): void |
| 141 | + { |
| 142 | + assertType( |
| 143 | + 'Closure(mixed ...): mixed', |
| 144 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param self $object |
| 150 | + */ |
| 151 | + public function testOnObjectWithClassType(object $object): void |
| 152 | + { |
| 153 | + assertType( |
| 154 | + 'Closure(non-empty-string): void', |
| 155 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param class-string<ServicesFunctionReturnTypeExtension>|self $object |
| 161 | + */ |
| 162 | + public function testOnUnionOfObjects(object|string $object): void |
| 163 | + { |
| 164 | + assertType( |
| 165 | + '(Closure(CodeIgniter\PHPStan\Type\ServicesReturnTypeHelper): void)|(Closure(non-empty-string): void)', |
| 166 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @param 'NotClass'|class-string<Environment> $object |
| 172 | + */ |
| 173 | + public function testOnUnionOfStringObjectsWithOneNonClass(string $object): void |
| 174 | + { |
| 175 | + assertType( |
| 176 | + '*NEVER*', |
| 177 | + self::getPrivateMethodInvoker($object, '__construct'), |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param '__construct'|'testReturnIsNever' $method |
| 183 | + */ |
| 184 | + public function testOnUnionOfMethods(string $method): void |
| 185 | + { |
| 186 | + assertType( |
| 187 | + '(Closure(): void)|(Closure(non-empty-string): void)', |
| 188 | + self::getPrivateMethodInvoker($this, $method), |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments