|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\MethodReflection; |
| 10 | +use PHPStan\Reflection\ParametersAcceptor; |
| 11 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 12 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 13 | +use PHPStan\Type\NeverType; |
| 14 | +use PHPStan\Type\ResourceType; |
| 15 | +use PHPStan\Type\StringType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * @covers \PHPStan\Type\Symfony\RequestDynamicReturnTypeExtension |
| 21 | + */ |
| 22 | +final class RequestDynamicReturnTypeExtensionTest extends TestCase |
| 23 | +{ |
| 24 | + |
| 25 | + public function testImplementsDynamicMethodReturnTypeExtension(): void |
| 26 | + { |
| 27 | + self::assertInstanceOf( |
| 28 | + DynamicMethodReturnTypeExtension::class, |
| 29 | + new RequestDynamicReturnTypeExtension() |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + public function testGetClass(): void |
| 34 | + { |
| 35 | + $extension = new RequestDynamicReturnTypeExtension(); |
| 36 | + self::assertSame('Symfony\Component\HttpFoundation\Request', $extension->getClass()); |
| 37 | + } |
| 38 | + |
| 39 | + public function testIsMethodSupported(): void |
| 40 | + { |
| 41 | + $methodGetContent = $this->createMock(MethodReflection::class); |
| 42 | + $methodGetContent->expects(self::once())->method('getName')->willReturn('getContent'); |
| 43 | + |
| 44 | + $methodFoo = $this->createMock(MethodReflection::class); |
| 45 | + $methodFoo->expects(self::once())->method('getName')->willReturn('foo'); |
| 46 | + |
| 47 | + $extension = new RequestDynamicReturnTypeExtension(); |
| 48 | + self::assertTrue($extension->isMethodSupported($methodGetContent)); |
| 49 | + self::assertFalse($extension->isMethodSupported($methodFoo)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider getTypeFromMethodCallProvider |
| 54 | + * @param MethodReflection $methodReflection |
| 55 | + * @param MethodCall $methodCall |
| 56 | + * @param Type $expectedType |
| 57 | + * @param Scope $scope |
| 58 | + */ |
| 59 | + public function testGetTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Type $expectedType, Scope $scope): void |
| 60 | + { |
| 61 | + $extension = new RequestDynamicReturnTypeExtension(); |
| 62 | + $type = $extension->getTypeFromMethodCall( |
| 63 | + $methodReflection, |
| 64 | + $methodCall, |
| 65 | + $scope |
| 66 | + ); |
| 67 | + self::assertEquals($expectedType, $type); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return mixed[] |
| 72 | + */ |
| 73 | + public function getTypeFromMethodCallProvider(): array |
| 74 | + { |
| 75 | + $scopeAsString = $this->createMock(Scope::class); |
| 76 | + $scopeAsString->expects(self::once())->method('getType')->willReturn(new ConstantBooleanType(false)); |
| 77 | + |
| 78 | + $scopeAsResource = $this->createMock(Scope::class); |
| 79 | + $scopeAsResource->expects(self::once())->method('getType')->willReturn(new ConstantBooleanType(true)); |
| 80 | + |
| 81 | + $scopeUnknown = $this->createMock(Scope::class); |
| 82 | + $scopeUnknown->expects(self::once())->method('getType')->willReturn($this->createMock(Type::class)); |
| 83 | + |
| 84 | + $parametersAcceptorUnknown = $this->createMock(ParametersAcceptor::class); |
| 85 | + $parametersAcceptorUnknown->expects(self::once())->method('getReturnType')->willReturn(new NeverType()); |
| 86 | + |
| 87 | + $methodReflectionUnknown = $this->createMock(MethodReflection::class); |
| 88 | + $methodReflectionUnknown->expects(self::once())->method('getVariants')->willReturn([$parametersAcceptorUnknown]); |
| 89 | + |
| 90 | + return [ |
| 91 | + 'noArgument' => [ |
| 92 | + $this->createMock(MethodReflection::class), |
| 93 | + new MethodCall($this->createMock(Expr::class), ''), |
| 94 | + new StringType(), |
| 95 | + $this->createMock(Scope::class), |
| 96 | + ], |
| 97 | + 'asString' => [ |
| 98 | + $this->createMock(MethodReflection::class), |
| 99 | + new MethodCall($this->createMock(Expr::class), '', [new Arg($this->createMock(Expr::class))]), |
| 100 | + new StringType(), |
| 101 | + $scopeAsString, |
| 102 | + ], |
| 103 | + 'asResource' => [ |
| 104 | + $this->createMock(MethodReflection::class), |
| 105 | + new MethodCall($this->createMock(Expr::class), '', [new Arg($this->createMock(Expr::class))]), |
| 106 | + new ResourceType(), |
| 107 | + $scopeAsResource, |
| 108 | + ], |
| 109 | + 'unknown' => [ |
| 110 | + $methodReflectionUnknown, |
| 111 | + new MethodCall($this->createMock(Expr::class), '', [new Arg($this->createMock(Expr::class))]), |
| 112 | + new NeverType(), |
| 113 | + $scopeUnknown, |
| 114 | + ], |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments