Skip to content

[13.x] Add flushState to FormRequest to reset global strict mode between tests#59574

Merged
taylorotwell merged 2 commits intolaravel:13.xfrom
sumaiazaman:13.x-form-request-flush-state
Apr 7, 2026
Merged

[13.x] Add flushState to FormRequest to reset global strict mode between tests#59574
taylorotwell merged 2 commits intolaravel:13.xfrom
sumaiazaman:13.x-form-request-flush-state

Conversation

@sumaiazaman
Copy link
Copy Markdown
Contributor

Summary

PR #59430 added FormRequest::failOnUnknownFields() which sets a static $globalFailOnUnknownFields flag. This flag persists across tests because FormRequest has no flushState() method and isn't included in the test lifecycle teardown.

The Bug

class FirstTest extends TestCase
{
    public function testStrictMode()
    {
        FormRequest::failOnUnknownFields();
        // test strict validation...
    }
}

class SecondTest extends TestCase
{
    public function testNormalMode()
    {
        // This test ALSO runs in strict mode because
        // $globalFailOnUnknownFields is still true!
    }
}

The Fix

  1. Add flushState() method to FormRequest that resets $globalFailOnUnknownFields
  2. Call it in InteractsWithTestCaseLifecycle teardown alongside all other flushState() calls

Context

Every other class with static state has flushState() called in the test teardown:

  • ConvertEmptyStringsToNull::flushState()
  • TrimStrings::flushState()
  • EncryptCookies::flushState()
  • PreventRequestForgery::flushState()
  • Validator::flushState()
  • etc.

FormRequest was missed because failOnUnknownFields() was just added in #59430.

Changes

  • src/Illuminate/Foundation/Http/FormRequest.php — Add flushState() method
  • src/Illuminate/Foundation/Testing/Concerns/InteractsWithTestCaseLifecycle.php — Call FormRequest::flushState() in teardown

sumaiazaman and others added 2 commits April 7, 2026 11:52
…een tests

FormRequest::failOnUnknownFields() sets a static flag that persists
across tests. Without a flushState() method and a call in the test
lifecycle teardown, enabling strict mode in one test leaks into all
subsequent tests.
@taylorotwell taylorotwell merged commit 9f4c5cf into laravel:13.x Apr 7, 2026
44 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants