Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accessing a context in another context #72

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Feature: Accessing a context in another context

Scenario: Accessing a context in another context
Given a working Symfony application with SymfonyExtension configured
And a Behat configuration containing:
"""
default:
suites:
default:
contexts:
- App\Tests\SomeContext
- App\Tests\AnotherContext
"""
And a feature file containing:
"""
Feature:
Scenario:
Then it should pass
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php

namespace App\Tests;

use Behat\Behat\Context\Context;

final class SomeContext implements Context {
public function someMethod(): void {}
}
"""
And a context file "tests/AnotherContext.php" containing:
"""
<?php

namespace App\Tests;

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

final class AnotherContext implements Context {
/** @var SomeContext */
private $someContext;

/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

$this->someContext = $environment->getContext('App\Tests\SomeContext');
}

/** @Then it should pass */
public function itShouldPass(): void
{
$this->someContext->someMethod();
}
}
"""
And a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true

App\Tests\AnotherContext:
public: true
"""
When I run Behat
Then it should pass
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ public function hasContextClass($class): bool
}

/**
* @see http://behat.org/en/latest/cookbooks/accessing_contexts_from_each_other.html
*
* @throws ContextNotFoundException
*/
private function getContext(string $class): Context
public function getContext(string $class): Context
{
if (!isset($this->contexts[$class])) {
throw new ContextNotFoundException(sprintf(
Expand Down