Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions tests/src/DrupalIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,15 @@ public function testDrupalTestInChildSiteContant() {
public function testExtensionReportsError() {
$is_d9 = version_compare('9.0.0', \Drupal::VERSION) !== 1;
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module');
// @todo this only broke on D9.
self::assertCount($is_d9 ? 4 : 3, $errors->getErrors(), var_export($errors, true));
self::assertCount(3, $errors->getErrors(), var_export($errors, true));
self::assertCount(0, $errors->getInternalErrors(), var_export($errors, true));

$errors = $errors->getErrors();
$error = array_shift($errors);
self::assertEquals('If condition is always false.', $error->getMessage());
$error = array_shift($errors);
self::assertEquals('Function phpstan_fixtures_MissingReturnRule() should return string but return statement is missing.', $error->getMessage());
if ($is_d9) {
$error = array_shift($errors);
self::assertEquals('Binary operation "." between SplString and \'/core/includes…\' results in an error.', $error->getMessage());
}
$error = array_shift($errors);

self::assertNotFalse(strpos($error->getMessage(), 'phpstan_fixtures/phpstan_fixtures.fetch.inc could not be loaded from Drupal\\Core\\Extension\\ModuleHandlerInterface::loadInclude'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@
use PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophet;

final class EntityTypeManagerGetStorageDynamicReturnTypeExtensionTest extends TestCase
{
/**
* @var Prophet
*
* @internal
*/
private $prophet;

protected function setUp(): void
{
parent::setUp();
// @note we do not use phpspec/prophecy-phpunit due to conflicts with Drupal 8 PHPUnit.
$this->prophet = new Prophet;
}

/**
* @covers \PHPStan\Type\EntityTypeManagerGetStorageDynamicReturnTypeExtension::__construct
Expand All @@ -35,35 +49,40 @@ public function testGetClass()
*/
public function testGetTypeFromMethodCall($entityType, $storageClass)
{
// If we were passed a string, assume it is a class name to be mocked.
if (is_string($entityType)) {
$entityType = $this->prophet->prophesize($entityType)->reveal();
}

$x = new EntityTypeManagerGetStorageDynamicReturnTypeExtension([
'node' => 'Drupal\node\NodeStorage',
'search_api_index' => 'Drupal\search_api\Entity\SearchApiConfigEntityStorage',
]);

$methodReflection = $this->prophesize(MethodReflection::class);
$methodReflection = $this->prophet->prophesize(MethodReflection::class);
$methodReflection->getName()->willReturn('getStorage');

$defaultObjectType = $this->prophesize(ObjectType::class);
$defaultObjectType = $this->prophet->prophesize(ObjectType::class);
$defaultObjectType->getClassName()->willReturn('Drupal\Core\Entity\EntityStorageInterface');
$variantsParametersAcceptor = $this->prophesize(ParametersAcceptor::class);
$variantsParametersAcceptor = $this->prophet->prophesize(ParametersAcceptor::class);
$variantsParametersAcceptor->getReturnType()->willReturn($defaultObjectType->reveal());
$methodReflection->getVariants()->willReturn([$variantsParametersAcceptor->reveal()]);

if ($entityType === null) {
$this->expectException(ShouldNotHappenException::class);
$methodCall = new MethodCall(
$this->prophesize(Expr::class)->reveal(),
$this->prophet->prophesize(Expr::class)->reveal(),
'getStorage'
);
} else {
$methodCall = new MethodCall(
$this->prophesize(Expr::class)->reveal(),
$this->prophet->prophesize(Expr::class)->reveal(),
'getStorage',
[new Arg($entityType)]
);
}

$scope = $this->prophesize(Scope::class);
$scope = $this->prophet->prophesize(Scope::class);

$type = $x->getTypeFromMethodCall(
$methodReflection->reveal(),
Expand All @@ -81,9 +100,9 @@ public function getEntityStorageProvider(): \Iterator
yield [new String_('user'), 'Drupal\Core\Entity\EntityStorageInterface'];
yield [new String_('search_api_index'), 'Drupal\search_api\Entity\SearchApiConfigEntityStorage'];
yield [null, null];
yield [$this->prophesize(MethodCall::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface'];
yield [$this->prophesize(Expr\StaticCall::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface'];
yield [$this->prophesize(Expr\BinaryOp\Concat::class)->reveal(), 'Drupal\Core\Entity\EntityStorageInterface'];
yield [MethodCall::class, 'Drupal\Core\Entity\EntityStorageInterface'];
yield [Expr\StaticCall::class, 'Drupal\Core\Entity\EntityStorageInterface'];
yield [Expr\BinaryOp\Concat::class, 'Drupal\Core\Entity\EntityStorageInterface'];
}

/**
Expand All @@ -94,11 +113,11 @@ public function testIsMethodSupported()
{
$x = new EntityTypeManagerGetStorageDynamicReturnTypeExtension([]);

$valid = $this->prophesize(MethodReflection::class);
$valid = $this->prophet->prophesize(MethodReflection::class);
$valid->getName()->willReturn('getStorage');
self::assertTrue($x->isMethodSupported($valid->reveal()));

$invalid = $this->prophesize(MethodReflection::class);
$invalid = $this->prophet->prophesize(MethodReflection::class);
$invalid->getName()->willReturn('getAccessControlHandler');
self::assertFalse($x->isMethodSupported($invalid->reveal()));
}
Expand Down