Skip to content

Commit 8b91cfa

Browse files
authored
Merge pull request #107 from mglaman/test-level-4
Test at level 4
2 parents e554a49 + 5c3def0 commit 8b91cfa

File tree

8 files changed

+92
-10
lines changed

8 files changed

+92
-10
lines changed

tests/fixtures/drupal/modules/phpstan_fixtures/src/DeprecatedGlobalConstants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class DeprecatedGlobalConstants {
66
public function test() {
77
$date = new \DateTime();
88
$date->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
9-
$date->format(DATETIME_DATE_STORAGE_FORMAT);
9+
$formatted = $date->format(DATETIME_DATE_STORAGE_FORMAT);
1010
}
1111
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Drupal\phpstan_fixtures\Entity;
4+
5+
use Drupal\Core\Entity\ContentEntityBase;
6+
use Drupal\Core\Entity\EntityTypeInterface;
7+
use Drupal\Core\Field\BaseFieldDefinition;
8+
use Drupal\user\UserInterface;
9+
10+
final class ReflectionEntityTest extends ContentEntityBase {
11+
12+
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
13+
{
14+
$fields = parent::baseFieldDefinitions($entity_type);
15+
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
16+
->setLabel(t('User ID'))
17+
->setDescription(t('The ID of the associated user.'))
18+
->setSetting('target_type', 'user')
19+
->setSetting('handler', 'default')
20+
// Default EntityTest entities to have the root user as the owner, to
21+
// simplify testing.
22+
->setDefaultValue([0 => ['target_id' => 1]])
23+
->setTranslatable(TRUE)
24+
->setDefaultValueCallback([static::class, 'getDefaultEntityOwner'])
25+
->setDisplayOptions('form', [
26+
'type' => 'entity_reference_autocomplete',
27+
'weight' => -1,
28+
'settings' => [
29+
'match_operator' => 'CONTAINS',
30+
'size' => '60',
31+
'placeholder' => '',
32+
],
33+
]);
34+
return $fields;
35+
}
36+
37+
public function getOwnerId(): int {
38+
return $this->get('user_id')->target_id;
39+
}
40+
41+
public function setOwnerId($uid): ReflectionEntityTest {
42+
$this->set('user_id', $uid);
43+
44+
return $this;
45+
}
46+
47+
public function getOwner(): UserInterface {
48+
return $this->get('user_id')->entity;
49+
}
50+
51+
public function setOwner(UserInterface $account): ReflectionEntityTest {
52+
$this->set('user_id', $account);
53+
54+
return $this;
55+
}
56+
57+
public static function getDefaultEntityOwner(): int {
58+
return \Drupal::currentUser()->id();
59+
}
60+
61+
}

tests/src/AnalyzerTestBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function runAnalyze(string $path) {
1616
$containerFactory = new ContainerFactory($rootDir);
1717

1818
$additionalConfigFiles = [
19-
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 2),
19+
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 4),
2020
__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon',
2121
];
2222
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, []);

tests/src/BootstrapTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function doDrupalBootstrap()
4848
$tmpDir = sys_get_temp_dir() . '/' . time() . 'phpstan';
4949
$containerFactory = new ContainerFactory($rootDir);
5050
$additionalConfigFiles = [
51-
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 2),
51+
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 4),
5252
__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon',
5353
];
5454
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, []);

tests/src/DeprecationRulesTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DeprecationRulesTest extends AnalyzerTestBase
1111
public function testDeprecationRules(string $path, int $count, array $errorMessages)
1212
{
1313
$errors = $this->runAnalyze($path);
14-
$this->assertCount($count, $errors);
14+
$this->assertCount($count, $errors, var_export($errors, true));
1515
foreach ($errors as $key => $error) {
1616
$this->assertEquals($errorMessages[$key], $error->getMessage());
1717
}
@@ -32,10 +32,11 @@ public function dataDeprecatedSamples(): \Generator
3232
];
3333
yield [
3434
__DIR__ . '/../fixtures/drupal/core/lib/Drupal/Core/Entity/EntityManager.php',
35-
2,
35+
3,
3636
[
3737
'Class Drupal\Core\Entity\EntityManager implements deprecated interface Drupal\Core\Entity\EntityManagerInterface:
3838
in drupal:8.0.0 and is removed from drupal:9.0.0.',
39+
'Method Drupal\\Core\\Entity\\EntityManager::setFieldMap() should return $this(Drupal\\Core\\Entity\\EntityManager) but returns Drupal\\Core\\Entity\\EntityFieldManager.',
3940
'Method Drupal\Core\Entity\EntityManager::clearDisplayModeInfo() should return $this(Drupal\Core\Entity\EntityManager) but return statement is missing.'
4041
]
4142
];

tests/src/DrupalIntegrationTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ public function testDrupalTestInChildSiteContant() {
3030

3131
public function testExtensionReportsError() {
3232
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module');
33-
$this->assertCount(1, $errors);
34-
$this->assertEquals('Function phpstan_fixtures_MissingReturnRule() should return string but return statement is missing.', $errors[0]->getMessage());
33+
$this->assertCount(2, $errors, var_export($errors, true));
34+
$error = array_shift($errors);
35+
$this->assertEquals('If condition is always false.', $error->getMessage());
36+
$error = array_shift($errors);
37+
$this->assertEquals('Function phpstan_fixtures_MissingReturnRule() should return string but return statement is missing.', $error->getMessage());
3538
}
3639

3740
public function testServiceMapping() {

tests/src/EntityTestClass.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,31 @@ final class EntityTestClass extends AnalyzerTestBase
77
/**
88
* @dataProvider dataEntitySamples
99
*/
10-
public function testEntityFields(string $path, int $count, array $errorMessages) {
10+
public function testEntityFields(string $path, int $count, array $errorMessages): void
11+
{
1112
$errors = $this->runAnalyze($path);
1213
$this->assertCount($count, $errors, print_r($errors, true));
1314
foreach ($errors as $key => $error) {
1415
$this->assertEquals($errorMessages[$key], $error->getMessage());
1516
}
1617
}
1718

19+
public function testEntityReferenceTargetIdPropertyReflection(): void
20+
{
21+
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ReflectionEntityTest.php');
22+
$this->assertCount(2, $errors, print_r($errors, true));
23+
$error = array_shift($errors);
24+
$this->assertEquals(
25+
'Method Drupal\phpstan_fixtures\Entity\ReflectionEntityTest::getOwnerId() should return int but returns string.',
26+
$error->getMessage()
27+
);
28+
$error = array_shift($errors);
29+
$this->assertEquals(
30+
'Method Drupal\phpstan_fixtures\Entity\ReflectionEntityTest::getOwner() should return Drupal\user\UserInterface but returns Drupal\Core\Entity\EntityInterface.',
31+
$error->getMessage()
32+
);
33+
}
34+
1835

1936
public function dataEntitySamples(): \Generator
2037
{

tests/src/ServiceProviderAutoloadingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function dataEntitySamples(): \Generator
2020
{
2121
yield [
2222
__DIR__ . '/../fixtures/drupal/modules/service_provider_test/src/ServiceProviderTestServiceProvider.php',
23-
0,
24-
[]
23+
1,
24+
['If condition is always true.']
2525
];
2626
}
2727
}

0 commit comments

Comments
 (0)