Injected parameter test POC

This commit is contained in:
Bartosz Pietrzak
2019-01-09 16:09:20 +01:00
parent 9c41bdbcba
commit 742d3358b5
2 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
Feature: Injecting parameters into context
Background:
Given a context file "features/bootstrap/FeatureContext.php" containing:
"""
<?php
use Behat\Behat\Context\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeatureContext implements Context
{
private $parameter;
public function __construct(string $parameter)
{
$this->parameter = $parameter;
}
/**
* @Then the parameter should be injected into the context
*/
public function behatContainerShouldBeInjectedToTheContext()
{
if (null === $this->parameter || empty($this->parameter)) {
throw new \DomainException('Required parameter was not injected!');
}
}
}
"""
And an application kernel injecting a parameter into the FeatureContext class
And a Behat configuration with the minimal working configuration for SymfonyExtension
And a Behat configuration with the minimal working configuration for MinkExtension
Scenario: Injecting parameters into context with SymfonyExtension
Given a feature file "features/injecting_parameter_into_context.feature" containing:
"""
Feature: Injecting parameter into the context
Scenario:
Then the parameter should be injected into the context
"""
When I run Behat
Then it should pass

View File

@@ -157,6 +157,48 @@ CON
); );
} }
/**
* @Given /^an application kernel injecting a parameter into the FeatureContext class$/
*/
public function thereIsKernelInjectingParameterIntoFeatureContextClass(): void
{
$this->thereIsFile('app/AppKernel.php', <<<'CON'
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
];
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container): void {
$container->loadFromExtension('framework', [
'test' => $this->getEnvironment() === 'test',
'secret' => 'Pigeon',
]);
$contextDefinition = new Definition(FeatureContext::class, [new Parameter('kernel.environment')]);
$contextDefinition->setAutoconfigured(true);
$container->setDefinition(FeatureContext::class, $contextDefinition);
});
}
}
CON
);
}
/** /**
* @Given /^a feature file containing(?: "([^"]+)"|:)$/ * @Given /^a feature file containing(?: "([^"]+)"|:)$/
*/ */