Skip to content

Instantly share code, notes, and snippets.

@jwalton512
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwalton512/26705122de4ce1b76ec2 to your computer and use it in GitHub Desktop.
Save jwalton512/26705122de4ce1b76ec2 to your computer and use it in GitHub Desktop.
Setting Laravel environment for acceptance testing (in Behat)
{"env":"local"}
<?php
class FeatureContext extends MinkContext implements SnippetAcceptingContext {
private static $envFilePath;
private static $previousEnv;
const TEST_ENVIRONMENT = 'acceptance';
/**
* Initializes context.
*
* Every scenario gets its own context object.
* You can also pass arbitrary arguments to the context constructor through behat.yml.
*/
public function __construct() {
}
/**
* @static
* @beforeSuite
*/
public static function bootstrapLaravel() {
static::$envFilePath = __DIR__ . '/../../.env.json';
self::$previousEnv = self::getEnvironment();
self::setEnvironment(self::TEST_ENVIRONMENT);
$unitTesting = true;
$testEnvironment = self::TEST_ENVIRONMENT;
require_once __DIR__ . '/../../bootstrap/start.php';
}
/**
* @static
* @afterSuite
*/
public static function cleanUp() {
self::setEnvironment(self::$previousEnv);
}
/**
* @static
* @beforeScenario @db
*/
public static function migrateDb() {
Artisan::call('migrate:refresh');
}
private static function setEnvironment($environment) {
$envFile = self::parseEnvFile();
$envFile->env = $environment;
file_put_contents(static::$envFilePath, json_encode($envFile));
}
private static function getEnvironment() {
$envFile = self::parseEnvFile();
return $envFile->env;
}
private static function parseEnvFile() {
$envFile = json_decode(file_get_contents(static::$envFilePath));
return $envFile;
}
}
<?php
// boostrap/start.php
$env = $app->detectEnvironment(function () {
$envFilePath = __DIR__.'/../.env.json';
$env = getenv('LARAVEL_ENV');
if (file_exists($envFilePath) && $env == 'local') {
$envFile = json_decode(file_get_contents($envFilePath));
$env = $envFile->env;
return $env;
}
return $env ?: 'local';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment