[12.x] Fix transaction control in HTTP Tests #58051
Closed
+704
−13
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.
Transaction Control in HTTP Tests
📋 Overview
This feature adds automatic database transaction control during HTTP tests when the
DatabaseTransactionstrait is used. The implementation ensures that the transaction level is always reset to 1 after each HTTP request in tests, avoiding issues with uncommitted nested transactions.🔧 Technical Changes
Modified Files
1.
src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.phpNew Method Added:
Updated HTTP Methods:
All HTTP methods in the trait have been updated to call
resetDatabaseTransactionLevelToOne()after executing the request:get()getJson()post()postJson()put()putJson()patch()patchJson()delete()deleteJson()options()optionsJson()head()Implementation Example:
2.
tests/Integration/Foundation/Testing/Concerns/MakeHttpRequestsWithDatabaseTransactionTest.php(New File)Complete test file with 620 lines containing 22 test scenarios covering:
🎯 Problem Solved
Before Changes
When you used the
DatabaseTransactionstrait in your tests and made HTTP requests that started new transactions (withDB::beginTransaction()), the framework did not properly manage the nested transaction level. This caused:Example of the Problem:
After Changes
The framework now automatically detects and resets nested transactions to level 1 after each HTTP request, ensuring that:
Same Example, Now Working:
✅ Why It Doesn't Break Existing Features
1. Full Backward Compatibility
The feature is opt-in through the
DatabaseTransactionstrait:DatabaseTransactions: Nothing changes. Behavior is exactly the same.DatabaseTransactions: Feature only adds additional behavior that improves transaction management.2. Only Fixes Problematic Behavior
The feature doesn't change expected behavior, it only fixes a bug where nested transactions weren't managed correctly. Previous behavior was inconsistent and caused hard-to-debug bugs.
3. Respects Explicit Commits
If application code explicitly commits, the feature respects that decision:
4. Doesn't Interfere with Level 1 Transactions
Feature only acts when there are nested transactions (level > 1). The main transaction from
DatabaseTransactions(level 1) remains intact: