Skip to content

Commit 18706de

Browse files
staabmondrejmirtes
andauthored
Indicate file on TypeInferenceTestCase validation errors
Co-authored-by: Ondrej Mirtes <[email protected]>
1 parent 146d3ba commit 18706de

File tree

5 files changed

+105
-16
lines changed

5 files changed

+105
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\File;
4+
5+
use function str_starts_with;
6+
use function strlen;
7+
use function substr;
8+
9+
class SystemAgnosticSimpleRelativePathHelper implements RelativePathHelper
10+
{
11+
12+
public function __construct(private FileHelper $fileHelper)
13+
{
14+
}
15+
16+
public function getRelativePath(string $filename): string
17+
{
18+
$cwd = $this->fileHelper->getWorkingDirectory();
19+
if ($cwd !== '' && str_starts_with($filename, $cwd)) {
20+
return substr($filename, strlen($cwd) + 1);
21+
}
22+
23+
return $filename;
24+
}
25+
26+
}

src/Testing/TypeInferenceTestCase.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
1313
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
1414
use PHPStan\File\FileHelper;
15+
use PHPStan\File\SystemAgnosticSimpleRelativePathHelper;
1516
use PHPStan\Php\PhpVersion;
1617
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
1718
use PHPStan\PhpDoc\StubPhpDocProvider;
@@ -143,8 +144,14 @@ public function assertFileAsserts(
143144
*/
144145
public static function gatherAssertTypes(string $file): array
145146
{
147+
$fileHelper = self::getContainer()->getByType(FileHelper::class);
148+
149+
$relativePathHelper = new SystemAgnosticSimpleRelativePathHelper($fileHelper);
150+
151+
$file = $fileHelper->normalizePath($file);
152+
146153
$asserts = [];
147-
self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file): void {
154+
self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file, $relativePathHelper): void {
148155
if (!$node instanceof Node\Expr\FuncCall) {
149156
return;
150157
}
@@ -157,21 +164,32 @@ public static function gatherAssertTypes(string $file): array
157164
$functionName = $nameNode->toString();
158165
if (in_array(strtolower($functionName), ['asserttype', 'assertnativetype', 'assertvariablecertainty'], true)) {
159166
self::fail(sprintf(
160-
'Missing use statement for %s() on line %d.',
167+
'Missing use statement for %s() in %s on line %d.',
161168
$functionName,
169+
$relativePathHelper->getRelativePath($file),
162170
$node->getStartLine(),
163171
));
164172
} elseif ($functionName === 'PHPStan\\Testing\\assertType') {
165173
$expectedType = $scope->getType($node->getArgs()[0]->value);
166174
if (!$expectedType instanceof ConstantScalarType) {
167-
self::fail(sprintf('Expected type must be a literal string, %s given on line %d.', $expectedType->describe(VerbosityLevel::precise()), $node->getLine()));
175+
self::fail(sprintf(
176+
'Expected type must be a literal string, %s given in %s on line %d.',
177+
$expectedType->describe(VerbosityLevel::precise()),
178+
$relativePathHelper->getRelativePath($file),
179+
$node->getLine(),
180+
));
168181
}
169182
$actualType = $scope->getType($node->getArgs()[1]->value);
170183
$assert = ['type', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $node->getStartLine()];
171184
} elseif ($functionName === 'PHPStan\\Testing\\assertNativeType') {
172185
$expectedType = $scope->getType($node->getArgs()[0]->value);
173186
if (!$expectedType instanceof ConstantScalarType) {
174-
self::fail(sprintf('Expected type must be a literal string, %s given on line %d.', $expectedType->describe(VerbosityLevel::precise()), $node->getLine()));
187+
self::fail(sprintf(
188+
'Expected type must be a literal string, %s given in %s on line %d.',
189+
$expectedType->describe(VerbosityLevel::precise()),
190+
$relativePathHelper->getRelativePath($file),
191+
$node->getLine(),
192+
));
175193
}
176194

177195
$actualType = $scope->getNativeType($node->getArgs()[1]->value);
@@ -226,17 +244,19 @@ public static function gatherAssertTypes(string $file): array
226244
}
227245

228246
self::fail(sprintf(
229-
'Function %s imported with wrong namespace %s called on line %d.',
247+
'Function %s imported with wrong namespace %s called in %s on line %d.',
230248
$correctFunction,
231249
$functionName,
250+
$relativePathHelper->getRelativePath($file),
232251
$node->getStartLine(),
233252
));
234253
}
235254

236255
if (count($node->getArgs()) !== 2) {
237256
self::fail(sprintf(
238-
'ERROR: Wrong %s() call on line %d.',
257+
'ERROR: Wrong %s() call in %s on line %d.',
239258
$functionName,
259+
$relativePathHelper->getRelativePath($file),
240260
$node->getStartLine(),
241261
));
242262
}

tests/PHPStan/Analyser/PathConstantsTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
namespace PHPStan\Analyser;
44

55
use PHPStan\Testing\TypeInferenceTestCase;
6+
use const DIRECTORY_SEPARATOR;
67

78
class PathConstantsTest extends TypeInferenceTestCase
89
{
910

1011
public function dataFileAsserts(): iterable
1112
{
12-
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathConstants.php');
13+
if (DIRECTORY_SEPARATOR === '\\') {
14+
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathConstants-win.php');
15+
} else {
16+
yield from $this->gatherAssertTypes(__DIR__ . '/data/pathConstants.php');
17+
}
1318
}
1419

1520
/**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace PathConstantsTestWin;
4+
5+
\PHPStan\Testing\assertType('\'Analyser\\\\data\'', substr(__DIR__, -13));
6+
\PHPStan\Testing\assertType('\'pathConstants-win.php\'', substr(__FILE__, -21));

tests/PHPStan/Testing/TypeInferenceTestCaseTest.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,80 @@
22

33
namespace PHPStan\Testing;
44

5+
use PHPStan\File\FileHelper;
56
use PHPUnit\Framework\AssertionFailedError;
7+
use function sprintf;
68

79
final class TypeInferenceTestCaseTest extends TypeInferenceTestCase
810
{
911

1012
public static function dataFileAssertionFailedErrors(): iterable
1113
{
14+
/** @var FileHelper $fileHelper */
15+
$fileHelper = self::getContainer()->getByType(FileHelper::class);
16+
1217
yield [
1318
__DIR__ . '/data/assert-certainty-missing-namespace.php',
14-
'Missing use statement for assertVariableCertainty() on line 8.',
19+
sprintf(
20+
'Missing use statement for assertVariableCertainty() in %s on line 8.',
21+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-certainty-missing-namespace.php'),
22+
),
1523
];
1624
yield [
1725
__DIR__ . '/data/assert-native-type-missing-namespace.php',
18-
'Missing use statement for assertNativeType() on line 6.',
26+
sprintf(
27+
'Missing use statement for assertNativeType() in %s on line 6.',
28+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-native-type-missing-namespace.php'),
29+
),
1930
];
2031
yield [
2132
__DIR__ . '/data/assert-type-missing-namespace.php',
22-
'Missing use statement for assertType() on line 6.',
33+
sprintf(
34+
'Missing use statement for assertType() in %s on line 6.',
35+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-missing-namespace.php'),
36+
),
2337
];
2438
yield [
2539
__DIR__ . '/data/assert-certainty-wrong-namespace.php',
26-
'Function PHPStan\Testing\assertVariableCertainty imported with wrong namespace SomeWrong\Namespace\assertVariableCertainty called on line 9.',
40+
sprintf(
41+
'Function PHPStan\Testing\assertVariableCertainty imported with wrong namespace SomeWrong\Namespace\assertVariableCertainty called in %s on line 9.',
42+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-certainty-wrong-namespace.php'),
43+
),
2744
];
2845
yield [
2946
__DIR__ . '/data/assert-native-type-wrong-namespace.php',
30-
'Function PHPStan\Testing\assertNativeType imported with wrong namespace SomeWrong\Namespace\assertNativeType called on line 8.',
47+
sprintf(
48+
'Function PHPStan\Testing\assertNativeType imported with wrong namespace SomeWrong\Namespace\assertNativeType called in %s on line 8.',
49+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-native-type-wrong-namespace.php'),
50+
),
3151
];
3252
yield [
3353
__DIR__ . '/data/assert-type-wrong-namespace.php',
34-
'Function PHPStan\Testing\assertType imported with wrong namespace SomeWrong\Namespace\assertType called on line 8.',
54+
sprintf(
55+
'Function PHPStan\Testing\assertType imported with wrong namespace SomeWrong\Namespace\assertType called in %s on line 8.',
56+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-wrong-namespace.php'),
57+
),
3558
];
3659
yield [
3760
__DIR__ . '/data/assert-certainty-case-insensitive.php',
38-
'Missing use statement for assertvariablecertainty() on line 8.',
61+
sprintf(
62+
'Missing use statement for assertvariablecertainty() in %s on line 8.',
63+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-certainty-case-insensitive.php'),
64+
),
3965
];
4066
yield [
4167
__DIR__ . '/data/assert-native-type-case-insensitive.php',
42-
'Missing use statement for assertNATIVEType() on line 6.',
68+
sprintf(
69+
'Missing use statement for assertNATIVEType() in %s on line 6.',
70+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-native-type-case-insensitive.php'),
71+
),
4372
];
4473
yield [
4574
__DIR__ . '/data/assert-type-case-insensitive.php',
46-
'Missing use statement for assertTYPe() on line 6.',
75+
sprintf(
76+
'Missing use statement for assertTYPe() in %s on line 6.',
77+
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-case-insensitive.php'),
78+
),
4779
];
4880
}
4981

0 commit comments

Comments
 (0)