Skip to content
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

fix: allow to use parameter resolution in configuration for bootstrap key #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions features/configuration/loading_configured_bootstrap_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,58 @@ Feature: Loading configured bootstrap file
"""
When I run Behat
Then it should pass

Scenario: Loading configured bootstrap file with parameter resolution
Given a working Symfony application with SymfonyExtension configured
And a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: '%paths.base%/custom/bootstrap.php'

suites:
default:
contexts:
- App\Tests\SomeContext
"""
And a boostrap file "custom/bootstrap.php" containing:
"""
<?php

putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php

namespace App\Tests;

use Behat\Behat\Context\Context;

final class SomeContext implements Context {
private $parameter;

public function __construct(?string $parameter = null) { $this->parameter = $parameter; }

/** @Then the passed parameter should be :expected */
public function parameterShouldBe(string $expected): void { assert($this->parameter === $expected); }
}
"""
And a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- "%env(CUSTOM_VARIABLE)%"
"""
And a feature file containing:
"""
Feature:
Scenario:
Then the passed parameter should be "lol2"
"""
When I run Behat
Then it should pass
16 changes: 9 additions & 7 deletions src/ServiceContainer/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;

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

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

$this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel']));
$this->loadDriverKernel($container);
Expand Down Expand Up @@ -232,26 +233,27 @@ private function autodiscoverKernelConfiguration(array $config): array
/**
* @param string|bool|null $bootstrap
*/
private function autodiscoverBootstrap($bootstrap): ?string
private function autodiscoverBootstrap($bootstrap, ParameterBag $parameterBag): ?string
{
if (is_string($bootstrap)) {
return $bootstrap;
return $parameterBag->resolveString($bootstrap);
}

if ($bootstrap === false) {
return null;
}

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

if (file_exists('config/bootstrap.php')) {
$bootstrap = 'config/bootstrap.php';
if (file_exists($basePath . '/config/bootstrap.php')) {
$bootstrap = $basePath . '/config/bootstrap.php';

++$autodiscovered;
}

if (file_exists('app/autoload.php')) {
$bootstrap = 'app/autoload.php';
if (file_exists($basePath . '/app/autoload.php')) {
$bootstrap = $basePath . '/app/autoload.php';

++$autodiscovered;
}
Expand Down