From 26dd5c32f973a311ead0d752f732c0635122dea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Pi=C4=85tek?= Date: Tue, 8 Aug 2017 15:27:14 +0200 Subject: [PATCH 1/4] Added spec --- spec/PhpSpec/Laravel/Util/LaravelSpec.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spec/PhpSpec/Laravel/Util/LaravelSpec.php b/spec/PhpSpec/Laravel/Util/LaravelSpec.php index 851d078..54c6992 100644 --- a/spec/PhpSpec/Laravel/Util/LaravelSpec.php +++ b/spec/PhpSpec/Laravel/Util/LaravelSpec.php @@ -17,13 +17,19 @@ function let(Application $appInst) function it_boots_in_the_testing_env_by_default() { - $this->beConstructedWith(null, '.'); + $this->beConstructedWith(null, '.', null); $this->getEnv()->shouldBe('testing'); } function it_allows_the_env_to_be_set_to_anything() { - $this->beConstructedWith('whatever', '.'); + $this->beConstructedWith('whatever', '.', null); $this->getEnv()->shouldBe('whatever'); } + + function it_allows_the_env_file_to_be_set() + { + $this->beConstructedWith(null, '.', '.phpspec.env'); + $this->getEnvFile()->shouldBe('.phpspec.env'); + } } From c19822b3ac7f5162b431e3e125b981233e97edf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Pi=C4=85tek?= Date: Tue, 8 Aug 2017 15:31:08 +0200 Subject: [PATCH 2/4] Implemented the change --- .../Laravel/Extension/LaravelExtension.php | 3 ++- src/PhpSpec/Laravel/Util/Laravel.php | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/PhpSpec/Laravel/Extension/LaravelExtension.php b/src/PhpSpec/Laravel/Extension/LaravelExtension.php index c4fad9a..7c7726d 100644 --- a/src/PhpSpec/Laravel/Extension/LaravelExtension.php +++ b/src/PhpSpec/Laravel/Extension/LaravelExtension.php @@ -25,7 +25,8 @@ public function load(ServiceContainer $container, array $params) $container->define('laravel', function () use ($params) { return new Laravel( isset($params['testing_environment']) ? $params['testing_environment'] : null, - $this->getBootstrapPath(isset($params['framework_path']) ? $params['framework_path'] : null) + $this->getBootstrapPath(isset($params['framework_path']) ? $params['framework_path'] : null), + isset($params['testing_environment_file']) ? $params['testing_environment_file'] : null ); }); diff --git a/src/PhpSpec/Laravel/Util/Laravel.php b/src/PhpSpec/Laravel/Util/Laravel.php index dfb06bd..ae88b51 100644 --- a/src/PhpSpec/Laravel/Util/Laravel.php +++ b/src/PhpSpec/Laravel/Util/Laravel.php @@ -32,16 +32,25 @@ class Laravel */ protected $appPath; + /** + * Path to the custom .env file. + * + * @var string + */ + protected $envFile; + /** * Constructor. * * @param string $env Laravel testing environment. 'testing' by default * @param string $appPath Path to the Laravel bootstrap dir + * @param string $envPath Path to the custom .env file */ - public function __construct($env, $appPath) + public function __construct($env, $appPath, $envPath) { $this->env = $env ?: 'testing'; $this->appPath = $appPath; + $this->envFile = $envPath; } /** @@ -75,6 +84,11 @@ public function getAppPath() return $this->appPath; } + public function getEnvFile() + { + return $this->envFile; + } + /** * Creates a Laravel application. * @@ -82,11 +96,15 @@ public function getAppPath() */ protected function createApplication() { - putenv('APP_ENV=' . $this->getEnv()); - $app = require $this->appPath; - $app->make(Kernel::class)->bootstrap(); + if ($this->getEnvFile()) { + $app->loadEnvironmentFrom($this->getEnvFile()); + } else { + putenv('APP_ENV=' . $this->getEnv()); + } + + $app->make(Kernel::class)->bootstrap();; Carbon::setTestNow(Carbon::now()); From 07a3506914efb4252f668a26a67d8dc49f174ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Pi=C4=85tek?= Date: Tue, 8 Aug 2017 15:31:23 +0200 Subject: [PATCH 3/4] Updated the example PHPSpec configuration --- example.phpspec.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example.phpspec.yml b/example.phpspec.yml index c9cc3ae..9bd3bca 100644 --- a/example.phpspec.yml +++ b/example.phpspec.yml @@ -8,5 +8,7 @@ suites: extensions: PhpSpec\Laravel\Extension\LaravelExtension: testing_environment: "testing" + # OR + testing_environment_file: ".phpspec.env" formatter.name: pretty From 42d0fdc5cfeb730c8d1330b7c914011cfbe862e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Pi=C4=85tek?= Date: Tue, 8 Aug 2017 15:31:33 +0200 Subject: [PATCH 4/4] Updated the readme file --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 08cc75b..0f418cd 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,19 @@ extensions: in your `phpspec.yml`. +### Environment file + +You may also want to bootstrap laravel with a custom `.env` file. You can do this by setting: +```yaml +extensions: + PhpSpec\Laravel\Extension\LaravelExtension: + testing_environment_file: ".phpspec.env" +``` + +in your `phpspec.yml`. + +> Please note that `APP_ENV` set in your custom `.env` file will override the `testing_environment` setting. + ### App bootstrap path By default, the extension will bootstrap your app by looking for `bootstrap/app.php`