Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrismichaels84's avatar

How to test package that relies on Eloquent. Mock DB?

I am working on a simple package that allows for metadata to be attached to eloquent models. I forked Scubaclick/Meta and added a few features. Everything is working well, but I want to add some simple unit tests.

My question is: what is the best practice to test Eloquent models from a package? Every test will write or retrieve something from the database. Some research has pointed me in the direction of using sqlite in memory database for this. The problem is, I'm writing a package test, not an application test. I don't necessarily have control over application configuration. There is also an artisan command that generates a migration that needs to be tested.

I feel like I'm missing something simple here. I hope this makes sense. Thanks!

0 likes
2 replies
iverberk's avatar
Level 1

Hi Chris,

I faced the same problem for my package where I needed interaction with the database but don't have a full-blown application context. Take a look at my tests bootstrap file here:

https://github.com/iverberk/larasearch/blob/master/tests/bootstrap.php

As you can see it's pretty simple to boot up Eloquent and use it stand-alone in your tests. I've used a persistent database, but you could switch to an in memory database with migrations.

Hope this helps.

Please or to participate in this conversation.