Skip to content

Commit

Permalink
Require bootstrap.php if exists, to load all necessary .env files (#190)
Browse files Browse the repository at this point in the history
* Require bootstrap.php if exists
* add bootstrap config parameter
  • Loading branch information
TavoNiievez committed Apr 17, 2024
1 parent 36e08c9 commit e02ff5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
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
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

0 comments on commit e02ff5a

Please sign in to comment.