|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Weebly\PHPStan\Laravel; |
| 4 | + |
| 5 | +use PHPStan\Testing\TestCase; |
| 6 | +use PHPStan\Broker\Broker; |
| 7 | +use Weebly\PHPStan\Laravel\MethodReflectionFactory; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPStan\Reflection\Php\PhpMethodReflectionFactory; |
| 10 | +use PHPStan\Reflection\Php\PhpMethodReflection; |
| 11 | +use PHPStan\Type\FileTypeMapper; |
| 12 | +use Weebly\PHPStan\Laravel\FacadeMethodExtension; |
| 13 | +use Illuminate\Support\Facades\Facade; |
| 14 | + |
| 15 | +/** |
| 16 | + * @package Tests\Weebly\PHPStan\Laravel |
| 17 | + */ |
| 18 | +class FacadeMethodExtensionTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Broker |
| 22 | + */ |
| 23 | + private $broker; |
| 24 | + |
| 25 | + public function testHasMethod() |
| 26 | + { |
| 27 | + // Native accessor method |
| 28 | + $this->assertTrue($this->hasMethod(TestFacade::class, 'someMethod')); |
| 29 | + $this->assertFalse($this->hasMethod(TestFacade::class, 'fakeMethod')); |
| 30 | + // Method from accessor mixin |
| 31 | + $this->assertTrue($this->hasMethod(TestFacade::class, 'table')); |
| 32 | + $this->assertTrue($this->hasMethod(TestFacade::class, 'shouldUse')); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected function setUp() |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + $this->broker = $this->createBroker(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return MethodReflectionFactory |
| 46 | + */ |
| 47 | + private function makeMethodReflectionFactoryMock() |
| 48 | + { |
| 49 | + /** @var MockObject|PhpMethodReflectionFactory $phpMethodReflectionFactory */ |
| 50 | + $phpMethodReflectionFactory = $this |
| 51 | + ->getMockBuilder(PhpMethodReflectionFactory::class) |
| 52 | + ->getMockForAbstractClass(); |
| 53 | + $methodReflectionMock = $this |
| 54 | + ->getMockBuilder(PhpMethodReflection::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + $phpMethodReflectionFactory->method('create')->willReturn($methodReflectionMock); |
| 58 | + /** @var FileTypeMapper $fileTypeMapper */ |
| 59 | + $fileTypeMapper = $this->getContainer()->createInstance(FileTypeMapper::class); |
| 60 | + |
| 61 | + return new MethodReflectionFactory($phpMethodReflectionFactory, $fileTypeMapper); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Check existence of the method in given class |
| 66 | + * |
| 67 | + * @param string $className |
| 68 | + * @param string $methodName |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + private function hasMethod(string $className, string $methodName): bool |
| 72 | + { |
| 73 | + $extension = new FacadeMethodExtension($this->makeMethodReflectionFactoryMock()); |
| 74 | + $extension->setBroker($this->broker); |
| 75 | + |
| 76 | + return $extension->hasMethod($this->broker->getClass($className), $methodName); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @mixin \Illuminate\Database\Connection |
| 82 | + * @mixin \Illuminate\Auth\AuthManager |
| 83 | + */ |
| 84 | +class TestFacadeAccessor { |
| 85 | + function someMethod() { |
| 86 | + return true; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class TestFacade extends Facade { |
| 91 | + /** |
| 92 | + * @inheritdoc |
| 93 | + */ |
| 94 | + protected static function getFacadeAccessor() |
| 95 | + { |
| 96 | + return new TestFacadeAccessor(); |
| 97 | + } |
| 98 | +} |
0 commit comments