Hi guys,
I like to run my tests off of the php artisan serve command so everything runs through localhost:8000 instead of an entirely new hostname on homestead. It also makes it easy to push the code up to CI servers, anyway, here's what I'm doing, I find it works pretty well...
I use the @BeforeScenario hook to remove the sqlite database, if it exists (usually because of a cancelled test or something), and then re-create it and run my migrations and seeds. The key here is once they've run, I simply copy my .env.behat file to .env and let my tests run through the browser as normal with all my testing environment related settings.
Once all my tests have run, I use the @AfterSuite hook, to remove the sqlite database and also the .env file. I'm toying with the idea of keeping a .env.local file with all my homestead related settings and once my tests have finished, copying that back to .env.
Hopefully that helps somebody :)
<?php
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
//...
/**
* Flag the test database as ready for testing.
*
* @var bool
*/
public static $databaseReady = false;
/**
* @BeforeScenario
*/
public function onSetup()
{
if (!self::$databaseReady) {
if (file_exists('storage/database.sqlite')) {
shell_exec('rm storage/database.sqlite');
}
shell_exec('touch storage/database.sqlite');
shell_exec('cp .env.behat .env');
Artisan::call('migrate:refresh', ['--seed' => true]);
self::$databaseReady = true;
}
}
/**
* @AfterSuite
*/
public static function afterSuite()
{
shell_exec('rm .env');
shell_exec('rm storage/database.sqlite');
}
// ...
}
And my environment file looks something like this...
APP_ENV=testing
APP_DEBUG=true
APP_KEY=SomeRandomKey
DB_CONNECTION=sqlite
CACHE_DRIVER=file
SESSION_DRIVER=database/file
QUEUE_DRIVER=sync
# Using the Chrome Driver, so these should be left blank for
# testing, chrome won't parse sessions/cookies under localhost
SESSION_DOMAIN=
COOKIE_DOMAIN=