Skip to content

Commit d051f2a

Browse files
Clean RuleTestCase (#132)
1 parent 6f05111 commit d051f2a

8 files changed

+30
-205
lines changed

tests/src/RuleTestCase.php

Lines changed: 6 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,26 @@
22

33
namespace Pepakriz\PHPStanExceptionRules;
44

5-
use InvalidArgumentException;
65
use LogicException;
76
use Nette\Utils\Strings;
8-
use PhpParser\PrettyPrinter\Standard;
9-
use PHPStan\Analyser\Analyser;
10-
use PHPStan\Analyser\Error;
11-
use PHPStan\Analyser\FileAnalyser;
12-
use PHPStan\Analyser\NodeScopeResolver;
13-
use PHPStan\Analyser\TypeSpecifier;
14-
use PHPStan\Broker\AnonymousClassNameHelper;
15-
use PHPStan\Cache\Cache;
16-
use PHPStan\Dependency\DependencyResolver;
17-
use PHPStan\File\FileHelper;
18-
use PHPStan\File\SimpleRelativePathHelper;
19-
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
20-
use PHPStan\PhpDoc\PhpDocNodeResolver;
21-
use PHPStan\PhpDoc\PhpDocStringResolver;
22-
use PHPStan\PhpDoc\TypeNodeResolverExtension;
237
use PHPStan\PhpDocParser\Lexer\Lexer;
248
use PHPStan\PhpDocParser\Parser\PhpDocParser;
25-
use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider;
26-
use PHPStan\Rules\Registry;
27-
use PHPStan\Rules\Rule;
28-
use PHPStan\Testing\TestCase;
29-
use PHPStan\Type\FileTypeMapper;
30-
use PHPStan\Type\MethodTypeSpecifyingExtension;
31-
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
32-
use function array_map;
9+
use PHPStan\Testing\RuleTestCase as BaseRuleTestCase;
3310
use function explode;
3411
use function file_get_contents;
35-
use function implode;
36-
use function sprintf;
3712
use function trim;
3813

3914
/**
4015
* @template TRule of \PHPStan\Rules\Rule
16+
* @extends BaseRuleTestCase<TRule>
4117
*/
42-
abstract class RuleTestCase extends TestCase
18+
abstract class RuleTestCase extends BaseRuleTestCase
4319
{
4420

45-
/**
46-
* @var Analyser
47-
*/
48-
private $analyser;
49-
50-
/**
51-
* @phpstan-return TRule
52-
*/
53-
abstract protected function getRule(): Rule;
54-
55-
protected function getTypeSpecifier(): TypeSpecifier
21+
protected function analyseFile(string $file): void
5622
{
57-
return $this->createTypeSpecifier(
58-
new Standard(),
59-
$this->createBroker(),
60-
$this->getMethodTypeSpecifyingExtensions(),
61-
$this->getStaticMethodTypeSpecifyingExtensions()
62-
);
23+
$file = $this->getFileHelper()->normalizePath($file);
24+
$this->analyse([$file], $this->parseExpectedErrors($file));
6325
}
6426

6527
protected function createThrowsAnnotationReader(): ThrowsAnnotationReader
@@ -71,123 +33,6 @@ protected function createThrowsAnnotationReader(): ThrowsAnnotationReader
7133
);
7234
}
7335

