Skip to content

Commit efcc394

Browse files
committed
Require bootstrap.php if exists
1 parent 36e08c9 commit efcc394

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;
@@ -204,6 +206,7 @@ public function _initialize(): void
204206
}
205207

206208
$this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']);
209+
$this->bootstrapEnvironment();
207210
$this->kernel->boot();
208211

209212
if ($this->config['cache_router'] === true) {
@@ -459,6 +462,30 @@ protected function getInternalDomains(): array
459462
return array_unique($internalDomains);
460463
}
461464

465+
private function bootstrapEnvironment(): void
466+
{
467+
foreach (['/config', '/tests'] as $folder) {
468+
if (file_exists($bootstrapFile = $this->kernel->getProjectDir() . $folder . '/bootstrap.php')) {
469+
break;
470+
}
471+
}
472+
473+
if (!empty($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)