Implement bootstrap file autodiscovering

This commit is contained in:
Kamil Kokot
2019-01-11 00:24:59 +01:00
parent fa3c02720c
commit d6701d785c
3 changed files with 142 additions and 3 deletions

View File

@@ -49,7 +49,7 @@ Feature: Autodiscovering the application kernel
- "@service_container" - "@service_container"
""" """
Scenario: Autodiscovering kernel in Symfony 3 directory structure application Scenario: Autodiscovering kernel in Symfony 4 directory structure application
Given a kernel file "src/Kernel.php" containing: Given a kernel file "src/Kernel.php" containing:
""" """
<?php <?php
@@ -91,7 +91,7 @@ Feature: Autodiscovering the application kernel
When I run Behat When I run Behat
Then it should pass Then it should pass
Scenario: Autodiscovering kernel in Symfony 4 directory structure application Scenario: Autodiscovering kernel in Symfony 3 directory structure application
Given a kernel file "app/AppKernel.php" containing: Given a kernel file "app/AppKernel.php" containing:
""" """
<?php <?php

View File

@@ -0,0 +1,101 @@
Feature: Autodiscovering bootstrap file
Background:
Given a working Symfony application with SymfonyExtension configured
And a Behat configuration containing:
"""
default:
suites:
default:
contexts:
- App\Tests\SomeContext
"""
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"
"""
Scenario: Autodiscovering bootstrap file in Symfony 4 directory structure application
Given a boostrap file "config/bootstrap.php" containing:
"""
<?php
putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
When I run Behat
Then it should pass
Scenario: Autodiscovering bootstrap file in Symfony 3 directory structure application
Given a boostrap file "app/autoload.php" containing:
"""
<?php
putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
When I run Behat
Then it should pass
Scenario: Failing to autodiscover the bootstrap file
Given a boostrap file "config/bootstrap.php" containing:
"""
<?php
putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
And a boostrap file "app/autoload.php" containing:
"""
<?php
putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
When I run Behat
Then it should fail with "Could not autodiscover the bootstrap file"
Scenario: Not loading autodiscovered bootstrap file if explicitly disabled
Given a boostrap file "config/bootstrap.php" containing:
"""
<?php
putenv("CUSTOM_VARIABLE=lol2");
$_SERVER['CUSTOM_VARIABLE'] = $_ENV['CUSTOM_VARIABLE'] = 'lol2';
"""
And a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: false
"""
When I run Behat
Then it should fail with "Symfony\Component\DependencyInjection\Exception\EnvNotFoundException"

View File

@@ -86,7 +86,7 @@ final class SymfonyExtension implements Extension
public function process(ContainerBuilder $container): void public function process(ContainerBuilder $container): void
{ {
$this->processBootstrap($container->getParameter('fob_symfony.bootstrap')); $this->processBootstrap($this->autodiscoverBootstrap($container->getParameter('fob_symfony.bootstrap')));
} }
private function registerMinkDriver(ExtensionManager $extensionManager): void private function registerMinkDriver(ExtensionManager $extensionManager): void
@@ -193,6 +193,44 @@ final class SymfonyExtension implements Extension
return $config; return $config;
} }
/**
* @param string|bool|null $bootstrap
*/
private function autodiscoverBootstrap($bootstrap): ?string
{
if (is_string($bootstrap)) {
return $bootstrap;
}
if ($bootstrap === false) {
return null;
}
$autoconfigured = 0;
if (file_exists('config/bootstrap.php')) {
$bootstrap = 'config/bootstrap.php';
++$autoconfigured;
}
if (file_exists('app/autoload.php')) {
$bootstrap = 'app/autoload.php';
++$autoconfigured;
}
if ($autoconfigured === 2) {
throw new \RuntimeException(
'Could not autodiscover the bootstrap file. ' .
'Please define it manually with "FriendsOfBehat\SymfonyExtension.bootstrap" configuration option. ' .
'Setting that option to "false" disables autodiscovering.'
);
}
return is_string($bootstrap) ? $bootstrap : null;
}
private function processBootstrap(?string $bootstrap): void private function processBootstrap(?string $bootstrap): void
{ {
if ($bootstrap === null) { if ($bootstrap === null) {