74-
private function getAnalyser(): Analyser
75-
{
76-
if ($this->analyser === null) {
77-
$registry = new Registry([$this->getRule()]);
78-
$broker = $this->createBroker();
79-
$printer = new Standard();
80-
81-
$currentWorkingDirectory = $this->getCurrentWorkingDirectory();
82-
$fileHelper = new FileHelper($currentWorkingDirectory);
83-
$relativePathHelper = new SimpleRelativePathHelper($currentWorkingDirectory);
84-
$anonymousClassNameHelper = new AnonymousClassNameHelper($fileHelper, $relativePathHelper);
85-
86-
$typeSpecifier = $this->createTypeSpecifier(
87-
$printer,
88-
$broker,
89-
$this->getMethodTypeSpecifyingExtensions(),
90-
$this->getStaticMethodTypeSpecifyingExtensions()
91-
);
92-
93-
$fileTypeMapper = new FileTypeMapper(
94-
new DirectReflectionProviderProvider($broker),
95-
$this->getParser(),
96-
self::getContainer()->getByType(PhpDocStringResolver::class),
97-
self::getContainer()->getByType(PhpDocNodeResolver::class),
98-
$this->createMock(Cache::class),
99-
$anonymousClassNameHelper
100-
);
101-
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper);
102-
103-
$nodeScopeResolver = new NodeScopeResolver(
104-
$broker,
105-
self::getReflectors()[0],
106-
$this->getClassReflectionExtensionRegistryProvider(),
107-
$this->getParser(),
108-
$fileTypeMapper,
109-
$phpDocInheritanceResolver,
110-
$fileHelper,
111-
$typeSpecifier,
112-
$this->shouldPolluteScopeWithLoopInitialAssignments(),
113-
$this->shouldPolluteCatchScopeWithTryAssignments(),
114-
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
115-
[],
116-
[]
117-
);
118-
119-
$fileAnalyser = new FileAnalyser(
120-
$this->createScopeFactory($broker, $typeSpecifier),
121-
$nodeScopeResolver,
122-
$this->getParser(),
123-
new DependencyResolver($broker),
124-
$fileHelper,
125-
$this->shouldReportUnmatchedIgnoredErrors()
126-
);
127-
128-
$this->analyser = new Analyser($fileAnalyser, $registry, $nodeScopeResolver, 50);
129-
}
130-
131-
return $this->analyser;
132-
}
133-
134-
/**
135-
* @return MethodTypeSpecifyingExtension[]
136-
*/
137-
protected function getMethodTypeSpecifyingExtensions(): array
138-
{
139-
return [];
140-
}
141-
142-
/**
143-
* @return StaticMethodTypeSpecifyingExtension[]
144-
*/
145-
protected function getStaticMethodTypeSpecifyingExtensions(): array
146-
{
147-
return [];
148-
}
149-
150-
/**
151-
* @return TypeNodeResolverExtension[]
152-
*/
153-
protected function getTypeNodeResolverExtensions(): array
154-
{
155-
return [];
156-
}
157-
158-
public function analyse(string $file): void
159-
{
160-
$file = $this->getFileHelper()->normalizePath($file);
161-
$expectedErrors = $this->parseExpectedErrors($file);
162-
$actualErrors = $this->getAnalyser()->analyse([$file])->getUnorderedErrors();
163-
164-
$strictlyTypedSprintf = static function (int $line, string $message): string {
165-
return sprintf('%02d: %s', $line, $message);
166-
};
167-
168-
$expectedErrors = array_map(
169-
static function (array $error) use ($strictlyTypedSprintf): string {
170-
if (!isset($error[0])) {
171-
throw new InvalidArgumentException('Missing expected error message.');
172-
}
173-
if (!isset($error[1])) {
174-
throw new InvalidArgumentException('Missing expected file line.');
175-
}
176-
return $strictlyTypedSprintf($error[1], $error[0]);
177-
},
178-
$expectedErrors
179-
);
180-
181-
$actualErrors = array_map(
182-
static function (Error $error): string {
183-
return sprintf('%02d: %s', $error->getLine(), $error->getMessage());
184-
},
185-
$actualErrors
186-
);
187-
188-
self::assertSame(implode("\n", $expectedErrors), implode("\n", $actualErrors));
189-
}
190-
19136
/**
19237
* @return mixed[]
19338
*/
@@ -217,24 +62,4 @@ private function parseExpectedErrors(string $file): array
21762
return $expectedErrors;
21863
}
21964

220-
protected function shouldPolluteScopeWithLoopInitialAssignments(): bool
221-
{
222-
return false;
223-
}
224-
225-
protected function shouldPolluteCatchScopeWithTryAssignments(): bool
226-
{
227-
return false;
228-
}
229-
230-
protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool
231-
{
232-
return false;
233-
}
234-
235-
protected function shouldReportUnmatchedIgnoredErrors(): bool
236-
{
237-
return true;
238-
}
239-
24065
}

tests/src/Rules/Bug113Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function getRule(): Rule
5454

5555
public function testBasicThrows(): void
5656
{
57-
$this->analyse(__DIR__ . '/data/bug113.php');
57+
$this->analyseFile(__DIR__ . '/data/bug113.php');
5858
}
5959

6060
}

tests/src/Rules/DeadCatchUnionRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function getRule(): Rule
1818

1919
public function test(): void
2020
{
21-
$this->analyse(__DIR__ . '/data/dead-catch-union.php');
21+
$this->analyseFile(__DIR__ . '/data/dead-catch-union.php');
2222
}
2323

2424
}

tests/src/Rules/PhpInternalsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ protected function getRule(): Rule
6363

6464
public function testPhpInternalFunctions(): void
6565
{
66-
$this->analyse(__DIR__ . '/data/throws-php-internal-functions.php');
66+
$this->analyseFile(__DIR__ . '/data/throws-php-internal-functions.php');
6767
}
6868

6969
/**
7070
* @requires PHP 7.3
7171
*/
7272
public function testPhpInternalFunctionsPhp73(): void
7373
{
74-
$this->analyse(__DIR__ . '/data/throws-php-internal-functions-php7.3.php');
74+
$this->analyseFile(__DIR__ . '/data/throws-php-internal-functions-php7.3.php');
7575
}
7676

7777
public function testPhpInternalOperators(): void
7878
{
79-
$this->analyse(__DIR__ . '/data/throws-php-internal-operators.php');
79+
$this->analyseFile(__DIR__ . '/data/throws-php-internal-operators.php');
8080
}
8181

8282
}

tests/src/Rules/ThrowsPhpDocInheritanceRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ protected function getRule(): Rule
4242

4343
public function testInheritance(): void
4444
{
45-
$this->analyse(__DIR__ . '/data/throws-inheritance.php');
45+
$this->analyseFile(__DIR__ . '/data/throws-inheritance.php');
4646
}
4747

