|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Tests\PHPStan\Type\Doctrine; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension; |
| 10 | +use PHPStan\Type\ObjectType; |
| 11 | +use PHPStan\Type\Type; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +final class DoctrineSelectableDynamicReturnTypeExtensionTest extends TestCase |
| 15 | +{ |
| 16 | + |
| 17 | + /** @var \PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension */ |
| 18 | + private $extension; |
| 19 | + |
| 20 | + protected function setUp() |
| 21 | + { |
| 22 | + $this->extension = new DoctrineSelectableDynamicReturnTypeExtension(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @return mixed[] |
| 27 | + */ |
| 28 | + public function dataIsMethodSupported(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + ['matching', true], |
| 32 | + ['filter', false], |
| 33 | + ['foo', false], |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @dataProvider dataIsMethodSupported |
| 39 | + * @param string $method |
| 40 | + * @param bool $expectedResult |
| 41 | + */ |
| 42 | + public function testIsMethodSupported(string $method, bool $expectedResult) |
| 43 | + { |
| 44 | + $methodReflection = $this->createMock(MethodReflection::class); |
| 45 | + $methodReflection->method('getName')->willReturn($method); |
| 46 | + $this->assertSame($expectedResult, $this->extension->isMethodSupported($methodReflection)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetTypeFromMethodCall() |
| 50 | + { |
| 51 | + $methodReflection = $this->createMock(MethodReflection::class); |
| 52 | + |
| 53 | + $scope = $this->createMock(Scope::class); |
| 54 | + $scope->method('getType')->will( |
| 55 | + self::returnCallback( |
| 56 | + function (\PhpParser\Node\Expr $node): Type { |
| 57 | + return new ObjectType($node->getType()); |
| 58 | + } |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + $var = $this->createMock(Expr::class); |
| 63 | + $var->method('getType')->willReturn(\Doctrine\Common\Collections\Collection::class); |
| 64 | + $methodCall = $this->createMock(MethodCall::class); |
| 65 | + $methodCall->var = $var; |
| 66 | + |
| 67 | + $resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope); |
| 68 | + |
| 69 | + $this->assertInstanceOf(ObjectType::class, $resultType); |
| 70 | + $this->assertSame(\Doctrine\Common\Collections\Collection::class, $resultType->describe()); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments