Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions example.phpspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ suites:
extensions:
PhpSpec\Laravel\Extension\LaravelExtension:
testing_environment: "testing"
# OR
testing_environment_file: ".phpspec.env"

formatter.name: pretty
10 changes: 8 additions & 2 deletions spec/PhpSpec/Laravel/Util/LaravelSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
3 changes: 2 additions & 1 deletion src/PhpSpec/Laravel/Extension/LaravelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

Expand Down
26 changes: 22 additions & 4 deletions src/PhpSpec/Laravel/Util/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -75,18 +84,27 @@ public function getAppPath()
return $this->appPath;
}

public function getEnvFile()
{
return $this->envFile;
}

/**
* Creates a Laravel application.
*
* @return \Illuminate\Foundation\Application
*/
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());

Expand Down