|
| 1 | +<?php declare(strict_types = 1); |
| 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 Illuminate\Database\Eloquent\Model; |
| 13 | +use Weebly\PHPStan\Laravel\BuilderMethodExtension; |
| 14 | +use stdClass; |
| 15 | +use Illuminate\Database\Eloquent\Builder; |
| 16 | +use Weebly\PHPStan\Laravel\Utils\AnnotationsHelper; |
| 17 | +use PHPStan\Broker\ClassNotFoundException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @package Tests\Weebly\PHPStan\Laravel |
| 21 | + */ |
| 22 | +class BuilderMethodExtensionTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Broker |
| 26 | + */ |
| 27 | + private $broker; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private $childOfModelClassName; |
| 33 | + |
| 34 | + public function testHasMethodInSubclassOfModel() |
| 35 | + { |
| 36 | + try { |
| 37 | + $this->assertFalse($this->hasMethod(stdClass::class, 'find')); |
| 38 | + $this->assertTrue($this->hasMethod($this->childOfModelClassName, 'find')); |
| 39 | + $this->assertFalse($this->hasMethod(stdClass::class, 'select')); |
| 40 | + $this->assertTrue($this->hasMethod($this->childOfModelClassName, 'select')); |
| 41 | + } catch (ClassNotFoundException $e) { |
| 42 | + $this->markTestIncomplete($e->getMessage()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function testHasMethodInClassWithMixinAnnotation() |
| 47 | + { |
| 48 | + try { |
| 49 | + $this->assertFalse($this->hasMethod(stdClass::class, 'find')); |
| 50 | + $this->assertTrue($this->hasMethod(stdClass::class, 'find', true)); |
| 51 | + $this->assertFalse($this->hasMethod(stdClass::class, 'select')); |
| 52 | + $this->assertTrue($this->hasMethod(stdClass::class, 'select', true)); |
| 53 | + } catch (ClassNotFoundException $e) { |
| 54 | + $this->markTestIncomplete($e->getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function testHasMethodInBuilder() |
| 59 | + { |
| 60 | + try { |
| 61 | + $this->assertFalse($this->hasMethod(stdClass::class, 'find')); |
| 62 | + $this->assertTrue($this->hasMethod(Builder::class, 'find')); |
| 63 | + $this->assertFalse($this->hasMethod(stdClass::class, 'select')); |
| 64 | + $this->assertTrue($this->hasMethod(Builder::class, 'select')); |
| 65 | + } catch (ClassNotFoundException $e) { |
| 66 | + $this->markTestIncomplete($e->getMessage()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritdoc |
| 72 | + */ |
| 73 | + protected function setUp() |
| 74 | + { |
| 75 | + parent::setUp(); |
| 76 | + $this->broker = $this->createBroker(); |
| 77 | + $this->childOfModelClassName = get_class(new class() extends Model {}); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return MethodReflectionFactory |
| 82 | + */ |
| 83 | + private function makeMethodReflectionFactoryMock() |
| 84 | + { |
| 85 | + /** @var MockObject|PhpMethodReflectionFactory $phpMethodReflectionFactory */ |
| 86 | + $phpMethodReflectionFactory = $this |
| 87 | + ->getMockBuilder(PhpMethodReflectionFactory::class) |
| 88 | + ->getMockForAbstractClass(); |
| 89 | + $methodReflectionMock = $this |
| 90 | + ->getMockBuilder(PhpMethodReflection::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
| 93 | + $phpMethodReflectionFactory->method('create')->willReturn($methodReflectionMock); |
| 94 | + /** @var FileTypeMapper $fileTypeMapper */ |
| 95 | + $fileTypeMapper = $this->getContainer()->createInstance(FileTypeMapper::class); |
| 96 | + |
| 97 | + return new MethodReflectionFactory($phpMethodReflectionFactory, $fileTypeMapper); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Check existence of the method in given class |
| 102 | + * |
| 103 | + * @param string $className |
| 104 | + * @param string $methodName |
| 105 | + * @param bool $addBuilderMixin |
| 106 | + * @return bool |
| 107 | + * @throws ClassNotFoundException |
| 108 | + */ |
| 109 | + private function hasMethod(string $className, string $methodName, bool $addBuilderMixin = false): bool |
| 110 | + { |
| 111 | + $extension = new BuilderMethodExtension( |
| 112 | + $this->makeMethodReflectionFactoryMock(), |
| 113 | + $this->makeAnnotationsHelperMock($addBuilderMixin) |
| 114 | + ); |
| 115 | + $extension->setBroker($this->broker); |
| 116 | + |
| 117 | + return $extension->hasMethod($this->broker->getClass($className), $methodName); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param bool $withBuilder |
| 122 | + * @return AnnotationsHelper|MockObject |
| 123 | + */ |
| 124 | + private function makeAnnotationsHelperMock(bool $withBuilder = false) |
| 125 | + { |
| 126 | + $annotationsHelper = $this |
| 127 | + ->getMockBuilder(AnnotationsHelper::class) |
| 128 | + ->getMock(); |
| 129 | + $annotationsHelper->method('getMixins')->willReturn($withBuilder ? [Builder::class] : []); |
| 130 | + |
| 131 | + return $annotationsHelper; |
| 132 | + } |
| 133 | +} |
0 commit comments