|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Arrayy\tests; |
| 6 | + |
| 7 | +use Arrayy\PHPStan\MetaDynamicStaticMethodReturnTypeExtension; |
| 8 | +use Arrayy\tests\PHPStan\ArrayShapeUser; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
| 10 | +use PhpParser\Node\Expr\Variable; |
| 11 | +use PhpParser\Node\Name; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\Reflection\MethodReflection; |
| 14 | +use PHPStan\Type\ObjectShapeType; |
| 15 | +use PHPStan\Type\VerbosityLevel; |
| 16 | + |
| 17 | +require_once __DIR__ . '/PHPStan/ArrayShapeCity.php'; |
| 18 | +require_once __DIR__ . '/PHPStan/ArrayShapeUser.php'; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class MetaDynamicStaticMethodReturnTypeExtensionTest extends \PHPUnit\Framework\TestCase |
| 24 | +{ |
| 25 | + public function testGetClassTargetsArrayy(): void |
| 26 | + { |
| 27 | + $extension = new MetaDynamicStaticMethodReturnTypeExtension(); |
| 28 | + |
| 29 | + self::assertSame(\Arrayy\Arrayy::class, $extension->getClass()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testIsStaticMethodSupportedOnlyForMeta(): void |
| 33 | + { |
| 34 | + $extension = new MetaDynamicStaticMethodReturnTypeExtension(); |
| 35 | + |
| 36 | + $metaMethod = $this->createMock(MethodReflection::class); |
| 37 | + $metaMethod->method('getName')->willReturn('meta'); |
| 38 | + |
| 39 | + $createMethod = $this->createMock(MethodReflection::class); |
| 40 | + $createMethod->method('getName')->willReturn('create'); |
| 41 | + |
| 42 | + self::assertTrue($extension->isStaticMethodSupported($metaMethod)); |
| 43 | + self::assertFalse($extension->isStaticMethodSupported($createMethod)); |
| 44 | + } |
| 45 | + |
| 46 | + public function testReturnsNullWhenStaticCallClassIsNotANameNode(): void |
| 47 | + { |
| 48 | + $extension = new MetaDynamicStaticMethodReturnTypeExtension(); |
| 49 | + |
| 50 | + $method = $this->createMock(MethodReflection::class); |
| 51 | + $scope = $this->createMock(Scope::class); |
| 52 | + $scope->expects(self::never())->method('resolveName'); |
| 53 | + |
| 54 | + $type = $extension->getTypeFromStaticMethodCall( |
| 55 | + $method, |
| 56 | + new StaticCall(new Variable('className'), 'meta'), |
| 57 | + $scope |
| 58 | + ); |
| 59 | + |
| 60 | + self::assertNull($type); |
| 61 | + } |
| 62 | + |
| 63 | + public function testReturnsNullForNonArrayyClasses(): void |
| 64 | + { |
| 65 | + $extension = new MetaDynamicStaticMethodReturnTypeExtension(); |
| 66 | + |
| 67 | + $method = $this->createMock(MethodReflection::class); |
| 68 | + $scope = $this->createMock(Scope::class); |
| 69 | + $scope->expects(self::once()) |
| 70 | + ->method('resolveName') |
| 71 | + ->willReturn(\stdClass::class); |
| 72 | + |
| 73 | + $type = $extension->getTypeFromStaticMethodCall( |
| 74 | + $method, |
| 75 | + new StaticCall(new Name('stdClass'), 'meta'), |
| 76 | + $scope |
| 77 | + ); |
| 78 | + |
| 79 | + self::assertNull($type); |
| 80 | + } |
| 81 | + |
| 82 | + public function testBuildsAndCachesMetaShapeTypes(): void |
| 83 | + { |
| 84 | + $extension = new MetaDynamicStaticMethodReturnTypeExtension(); |
| 85 | + |
| 86 | + $method = $this->createMock(MethodReflection::class); |
| 87 | + $scope = $this->createMock(Scope::class); |
| 88 | + $scope->expects(self::exactly(2)) |
| 89 | + ->method('resolveName') |
| 90 | + ->willReturn(ArrayShapeUser::class); |
| 91 | + |
| 92 | + $call = new StaticCall(new Name('ArrayShapeUser'), 'meta'); |
| 93 | + |
| 94 | + $firstType = $extension->getTypeFromStaticMethodCall($method, $call, $scope); |
| 95 | + $secondType = $extension->getTypeFromStaticMethodCall($method, $call, $scope); |
| 96 | + |
| 97 | + self::assertInstanceOf(ObjectShapeType::class, $firstType); |
| 98 | + self::assertSame($firstType, $secondType); |
| 99 | + self::assertSame( |
| 100 | + "object{id: 'id', firstName: 'firstName', lastName: 'lastName', city: 'city'}", |
| 101 | + $firstType->describe(VerbosityLevel::precise()) |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments