[4.x] Correct DatabaseTenancyBootstrapperTest#1466
Conversation
Set the url in the template connection config only after creating a tenant. Before, the url was set before creating a tenant, and because of that, the tenant couldn't be created in the first place. During CreateDatabase, a QueryException (`SQLSTATE[HY000] [1049] Unknown database 'bc.us-east-1.rds.amazonaws.com'`) was thrown, and the test didn't get to exercise the bootstrapper's code branch that should throw an exception if the db url is set
The env var got renamed in Laravel 11 to `DB_URL`
📝 WalkthroughWalkthroughRenames the ChangesDB_URL rename and test updates
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1466 +/- ##
============================================
+ Coverage 86.35% 86.37% +0.02%
Complexity 1200 1200
============================================
Files 186 186
Lines 3524 3524
============================================
+ Hits 3043 3044 +1
+ Misses 481 480 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Other tests grab the central connection from the config instead of hardcoding 'central'. Doing the same in 'harden prevents tenants from using the database of another tenant' for consistency.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/TestCase.php`:
- Line 134: The shared test fixture in TestCase::createApplication() should not
depend on env('DB_URL'), because DatabaseTenancyBootstrapper::bootstrap()
rejects a non-null connection URL and makes tenancy tests sensitive to ambient
.env values. Remove the default url assignment from the suite-level central
database config, or switch it to a test-only env var so the fixture stays
URL-free unless a specific test overrides database.connections.central.url
itself.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: f9285dfd-5bc3-4ef4-81f6-2c4578078a3b
📒 Files selected for processing (3)
src/Bootstrappers/DatabaseTenancyBootstrapper.phptests/Bootstrappers/DatabaseTenancyBootstrapperTest.phptests/TestCase.php
…ency Decided to go with the variable assignment instead of inlining the config(...) call in the assertion since it's a bit more readable imo
Is this based on a coderabbit suggestion? I'm pretty sure we almost always hardcode |
Test for DatabaseTenancyBootstrapper throwing an exception when
DB_URLis setThe test meant to cover this set
central.urlbefore creating a tenant. That made theCreateDatabasejob fail with a QueryException (it tried to use the URL host as a database name). The bootstrapper's check was never reached, and the exception that the test intended to cover wasn't ever thrown in the end. The test passed for the wrong reason.Fixed by creating the tenant first, setting the url only after that, and asserting that
tenancy()->initialize()throws the bootstrapper's specific exception.Reference env variable that Laravel uses now (
DB_URL) instead ofDATABASE_URLChanged all
DATABASE_URLreferences in Tenancy toDB_URL. In Laravel 11, theDATABASE_URLenv variable got renamed toDB_URLinconfig/database.php. The env var got renamed quite a while ago, so referencingDATABASE_URLin Tenancy's comments and tests was a bit confusing.Minor cleanup
The
harden prevents tenants from using the database of another tenanttest now reads the central connection name fromconfig('tenancy.database.central_connection')instead of hardcoding'central'for consistency with the other tests.Summary by CodeRabbit
DB_URL, ensuring consistent connection setup.