4848
public function testInheritanceWithInterfaces(): void
4949
{
50-
$this->analyse(__DIR__ . '/data/throws-inheritance-interfaces.php');
50+
$this->analyseFile(__DIR__ . '/data/throws-inheritance-interfaces.php');
5151
}
5252

5353
public function testInheritanceWithOverriding(): void
5454
{
55-
$this->analyse(__DIR__ . '/data/throws-inheritance-overriding.php');
55+
$this->analyseFile(__DIR__ . '/data/throws-inheritance-overriding.php');
5656
}
5757

5858
}

tests/src/Rules/ThrowsPhpDocRuleTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ protected function getRule(): Rule
9191

9292
public function testBasicThrows(): void
9393
{
94-
$this->analyse(__DIR__ . '/data/throws-annotations.php');
94+
$this->analyseFile(__DIR__ . '/data/throws-annotations.php');
9595
}
9696

9797
public function testTryCatch(): void
9898
{
99-
$this->analyse(__DIR__ . '/data/try-catch.php');
99+
$this->analyseFile(__DIR__ . '/data/try-catch.php');
100100
}
101101

102102
public function testUnusedThrows(): void
103103
{
104-
$this->analyse(__DIR__ . '/data/unused-throws.php');
104+
$this->analyseFile(__DIR__ . '/data/unused-throws.php');
105105
}
106106

107107
public function testUnusedCatches(): void
@@ -119,61 +119,61 @@ public function testUnusedCatches(): void
119119
],
120120
];
121121

122-
$this->analyse(__DIR__ . '/data/unused-catches.php');
122+
$this->analyseFile(__DIR__ . '/data/unused-catches.php');
123123
}
124124

125125
public function testAllUnusedCatches(): void
126126
{
127127
$this->reportUnusedCatchesOfUncheckedExceptions = true;
128-
$this->analyse(__DIR__ . '/data/unused-catches-all.php');
128+
$this->analyseFile(__DIR__ . '/data/unused-catches-all.php');
129129
}
130130

131131
public function testIterators(): void
132132
{
133-
$this->analyse(__DIR__ . '/data/iterators.php');
133+
$this->analyseFile(__DIR__ . '/data/iterators.php');
134134
}
135135

136136
public function testCountable(): void
137137
{
138-
$this->analyse(__DIR__ . '/data/countables.php');
138+
$this->analyseFile(__DIR__ . '/data/countables.php');
139139
}
140140

141141
public function testJsonSerializable(): void
142142
{
143-
$this->analyse(__DIR__ . '/data/json-serializable.php');
143+
$this->analyseFile(__DIR__ . '/data/json-serializable.php');
144144
}
145145

146146
public function testDynamicExtension(): void
147147
{
148-
$this->analyse(__DIR__ . '/data/dynamic-extension.php');
148+
$this->analyseFile(__DIR__ . '/data/dynamic-extension.php');
149149
}
150150

151151
public function testUnsupportedCatchCheckedAndUnchecked(): void
152152
{
153-
$this->analyse(__DIR__ . '/data/unsupported-catch.php');
153+
$this->analyseFile(__DIR__ . '/data/unsupported-catch.php');
154154
}
155155

156156
public function testAnonymClass(): void
157157
{
158-
$this->analyse(__DIR__ . '/data/throws-anonym-class.php');
158+
$this->analyseFile(__DIR__ . '/data/throws-anonym-class.php');
159159
}
160160

161161
public function testThrowsInGlobalScope(): void
162162
{
163163
$this->reportCheckedThrowsInGlobalScope = true;
164-
$this->analyse(__DIR__ . '/data/throws-in-global-scope.php');
164+
$this->analyseFile(__DIR__ . '/data/throws-in-global-scope.php');
165165
}
166166

167167
public function testMethodWhitelist(): void
168168
{
169169
$this->methodWhitelist = [TestCase::class => '/^test/'];
170-
$this->analyse(__DIR__ . '/data/method-whitelisting.php');
170+
$this->analyseFile(__DIR__ . '/data/method-whitelisting.php');
171171
}
172172

173173
public function testIntentionallyUnusedThrows(): void
174174
{
175175
$this->reportUnusedCheckedThrowsInSubtypes = false;
176-
$this->analyse(__DIR__ . '/data/intentionally-unused-throws.php');
176+
$this->analyseFile(__DIR__ . '/data/intentionally-unused-throws.php');
177177
}
178178

179179
}

tests/src/Rules/UnreachableCatchRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function getRule(): Rule
1818

1919
public function test(): void
2020
{
21-
$this->analyse(__DIR__ . '/data/unreachable-catches.php');
21+
$this->analyseFile(__DIR__ . '/data/unreachable-catches.php');
2222
}
2323

2424
}

tests/src/Rules/UselessThrowsPhpDocRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function getRule(): Rule
2121

2222
public function testBasicUselessThrows(): void
2323
{
24-
$this->analyse(__DIR__ . '/data/useless-throws.php');
24+
$this->analyseFile(__DIR__ . '/data/useless-throws.php');
2525
}
2626

2727
}

0 commit comments

Comments
 (0)