Fix - Pest helper syntax errors by sanitizing generated TestCase namespace segments#666
Open
tusharnain wants to merge 1 commit into
Open
Fix - Pest helper syntax errors by sanitizing generated TestCase namespace segments#666tusharnain wants to merge 1 commit into
tusharnain wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a syntax error bug in the dynamically generated Pest helper file (
storage/framework/testing/_pest.php).When test configuration files or packages reside in subdirectories or contain characters like hyphens (
-), dots (.), or start with digits, the extension generates invalid PHP namespace declarations. This causes IDE parser failures in tools like PHP Intelephense, PHP Tools, or PHPStan, breaking autocomplete for Pest's functional test helpers (beforeEach(),afterEach(),test(), andit()).The Problem & Cause
Pest test configurations are mapped to virtual
TestCaseclasses containing the bound traits. These are written inside_pest.phpto define the$thisclosure context for IDE parsing.In
src/features/pest.ts, thepestNamespacehelper converted relative file paths directly to namespace segments:If a test file path was e.g.
vendor/_laravel_ide/discover-bf37f1dc35944596ec71c910bc1ca84f.php, it yielded:PHP namespaces do not allow hyphens (
-), dots (.), or starting segments with numbers. The above code causes parser crashes in the language server, disabling helper type-hints in the IDE.The Fix
We updated the
pestNamespacefunction insrc/features/pest.tsto sanitize each segment of the path:/or\).[^a-zA-Z0-9_\x7f-\xff]) with underscores (_)._) if the segment begins with a digit (which is illegal for PHP identifiers).\.Before / After Example:
vendor/_laravel_ide/discover-bf37f1dc35944596ec71c910bc1ca84f.php_Pest\vendor\_laravel_ide\discover-bf37f1dc35944596ec71c910bc1ca84f.php(invalid PHP syntax)_Pest\vendor\_laravel_ide\discover_bf37f1dc35944596ec71c910bc1ca84f_php(valid PHP syntax)Benefits & Impact
_pest.php, allowing IDE Language Servers to parse the helper correctly and restore auto-completions on$thiscontext in Pest tests.