Testing Laravel Applications without Vagrant or Docker


Even though Laravel has a great toolset for making testing easier, there is still a ridiculous amount of people who choose to ignore tests.

If you’re utilising the full Eloquent ORM and not relying on hammering out too many database engine dependant raw SQL queries, there is a neat way to run your tests without the need of Vagrant, Docker or any other external help.

All you need for this is PHPUnit and PHP 7.1 (or whatever your version of Laravel requires) installed on your machine. If you’re on macOS, you can easily install these using brew.

Open up the phpunit.xml file located in the root directory of your Laravel project. Now change whatever is in between the <php></php> tags with this:

<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

This will use the sqlite database driver (provided by default in Laravel) for testing and it will use an in-memory database.

All the other stuff is pretty much the default phpunit.xml that comes with Laravel. These simply set up the framework so that it does not use any external services for queues, sessions or cache and relies only on PHP.

Now, when you run phpunit it should be using the new settings.

Closing Note

This idea is certainly not novel, but it’s sparsely documented and thus it’s hard to come across. Hopefully this gives you less excuses to avoid testing and speeds up your workflows. Happy testing! :)

Related Posts

Zero Downtime Deployments With Laravel Forge