Skip to content

Commit e02ff5a

Browse files
authored
Require bootstrap.php if exists, to load all necessary .env files (#190)
* Require bootstrap.php if exists * add bootstrap config parameter
1 parent 36e08c9 commit e02ff5a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"symfony/config": "^5.4 | ^6.4 | ^7.0",
3333
"symfony/dependency-injection": "^5.4 | ^6.4 | ^7.0",
3434
"symfony/dom-crawler": "^5.4 | ^6.4 | ^7.0",
35+
"symfony/dotenv": "^5.4 | ^6.4 | ^7.0",
3536
"symfony/error-handler": "^5.4 | ^6.4 | ^7.0",
3637
"symfony/filesystem": "^5.4 | ^6.4 | ^7.0",
3738
"symfony/form": "^5.4 | ^6.4 | ^7.0",

src/Codeception/Module/Symfony.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
use Codeception\TestInterface;
2929
use Doctrine\ORM\EntityManagerInterface;
3030
use Exception;
31+
use LogicException;
3132
use ReflectionClass;
3233
use ReflectionException;
3334
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
3435
use Symfony\Component\BrowserKit\AbstractBrowser;
3536
use Symfony\Component\DependencyInjection\ContainerInterface;
37+
use Symfony\Component\Dotenv\Dotenv;
3638
use Symfony\Component\Finder\Finder;
3739
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
3840
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
@@ -84,6 +86,7 @@
8486
* * `cache_router`: 'false' - Enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
8587
* * `rebootable_client`: 'true' - Reboot client's kernel before each request
8688
* * `guard`: 'false' - Enable custom authentication system with guard (only for Symfony 5.4)
89+
* * `bootstrap`: 'false' - Enable the test environment setup with the tests/bootstrap.php file if it exists or with Symfony DotEnv otherwise. If false, it does nothing.
8790
* * `authenticator`: 'false' - Reboot client's kernel before each request (only for Symfony 6.0 or higher)
8891
*
8992
* #### Sample `Functional.suite.yml`
@@ -167,6 +170,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
167170
'em_service' => 'doctrine.orm.entity_manager',
168171
'rebootable_client' => true,
169172
'authenticator' => false,
173+
'bootstrap' => false,
170174
'guard' => false
171175
];
172176

@@ -204,6 +208,9 @@ public function _initialize(): void
204208
}
205209

206210
$this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']);
211+
if($this->config['bootstrap']) {
212+
$this->bootstrapEnvironment();
213+
}
207214
$this->kernel->boot();
208215

209216
if ($this->config['cache_router'] === true) {
@@ -459,6 +466,26 @@ protected function getInternalDomains(): array
459466
return array_unique($internalDomains);
460467
}
461468

469+
private function bootstrapEnvironment(): void
470+
{
471+
$bootstrapFile = $this->kernel->getProjectDir() . '/tests/bootstrap.php';
472+
473+
if (file_exists($bootstrapFile)) {
474+
require_once $bootstrapFile;
475+
} else {
476+
if (!method_exists(Dotenv::class, 'bootEnv')) {
477+
throw new LogicException(
478+
"Symfony DotEnv is missing. Try running 'composer require symfony/dotenv'\n" .
479+
"If you can't install DotEnv add your env files to the 'params' key in codeception.yml\n" .
480+
"or update your symfony/framework-bundle recipe by running:\n" .
481+
'composer recipes:install symfony/framework-bundle --force'
482+
);
483+
}
484+
$_ENV['APP_ENV'] = $this->config['environment'];
485+
(new Dotenv())->bootEnv('.env');
486+
}
487+
}
488+
462489
/**
463490
* Ensures autoloader loading of additional directories.
464491
* It is only required for CI jobs to run correctly.

0 commit comments

Comments
 (0)