Replies: 2 comments 2 replies
-
I'm currently trying to achieve something very similar, did you ever find a clean way to handle this? It's kind of a pain that sqlite doesn't support switching DBs, as I'd rather keep all this testing stuff out of MySQL entirely. My idea was to implement my own I haven't gotten to the bottom of exactly why this doesn't work, but I think it's something to do with switching tenants while a transaction is in progress. |
Beta Was this translation helpful? Give feedback.
-
Using MySQL we ran into issues around MySQL silently dropping transactions when any table or db creations happened. I worked around this with these steps A quick description of those steps are basically to create a test database service that pre-sets up a few tenant databases with migrations in place. It hooks this via phpunit Bootstrap and ParallelTesting events. For it to work with tests that deal with multiple tenants, you need to use a SwitchTenantDatabaseTask that effectively does a |
Beta Was this translation helpful? Give feedback.
-
Recently I've been wanting to be more purposeful about testing my app. Up til now I've had a few tests that I usually don't run. Last time I ran them everything works fine. Now I get all sort of database errors causing tests to fail.
I have a multitenancy app using this package and a unique db per tenant. I had a custom setup that would prefix all my database names with the enviroment so i have local_ and testing_ for each database on my machine. I had a custom setup method thst would migrate fresh the testing_landlord database before each test. And each test would create a tenant or two that it needed. Then if my tearDown I would iterate though each tenant record found in my landlord database to run my Job to delete the tenant db.
As you can imagine the process was slow. I'm talking about a minute just to run 22 tests. But it used to work. Now I have some jobs failing to delete the tenant db saying it's in use which means the following test fails to create it. It's a mess.
I did some reading and came to the conclusion I'd be better off having 2 or 3 tenants that are perpetual. So I would have a onetime setup at the beginning of the suite that would setup the testing_landlord database migrate fresh to it and create the tenants needed for all the tests. Then each test could use transactions to rollback their change each time to the inital state.
Problem is I'm not sure how to achieve this. AFAIK Laravel's trait to use transactions only seems to work on the default connection (the landlord database). And even if I could get it to work accross connections I only technically have 2 connections and the multitenancy package is swapping out the db in the second connection to the db for the tenant assigned to the currently authenticated user.
Any ideas on how to properly reset the world for each test when using this with per tenant databases?
Beta Was this translation helpful? Give feedback.
All reactions