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