|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPStan\Drupal; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PhpParser\Node\Scalar\String_; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Reflection\ParametersAcceptor; |
| 12 | +use PHPStan\ShouldNotHappenException; |
| 13 | +use PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +final class EntityTypeManagerGetStorageDynamicReturnTypeExtensionTest extends TestCase |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::__construct |
| 22 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::getClass |
| 23 | + */ |
| 24 | + public function testGetClass() |
| 25 | + { |
| 26 | + $x = new EntityTypeManagerGetStorageDynamicReturnTypeExtension([]); |
| 27 | + self::assertEquals('Drupal\Core\Entity\EntityTypeManagerInterface', $x->getClass()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider getEntityStorageProvider |
| 32 | + * |
| 33 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::__construct |
| 34 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::getTypeFromMethodCall |
| 35 | + */ |
| 36 | + public function testGetTypeFromMethodCall($entityType, $storageClass) |
| 37 | + { |
| 38 | + $x = new EntityTypeManagerGetStorageDynamicReturnTypeExtension([ |
| 39 | + 'node' => 'Drupal\node\NodeStorage', |
| 40 | + 'search_api_index' => 'Drupal\search_api\Entity\SearchApiConfigEntityStorage', |
| 41 | + ]); |
| 42 | + |
| 43 | + $methodReflection = $this->prophesize(MethodReflection::class); |
| 44 | + $methodReflection->getName()->willReturn('getStorage'); |
| 45 | + |
| 46 | + $defaultObjectType = $this->prophesize(ObjectType::class); |
| 47 | + $defaultObjectType->getClassName()->willReturn('Drupal\Core\Entity\EntityStorageInterface'); |
| 48 | + $variantsParametersAcceptor = $this->prophesize(ParametersAcceptor::class); |
| 49 | + $variantsParametersAcceptor->getReturnType()->willReturn($defaultObjectType->reveal()); |
| 50 | + $methodReflection->getVariants()->willReturn([$variantsParametersAcceptor->reveal()]); |
| 51 | + |
| 52 | + if ($entityType === null) { |
| 53 | + $this->expectException(ShouldNotHappenException::class); |
| 54 | + $methodCall = new MethodCall( |
| 55 | + $this->prophesize(Expr::class)->reveal(), |
| 56 | + 'getStorage' |
| 57 | + ); |
| 58 | + } else { |
| 59 | + $methodCall = new MethodCall( |
| 60 | + $this->prophesize(Expr::class)->reveal(), |
| 61 | + 'getStorage', |
| 62 | + [new Arg($entityType)] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + $scope = $this->prophesize(Scope::class); |
| 67 | + |
| 68 | + $type = $x->getTypeFromMethodCall( |
| 69 | + $methodReflection->reveal(), |
| 70 | + $methodCall, |
| 71 | + $scope->reveal() |
| 72 | + ); |
| 73 | + self::assertInstanceOf(ObjectType::class, $type); |
| 74 | + assert($type instanceof ObjectType); |
| 75 | + self::assertEquals($storageClass, $type->getClassName()); |
| 76 | + } |
| 77 | + |
| 78 | + public function getEntityStorageProvider(): \Iterator |
| 79 | + { |
| 80 | + yield [new String_('node'), 'Drupal\node\NodeStorage']; |
| 81 | + yield [new String_('user'), 'Drupal\Core\Entity\EntityStorageInterface']; |
| 82 | + yield [new String_('search_api_index'), 'Drupal\search_api\Entity\SearchApiConfigEntityStorage']; |
| 83 | + yield [null, null]; |
| 84 | + yield [$this->prophesize(MethodCall::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface']; |
| 85 | + yield [$this->prophesize(Expr\StaticCall::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface']; |
| 86 | + yield [$this->prophesize(Expr\BinaryOp\Concat::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface']; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::__construct |
| 91 | + * @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::isMethodSupported |
| 92 | + */ |
| 93 | + public function testIsMethodSupported() |
| 94 | + { |
| 95 | + $x = new EntityTypeManagerGetStorageDynamicReturnTypeExtension([]); |
| 96 | + |
| 97 | + $valid = $this->prophesize(MethodReflection::class); |
| 98 | + $valid->getName()->willReturn('getStorage'); |
| 99 | + self::assertTrue($x->isMethodSupported($valid->reveal())); |
| 100 | + |
| 101 | + $invalid = $this->prophesize(MethodReflection::class); |
| 102 | + $invalid->getName()->willReturn('getAccessControlHandler'); |
| 103 | + self::assertFalse($x->isMethodSupported($invalid->reveal())); |
| 104 | + } |
| 105 | +} |
0 commit comments