Skip to content

Commit 814dc0e

Browse files
authored
Merge pull request #213 from wuchen90/fix-bootstrap
fix: allow to use parameter resolution in configuration for bootstrap key
2 parents dfb1c9c + 65cd347 commit 814dc0e

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

features/configuration/loading_configured_bootstrap_file.feature

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,58 @@ Feature: Loading configured bootstrap file
5454
"""
5555
When I run Behat
5656
Then it should pass
57+
58+
Scenario: Loading configured bootstrap file with parameter resolution
59+
Given a working Symfony application with SymfonyExtension configured
60+
And a Behat configuration containing:
61+
"""
62+
default:
63+
extensions:
64+
FriendsOfBehat\SymfonyExtension:
65+
bootstrap: '%paths.base%/custom/bootstrap.php'
66+
67+
suites:
68+
default:
69+
contexts:
70+
- App\Tests\SomeContext
71+
"""
72+
And a boostrap file "custom/bootstrap.php" containing:
73+
"""
74+
<?php
75+
76+
putenv("CUSTOM_VARIABLE=lol2");
77+
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
78+
"""
79+
And a context file "tests/SomeContext.php" containing:
80+
"""
81+
<?php
82+
83+
namespace App\Tests;
84+
85+
use Behat\Behat\Context\Context;
86+
87+
final class SomeContext implements Context {
88+
private $parameter;
89+
90+
public function __construct(?string $parameter = null) { $this->parameter = $parameter; }
91+
92+
/** @Then the passed parameter should be :expected */
93+
public function parameterShouldBe(string $expected): void { assert($this->parameter === $expected); }
94+
}
95+
"""
96+
And a YAML services file containing:
97+
"""
98+
services:
99+
App\Tests\SomeContext:
100+
public: true
101+
arguments:
102+
- "%env(CUSTOM_VARIABLE)%"
103+
"""
104+
And a feature file containing:
105+
"""
106+
Feature:
107+
Scenario:
108+
Then the passed parameter should be "lol2"
109+
"""
110+
When I run Behat
111+
Then it should pass

src/ServiceContainer/SymfonyExtension.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
2222
use Symfony\Component\DependencyInjection\Definition;
2323
use Symfony\Component\DependencyInjection\Parameter;
24+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2425
use Symfony\Component\DependencyInjection\Reference;
2526

2627
final class SymfonyExtension implements Extension
@@ -73,7 +74,7 @@ public function load(ContainerBuilder $container, array $config): void
7374
{
7475
$this->setupTestEnvironment($config['kernel']['environment'] ?? 'test');
7576

76-
$this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap']));
77+
$this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap'], $container->getParameterBag()));
7778

7879
$this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel']));
7980
$this->loadDriverKernel($container);
@@ -232,26 +233,27 @@ private function autodiscoverKernelConfiguration(array $config): array
232233
/**
233234
* @param string|bool|null $bootstrap
234235
*/
235-
private function autodiscoverBootstrap($bootstrap): ?string
236+
private function autodiscoverBootstrap($bootstrap, ParameterBag $parameterBag): ?string
236237
{
237238
if (is_string($bootstrap)) {
238-
return $bootstrap;
239+
return $parameterBag->resolveString($bootstrap);
239240
}
240241

241242
if ($bootstrap === false) {
242243
return null;
243244
}
244245

245246
$autodiscovered = 0;
247+
$basePath = $parameterBag->get('paths.base');
246248

247-
if (file_exists('config/bootstrap.php')) {
248-
$bootstrap = 'config/bootstrap.php';
249+
if (file_exists($basePath . '/config/bootstrap.php')) {
250+
$bootstrap = $basePath . '/config/bootstrap.php';
249251

250252
++$autodiscovered;
251253
}
252254

253-
if (file_exists('app/autoload.php')) {
254-
$bootstrap = 'app/autoload.php';
255+
if (file_exists($basePath . '/app/autoload.php')) {
256+
$bootstrap = $basePath . '/app/autoload.php';
255257

256258
++$autodiscovered;
257259
}

0 commit comments

Comments
 (0)