|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Tests\PHPStan\Reflection\Doctrine; |
| 4 | + |
| 5 | +use PHPStan\Broker\Broker; |
| 6 | +use PHPStan\Reflection\ClassReflection; |
| 7 | +use PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Reflection\Php\PhpMethodReflection; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +final class DoctrineSelectableClassReflectionExtensionTest extends TestCase |
| 13 | +{ |
| 14 | + |
| 15 | + /** @var \PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension */ |
| 16 | + private $extension; |
| 17 | + |
| 18 | + protected function setUp() |
| 19 | + { |
| 20 | + $broker = $this->mockBroker(); |
| 21 | + |
| 22 | + $this->extension = new DoctrineSelectableClassReflectionExtension(); |
| 23 | + $this->extension->setBroker($broker); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @return mixed[] |
| 28 | + */ |
| 29 | + public function dataHasMethod(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + [\Doctrine\Common\Collections\Collection::class, 'matching', true], |
| 33 | + [\Doctrine\Common\Collections\Collection::class, 'foo', false], |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @dataProvider dataHasMethod |
| 39 | + * @param string $className |
| 40 | + * @param string $method |
| 41 | + * @param bool $expectedResult |
| 42 | + */ |
| 43 | + public function testHasMethod(string $className, string $method, bool $expectedResult) |
| 44 | + { |
| 45 | + $classReflection = $this->mockClassReflection(new \ReflectionClass($className)); |
| 46 | + $this->assertSame($expectedResult, $this->extension->hasMethod($classReflection, $method)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetMethod() |
| 50 | + { |
| 51 | + $classReflection = $this->mockClassReflection(new \ReflectionClass(\Doctrine\Common\Collections\Collection::class)); |
| 52 | + $methodReflection = $this->extension->getMethod($classReflection, 'matching'); |
| 53 | + $this->assertSame('matching', $methodReflection->getName()); |
| 54 | + } |
| 55 | + |
| 56 | + private function mockBroker(): Broker |
| 57 | + { |
| 58 | + $broker = $this->createMock(Broker::class); |
| 59 | + |
| 60 | + $broker->method('getClass')->willReturnCallback( |
| 61 | + function (string $className): ClassReflection { |
| 62 | + return $this->mockClassReflection(new \ReflectionClass($className)); |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + return $broker; |
| 67 | + } |
| 68 | + |
| 69 | + private function mockClassReflection(\ReflectionClass $reflectionClass): ClassReflection |
| 70 | + { |
| 71 | + $classReflection = $this->createMock(ClassReflection::class); |
| 72 | + $classReflection->method('getName')->willReturn($reflectionClass->getName()); |
| 73 | + $classReflection->method('getNativeMethod')->willReturnCallback( |
| 74 | + function (string $method) use ($reflectionClass): MethodReflection { |
| 75 | + return $this->mockMethodReflection($reflectionClass->getMethod($method)); |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + return $classReflection; |
| 80 | + } |
| 81 | + |
| 82 | + private function mockMethodReflection(\ReflectionMethod $method): PhpMethodReflection |
| 83 | + { |
| 84 | + $methodReflection = $this->createMock(PhpMethodReflection::class); |
| 85 | + $methodReflection->method('getName')->willReturn($method->getName()); |
| 86 | + return $methodReflection; |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments