Merge pull request #45 from pamil/configuration-on-steroids

Configuration for kernel and bootstrap file & user-friendly autodiscovery
This commit is contained in:
Łukasz Chruściel
2019-01-11 13:41:48 +01:00
committed by GitHub
10 changed files with 728 additions and 19 deletions

View File

@@ -0,0 +1,214 @@
Feature: Autodiscovering the application kernel
Background:
Given a standard Symfony autoloader configured
And a feature file containing:
"""
Feature:
Scenario:
Then the passed service should be an instance of "\Psr\Container\ContainerInterface"
"""
And a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension: ~
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 $service;
public function __construct($service = null) { $this->service = $service; }
/** @Then the passed service should be an instance of :expected */
public function serviceShouldBe(string $expected): void
{
assert(is_object($this->service));
assert($this->service instanceof $expected);
}
}
"""
And a services file "config/services.yaml" containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- "@service_container"
"""
Scenario: Autodiscovering kernel in Symfony 4 directory structure application
Given a kernel file "src/Kernel.php" containing:
"""
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
When I run Behat
Then it should pass
Scenario: Autodiscovering kernel in Symfony 3 directory structure application
Given a kernel file "app/AppKernel.php" containing:
"""
<?php
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class AppKernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
When I run Behat
Then it should pass
Scenario: Failing to autodiscover the kernel
When I run Behat
Then it should fail with "Could not autodiscover the application kernel"
Scenario: Failing to autodiscover the kernel
Given a kernel file "src/Kernel.php" containing:
"""
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
And a kernel file "app/AppKernel.php" containing:
"""
<?php
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class AppKernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
When I run Behat
Then it should fail with "Could not autodiscover the application kernel"

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

@@ -0,0 +1,95 @@
Feature: Configuring application kernel
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;
use Symfony\Component\HttpKernel\KernelInterface;
final class SomeContext implements Context {
private $kernel;
public function __construct(KernelInterface $kernel) { $this->kernel = $kernel; }
/** @Then the application kernel should have environment :environment */
public function kernelEnvironmentShouldBe(string $environment): void { assert($this->kernel->getEnvironment() === $environment); }
/** @Then the application kernel should have debug :state*/
public function kernelDebugShouldBe(string $state): void
{
$map = ['enabled' => true, 'disabled' => false];
if (!array_key_exists($state, $map)) { throw new \Exception('Invalid state passed!'); }
assert($this->kernel->isDebug() === $map[$state]);
}
}
"""
And a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- "@kernel"
"""
Scenario: Using test environment with debug enabled by default
Given a feature file containing:
"""
Feature:
Scenario:
Then the application kernel should have environment "test"
And the application kernel should have debug enabled
"""
When I run Behat
Then it should pass
Scenario: Using configured environment
Given a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
kernel:
environment: custom
"""
And a feature file containing:
"""
Feature:
Scenario:
Then the application kernel should have environment "custom"
"""
When I run Behat
Then it should pass
Scenario: Using configured debug setting
Given a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
kernel:
debug: false
"""
And a feature file containing:
"""
Feature:
Scenario:
And the application kernel should have debug disabled
"""
When I run Behat
Then it should pass

View File

@@ -0,0 +1,146 @@
Feature: Loading configured application kernel
Background:
Given a standard Symfony autoloader configured
And a feature file containing:
"""
Feature:
Scenario:
Then the passed service should be an instance of "\Psr\Container\ContainerInterface"
"""
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 $service;
public function __construct($service = null) { $this->service = $service; }
/** @Then the passed service should be an instance of :expected */
public function serviceShouldBe(string $expected): void
{
assert(is_object($this->service));
assert($this->service instanceof $expected);
}
}
"""
And a services file "config/services.yaml" containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- "@service_container"
"""
Scenario: Loading kernel by its classname
Given a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
kernel:
class: App\Custom\Kernel
"""
And a kernel file "src/Custom/Kernel.php" containing:
"""
<?php
namespace App\Custom;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
When I run Behat
Then it should pass
Scenario: Loading kernel from custom path
Given a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
kernel:
path: app/Nested/Kernel.php
class: AppKernel
"""
And a kernel file "app/Nested/Kernel.php" containing:
"""
<?php
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class AppKernel extends HttpKernel
{
use MicroKernelTrait;
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'test' => true,
'secret' => 'Pigeon',
]);
$loader->load(__DIR__ . '/../../config/services.yaml');
}
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
}
"""
When I run Behat
Then it should pass

View File

@@ -0,0 +1,56 @@
Feature: Loading configured bootstrap file
Scenario: Loading configured bootstrap file
Given a working Symfony application with SymfonyExtension configured
And a Behat configuration containing:
"""
default:
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: 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