Skip to content

[CodeQuality] Detect Behat context by interface, not class name suffix#691

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-assert-func-call-behat-context
Jun 29, 2026
Merged

[CodeQuality] Detect Behat context by interface, not class name suffix#691
TomasVotruba merged 1 commit into
mainfrom
fix-assert-func-call-behat-context

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Bug

AssertFuncCallToPHPUnitAssertRector decided a class was a Behat context with:

return str_ends_with($className, 'Context');

Any class whose name ends with Context matched — including ordinary
production classes that have nothing to do with Behat. The rule then rewrote
their assert() calls into \PHPUnit\Framework\Assert:: calls, pulling PHPUnit
into non-test source.

Found in webonyx/graphql-php, whose
rector.php skips this rule on src for exactly this reason. SchemaValidationContext
(a plain production class) was rewritten:

-assert($type instanceof ScalarType, 'only remaining option');
+\PHPUnit\Framework\Assert::assertInstanceOf(ScalarType::class, $type, 'only remaining option');

Fix

Detect a real Behat context by whether it implements Behat\Behat\Context\Context
(the existing BehatClassName::CONTEXT constant, already used the same way in
ScalarArgumentToExpectedParamTypeRector), instead of matching the class name.

Existing *Context fixtures updated to implement the interface; added
skip_non_behat_context_class.php.inc covering a non-Behat *Context class.

isBehatContext() matched any class whose name ends with 'Context' via
str_ends_with(). That false-positives on production classes like
SchemaValidationContext, rewriting their assert() calls into
\PHPUnit\Framework\Assert:: calls and pulling PHPUnit into non-test code.

Detect a real Behat context by whether it implements
Behat\Behat\Context\Context instead.
@TomasVotruba TomasVotruba merged commit af52365 into main Jun 29, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the fix-assert-func-call-behat-context branch June 29, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant