|
2 | 2 |
|
3 | 3 | namespace PHPStan\Type\Nette;
|
4 | 4 |
|
5 |
| -use Nette\Utils\ArrayHash; |
6 |
| -use PhpParser\Node\Arg; |
7 |
| -use PhpParser\Node\Expr; |
8 |
| -use PhpParser\Node\Expr\MethodCall; |
9 |
| -use PHPStan\Analyser\Scope; |
10 |
| -use PHPStan\Reflection\FunctionVariant; |
11 |
| -use PHPStan\Reflection\MethodReflection; |
12 |
| -use PHPStan\Type\ArrayType; |
13 |
| -use PHPStan\Type\Constant\ConstantBooleanType; |
14 |
| -use PHPStan\Type\Generic\TemplateTypeMap; |
15 |
| -use PHPStan\Type\IterableType; |
16 |
| -use PHPStan\Type\MixedType; |
17 |
| -use PHPStan\Type\ObjectType; |
18 |
| -use PHPStan\Type\UnionType; |
19 |
| -use PHPStan\Type\VerbosityLevel; |
20 |
| -use PHPUnit\Framework\TestCase; |
| 5 | +use PHPStan\Testing\TypeInferenceTestCase; |
21 | 6 |
|
22 |
| -final class FormContainerValuesDynamicReturnTypeExtensionTest extends TestCase |
| 7 | +final class FormContainerValuesDynamicReturnTypeExtensionTest extends TypeInferenceTestCase |
23 | 8 | {
|
24 | 9 |
|
25 |
| - private FormContainerValuesDynamicReturnTypeExtension $extension; |
26 |
| - |
27 |
| - protected function setUp(): void |
| 10 | + /** |
| 11 | + * @return iterable<string, mixed[]> |
| 12 | + */ |
| 13 | + public static function dataFileAsserts(): iterable |
28 | 14 | {
|
29 |
| - $this->extension = new FormContainerValuesDynamicReturnTypeExtension(); |
| 15 | + yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerModel.php'); |
30 | 16 | }
|
31 | 17 |
|
32 |
| - public function testParameterAsArray(): void |
| 18 | + /** |
| 19 | + * @dataProvider dataFileAsserts |
| 20 | + * @param mixed ...$args |
| 21 | + */ |
| 22 | + public function testFileAsserts( |
| 23 | + string $assertType, |
| 24 | + string $file, |
| 25 | + ...$args |
| 26 | + ): void |
33 | 27 | {
|
34 |
| - $methodReflection = $this->createMock(MethodReflection::class); |
35 |
| - $methodReflection |
36 |
| - ->method('getVariants') |
37 |
| - ->willReturn([new FunctionVariant( |
38 |
| - TemplateTypeMap::createEmpty(), |
39 |
| - TemplateTypeMap::createEmpty(), |
40 |
| - [], |
41 |
| - true, |
42 |
| - new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]), |
43 |
| - )]); |
44 |
| - |
45 |
| - $scope = $this->createMock(Scope::class); |
46 |
| - $scope->method('getType')->willReturn(new ConstantBooleanType(true)); |
47 |
| - |
48 |
| - $methodCall = $this->createMock(MethodCall::class); |
49 |
| - $arg = $this->createMock(Arg::class); |
50 |
| - $value = $this->createMock(Expr::class); |
51 |
| - $arg->value = $value; |
52 |
| - $methodCall->args = [ |
53 |
| - 0 => $arg, |
54 |
| - ]; |
55 |
| - $methodCall->method('getArgs')->willReturn($methodCall->args); |
56 |
| - |
57 |
| - $resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope); |
58 |
| - |
59 |
| - self::assertInstanceOf(ArrayType::class, $resultType); |
| 28 | + $this->assertFileAsserts($assertType, $file, ...$args); |
60 | 29 | }
|
61 | 30 |
|
62 |
| - public function testParameterAsArrayHash(): void |
| 31 | + public static function getAdditionalConfigFiles(): array |
63 | 32 | {
|
64 |
| - $methodReflection = $this->createMock(MethodReflection::class); |
65 |
| - $methodReflection |
66 |
| - ->method('getVariants') |
67 |
| - ->willReturn([new FunctionVariant(TemplateTypeMap::createEmpty(), TemplateTypeMap::createEmpty(), [], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]))]); |
68 |
| - |
69 |
| - $scope = $this->createMock(Scope::class); |
70 |
| - $scope->method('getType')->willReturn(new ConstantBooleanType(false)); |
71 |
| - |
72 |
| - $methodCall = $this->createMock(MethodCall::class); |
73 |
| - $arg = $this->createMock(Arg::class); |
74 |
| - $value = $this->createMock(Expr::class); |
75 |
| - $arg->value = $value; |
76 |
| - $methodCall->args = [ |
77 |
| - 0 => $arg, |
| 33 | + return [ |
| 34 | + __DIR__ . '/phpstan.neon', |
78 | 35 | ];
|
79 |
| - $methodCall->method('getArgs')->willReturn($methodCall->args); |
80 |
| - |
81 |
| - $resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope); |
82 |
| - |
83 |
| - self::assertInstanceOf(ObjectType::class, $resultType); |
84 |
| - self::assertSame(ArrayHash::class, $resultType->describe(VerbosityLevel::value())); |
85 |
| - } |
86 |
| - |
87 |
| - public function testDefaultParameterIsArrayHash(): void |
88 |
| - { |
89 |
| - $methodReflection = $this->createMock(MethodReflection::class); |
90 |
| - $methodReflection |
91 |
| - ->method('getVariants') |
92 |
| - ->willReturn([new FunctionVariant(TemplateTypeMap::createEmpty(), TemplateTypeMap::createEmpty(), [], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]))]); |
93 |
| - |
94 |
| - $scope = $this->createMock(Scope::class); |
95 |
| - $scope->method('getType')->willReturn(new ConstantBooleanType(false)); |
96 |
| - |
97 |
| - $methodCall = $this->createMock(MethodCall::class); |
98 |
| - $methodCall->args = []; |
99 |
| - $methodCall->method('getArgs')->willReturn($methodCall->args); |
100 |
| - |
101 |
| - $resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope); |
102 |
| - |
103 |
| - self::assertInstanceOf(ObjectType::class, $resultType); |
104 |
| - self::assertSame(ArrayHash::class, $resultType->describe(VerbosityLevel::value())); |
105 | 36 | }
|
106 | 37 |
|
107 | 38 | }
|
0 commit comments