diff --git a/composer.json b/composer.json index 9375767..684c23c 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "symfony/config": "^5.4 | ^6.4 | ^7.0", "symfony/dependency-injection": "^5.4 | ^6.4 | ^7.0", "symfony/dom-crawler": "^5.4 | ^6.4 | ^7.0", + "symfony/dotenv": "^5.4 | ^6.4 | ^7.0", "symfony/error-handler": "^5.4 | ^6.4 | ^7.0", "symfony/filesystem": "^5.4 | ^6.4 | ^7.0", "symfony/form": "^5.4 | ^6.4 | ^7.0", diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 2e6f4d7..6323e52 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -28,11 +28,13 @@ use Codeception\TestInterface; use Doctrine\ORM\EntityManagerInterface; use Exception; +use LogicException; use ReflectionClass; use ReflectionException; use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector; use Symfony\Component\BrowserKit\AbstractBrowser; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector; @@ -84,6 +86,7 @@ * * `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) * * `rebootable_client`: 'true' - Reboot client's kernel before each request * * `guard`: 'false' - Enable custom authentication system with guard (only for Symfony 5.4) + * * `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. * * `authenticator`: 'false' - Reboot client's kernel before each request (only for Symfony 6.0 or higher) * * #### Sample `Functional.suite.yml` @@ -167,6 +170,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule 'em_service' => 'doctrine.orm.entity_manager', 'rebootable_client' => true, 'authenticator' => false, + 'bootstrap' => false, 'guard' => false ]; @@ -204,6 +208,9 @@ public function _initialize(): void } $this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']); + if($this->config['bootstrap']) { + $this->bootstrapEnvironment(); + } $this->kernel->boot(); if ($this->config['cache_router'] === true) { @@ -459,6 +466,26 @@ protected function getInternalDomains(): array return array_unique($internalDomains); } + private function bootstrapEnvironment(): void + { + $bootstrapFile = $this->kernel->getProjectDir() . '/tests/bootstrap.php'; + + if (file_exists($bootstrapFile)) { + require_once $bootstrapFile; + } else { + if (!method_exists(Dotenv::class, 'bootEnv')) { + throw new LogicException( + "Symfony DotEnv is missing. Try running 'composer require symfony/dotenv'\n" . + "If you can't install DotEnv add your env files to the 'params' key in codeception.yml\n" . + "or update your symfony/framework-bundle recipe by running:\n" . + 'composer recipes:install symfony/framework-bundle --force' + ); + } + $_ENV['APP_ENV'] = $this->config['environment']; + (new Dotenv())->bootEnv('.env'); + } + } + /** * Ensures autoloader loading of additional directories. * It is only required for CI jobs to run correctly.