|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Lookyman\PHPStan\Symfony\Type; |
| 6 | + |
| 7 | +use Lookyman\PHPStan\Symfony\ServiceMap; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\MethodReflection; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\ObjectType; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use PhpParser\Node\Arg; |
| 15 | +use PhpParser\Node\Expr; |
| 16 | +use PhpParser\Node\Expr\MethodCall; |
| 17 | +use PhpParser\Node\Scalar\String_; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @covers \Lookyman\PHPStan\Symfony\Type\ContainerInterfaceDynamicReturnTypeExtension |
| 22 | + */ |
| 23 | +final class ContainerInterfaceDynamicReturnTypeExtensionTest extends TestCase |
| 24 | +{ |
| 25 | + |
| 26 | + public function testImplementsDynamicMethodReturnTypeExtension() |
| 27 | + { |
| 28 | + self::assertInstanceOf( |
| 29 | + DynamicMethodReturnTypeExtension::class, |
| 30 | + new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml')) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testGetClass() |
| 35 | + { |
| 36 | + $extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml')); |
| 37 | + self::assertEquals(ContainerInterface::class, $extension::getClass()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testIsMethodSupported() |
| 41 | + { |
| 42 | + $methodGet = $this->createMock(MethodReflection::class); |
| 43 | + $methodGet->expects(self::once())->method('getName')->willReturn('get'); |
| 44 | + |
| 45 | + $methodFoo = $this->createMock(MethodReflection::class); |
| 46 | + $methodFoo->expects(self::once())->method('getName')->willReturn('foo'); |
| 47 | + |
| 48 | + $extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml')); |
| 49 | + self::assertTrue($extension->isMethodSupported($methodGet)); |
| 50 | + self::assertFalse($extension->isMethodSupported($methodFoo)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider getTypeFromMethodCallProvider |
| 55 | + */ |
| 56 | + public function testGetTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Type $expectedType) |
| 57 | + { |
| 58 | + $extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml')); |
| 59 | + $type = $extension->getTypeFromMethodCall( |
| 60 | + $methodReflection, |
| 61 | + $methodCall, |
| 62 | + $this->createMock(Scope::class) |
| 63 | + ); |
| 64 | + self::assertEquals($expectedType, $type); |
| 65 | + } |
| 66 | + |
| 67 | + public function getTypeFromMethodCallProvider(): array |
| 68 | + { |
| 69 | + $notFoundType = $this->createMock(Type::class); |
| 70 | + |
| 71 | + $methodReflectionNotFound = $this->createMock(MethodReflection::class); |
| 72 | + $methodReflectionNotFound->expects(self::once())->method('getReturnType')->willReturn($notFoundType); |
| 73 | + |
| 74 | + return [ |
| 75 | + 'found' => [ |
| 76 | + $this->createMock(MethodReflection::class), |
| 77 | + new MethodCall($this->createMock(Expr::class), '', [new Arg(new String_('withClass'))]), |
| 78 | + new ObjectType('Foo'), |
| 79 | + ], |
| 80 | + 'notFound' => [ |
| 81 | + $methodReflectionNotFound, |
| 82 | + new MethodCall($this->createMock(Expr::class), ''), |
| 83 | + $notFoundType, |
| 84 | + ], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments