Sep 22, 2018

PHPUnit Setup

For any new project I make I have to go into an older project and grab some custom code for the phpunit.xml file that PHPUnit uses when running tests.

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

Now that it's posted here I won't have to go hunting through GitHub.

Explanation

The important part is the DB_CONNECTION and DB_DATABASE values, which - in the above config - use an in-memory sqlite database instead of running tests on the db set up in your .env file.

Caveats

Using SQLite to run tests could get you in some trouble if your production database is not SQLite (some bugs might not show up locally & there are limitations in SQLite that aren't in other databases).

Sidenote: it's possible to set up your local automated tests to use an .env.testing file that points to a database of the same kind you are running in production. This also means your automated tests won't change the database you're working off in your local .env file. So anything you're doing in browser will remain after running PHPUnit!


1 Comment:

Nick said:
First time using this! Woohoo :-)
Please Login or Register to Comment