Last active
August 29, 2015 14:05
-
-
Save jwalton512/26705122de4ce1b76ec2 to your computer and use it in GitHub Desktop.
Setting Laravel environment for acceptance testing (in Behat)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"env":"local"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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