rachids's avatar

Approach for testing a multi-tenant app.

Hello artisans, hope you are all doing well.

I am building a multi-tenant app and I am using the spatie/laravel-multitenancy package with single-domain / multiple db.

Everything is working great but I'd like to create some tests along the way and I hit a wall.

For instance, I want to test my signup process which goes like this :

  • User submit the signup form
  • I create the Tenant instance
  • Tenant instance generate a database
  • I make this tenant the current one
  • I create the user and some initial data

How would you test this knowing that the second part (creating the database) seems not possible with sqlite configured as in-memory ?

I'd like to have some insights from other developers and maybe get an occasion to improve my understanding of testing and working under multi-tenant context.

Thanks all for reading me.

0 likes
3 replies
fylzero's avatar
fylzero
Best Answer
Level 67

@rachids You could just shift your tests to use mysql. It is slower but there is an argument to be made for this case and also aligning to test against the exact same db you use in prod.

1 like
rachids's avatar

I haven't considered this but it makes sense!

Thanks @fylzero , I will shift my tests. :)

kei_mx's avatar

Hello 👋.

I had the same situation when I built a multitenant application. Then I created a custom test case to run tests in both the tenant and landlord contexts, and later I turned it into a package.

The package provides a comprehensive base test case with automatic tenant database isolation, configuration management, and helper methods to simplify testing in multi-tenant environments built with Spatie Laravel Multitenancy.

https://github.com/ArmCM/laravel-tenancy-testing

I hope this helps! I’d really appreciate any feedback or suggestions.

Please or to participate in this conversation.