Skip to content

Commit 0647fdf

Browse files
authored
Add a more helpful message when specs are failing due to a parse error in the spec (#282)
1 parent 550011a commit 0647fdf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
File renamed without changes.

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414

1515
namespace Humbug\PhpScoper\Scoper;
1616

17+
use function array_filter;
18+
use function array_slice;
19+
use function array_values;
1720
use Error;
21+
use function explode;
1822
use Generator;
1923
use Humbug\PhpScoper\PhpParser\TraverserFactory;
2024
use Humbug\PhpScoper\Reflector;
2125
use Humbug\PhpScoper\Scoper;
2226
use Humbug\PhpScoper\Whitelist;
27+
use PhpParser\Error as PhpParserError;
2328
use PHPUnit\Framework\TestCase;
2429
use Roave\BetterReflection\BetterReflection;
2530
use Roave\BetterReflection\Reflector\ClassReflector;
2631
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
2732
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
2833
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
34+
use function strpos;
2935
use Symfony\Component\Finder\Finder;
3036
use Symfony\Component\Finder\SplFileInfo;
3137
use Throwable;
@@ -112,6 +118,34 @@ public function test_can_scope_valid_files(
112118
$this->assertTrue(true);
113119

114120
return;
121+
} catch (PhpParserError $error) {
122+
if (0 !== strpos($error->getMessage(), 'Syntax error,')) {
123+
throw new Error(
124+
sprintf(
125+
'Could not parse the spec %s: %s',
126+
$spec,
127+
$error->getMessage()
128+
),
129+
0,
130+
$error
131+
);
132+
}
133+
134+
$lines = array_values(array_filter(explode("\n", $contents)));
135+
136+
$startLine = $error->getAttributes()['startLine'] - 1;
137+
$endLine = $error->getAttributes()['endLine'] + 1;
138+
139+
$this->fail(
140+
sprintf(
141+
'Unexpected parse error found in the following lines: %s%s',
142+
"\n\n> ",
143+
implode(
144+
"\n> ",
145+
array_slice($lines, $startLine, $endLine - $startLine + 1)
146+
)
147+
)
148+
);
115149
} catch (Throwable $throwable) {
116150
throw new Error(
117151
sprintf(

0 commit comments

Comments
 (0)