Skip to content

Commit

Permalink
Allow passing any env variable to BootstrapPimcore::bootstrap() (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Jun 20, 2023
1 parent ba6b8fd commit a924f74
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

## next

### Breaking Changes:

- `Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap()` now expects named arguments.<br>
If you pass the application environment as a parameter, you now have to prefix it with: `APP_ENV:`:
```diff
-BootstrapPimcore::bootstrap('something')
+BootstrapPimcore::bootstrap(APP_ENV: 'something')
```
- The second parameter (`$value`) of `Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::setEnv()`
is now of type `string`.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ include dirname(__DIR__).'/vendor/autoload.php';
Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap();
```

There's also `BootstrapPimcore::setEnv()` to set environment variables during the bootstrap process.
You can also pass any environment variable via named arguments to this method:

> **Hint**: you can pass the application environment (`APP_ENV`) directly as a parameter of
> `BootstrapPimcore::bootstrap()` (it defaults to `test`).
```php
# tests/bootstrap.php
Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore::bootstrap(
APP_ENV: 'custom',
SOMETHING: 'else',
);
```

#### Integration Tests For a Bundle

Expand All @@ -48,9 +53,10 @@ use Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore;

include dirname(__DIR__).'/vendor/autoload.php';

BootstrapPimcore::setEnv('PIMCORE_PROJECT_ROOT', __DIR__.'/app');
BootstrapPimcore::setEnv('KERNEL_CLASS', TestKernel::class);
BootstrapPimcore::bootstrap();
BootstrapPimcore::bootstrap(
PIMCORE_PROJECT_ROOT: __DIR__.'/app',
KERNEL_CLASS: TestKernel::class,
);
```

> **Note**: Don't forget to create the `tests/app` directory!
Expand Down
15 changes: 9 additions & 6 deletions src/Pimcore/BootstrapPimcore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

final class BootstrapPimcore
{
public static function bootstrap(string $env = 'test'): void
private const DEFAULT_ENV_VARS = [
'APP_ENV' => 'test',
];

public static function bootstrap(string ...$envVars): void
{
self::setEnv('APP_ENV', $env);
foreach ($envVars + self::DEFAULT_ENV_VARS as $name => $value) {
self::setEnv($name, $value);
}

Bootstrap::setProjectRoot();
Bootstrap::bootstrap();
}

/**
* @param mixed $value
*/
public static function setEnv(string $name, $value): void
public static function setEnv(string $name, string $value): void
{
putenv("{$name}=".$_ENV[$name] = $_SERVER[$name] = $value);
}
Expand Down

0 comments on commit a924f74

Please sign in to comment.