Skip to content

Require bootstrap.php if exists, to load all necessary .env files #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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
];

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
Loading