Test injecting parameters

This commit is contained in:
Kamil Kokot
2019-01-09 22:35:57 +01:00
parent 7250dfca76
commit 11f5a326b3

View File

@@ -2,52 +2,60 @@ Feature: Injecting parameters into context
Background:
Given a working Symfony application with SymfonyExtension configured
Given a context file "features/bootstrap/FeatureContext.php" containing:
And a Behat configuration containing:
"""
default:
suites:
default:
contexts:
- App\Tests\SomeContext
"""
And a feature file containing:
"""
Feature:
Scenario:
Then the passed parameter should be "test"
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php
use Behat\Behat\Context\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
namespace App\Tests;
class FeatureContext implements Context
{
use Behat\Behat\Context\Context;
final class SomeContext implements Context {
private $parameter;
public function __construct(string $parameter)
{
$this->parameter = $parameter;
}
public function __construct(?string $parameter = null) { $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!');
}
}
/** @Then the passed parameter should be :expected */
public function parameterShouldBe(string $expected): void { assert($this->parameter === $expected); }
}
"""
And an application kernel configured for SymfonyExtension with YAML services file containing:
Scenario: Injecting a parameter into a context explicitly set as public
Given a services file "config/services.yaml" containing:
"""
services:
FeatureContext:
class: FeatureContext
App\Tests\SomeContext:
public: true
arguments:
- "%kernel.environment%"
"""
And a Behat configuration with the minimal working configuration for SymfonyExtension
And a Behat configuration with the minimal working configuration for MinkExtension
When I run Behat
Then it should pass
Scenario: Injecting parameters into context with SymfonyExtension
Given a feature file "features/injecting_parameter_into_context.feature" containing:
Scenario: Injecting a parameter into an autoconfigured context
Given a services file "config/services.yaml" containing:
"""
Feature: Injecting parameter into the context
Scenario:
Then the parameter should be injected into the context
services:
_defaults:
autoconfigure: true
App\Tests\SomeContext:
arguments:
- "%kernel.environment%"
"""
When I run Behat
Then it should pass