Image for post
Image for post

Running serve automatically before Laravel Dusk

Whether it’s on CI or locally, I always run php artisan serve --env=dusk.local before running Dusk. Today I decided to automate that by adding my custom artisan command.

Make the command and register it

php artisan make:command DuskServeCommand

Don’t forget to open App/Console/Kernel.php and register it

/**
* The Artisan commands provided by your application.
*
*
@var array
*/
protected $commands = [
DuskServeCommand::class
];

The Command

We’re going to use the ProcessBuilder to run php artisan serve for us, fallback to dusk default behavior and then stop the processs. Unfortunately, we cannot call parent::handle() because Dusk will backup and replace .env by default. We need to swap the environment BEFORE running serve to make sure the correct variables are set in place. This means we’re going to have to copy most of Dusk original command into our custom command.

The Environment

Copy .env toenv.dusk.local file and set your environment variables. Just make sure to set APP_URL to http://127.0.0.1:8000 which is the address and port for serve .

Now just run it: php artisan dusk:serve . Also, remember that the file should be called .env.dusk.{YOUR_ORIGINAL_ENV} where your original env is whatever value you have in APP_ENV at .env .

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store