Add tests for context initializers
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
Feature: Mink integration with dependency injection
|
Feature: Mink integration
|
||||||
|
|
||||||
Background:
|
Background:
|
||||||
Given a working Symfony application with SymfonyExtension configured
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
Feature: Mink integration with context initializer
|
|
||||||
|
|
||||||
Scenario: Passing Mink instance and parameters through context initializer
|
|
||||||
Given a working Symfony application with SymfonyExtension configured
|
|
||||||
And a Behat configuration containing:
|
|
||||||
"""
|
|
||||||
default:
|
|
||||||
extensions:
|
|
||||||
Behat\MinkExtension:
|
|
||||||
base_url: "http://localhost:8080/"
|
|
||||||
default_session: symfony
|
|
||||||
sessions:
|
|
||||||
symfony:
|
|
||||||
symfony: ~
|
|
||||||
suites:
|
|
||||||
default:
|
|
||||||
contexts:
|
|
||||||
- App\Tests\SomeContext
|
|
||||||
"""
|
|
||||||
And a feature file containing:
|
|
||||||
"""
|
|
||||||
Feature:
|
|
||||||
Scenario:
|
|
||||||
When I visit the page "/hello-world"
|
|
||||||
Then I should see "Hello world!" on the page
|
|
||||||
And the base url from Mink parameters should be "http://localhost:8080/"
|
|
||||||
|
|
||||||
# Doubling the scenario to account for some weird error connected to Mink's session
|
|
||||||
Scenario:
|
|
||||||
When I visit the page "/hello-world"
|
|
||||||
Then I should see "Hello world!" on the page
|
|
||||||
And the base url from Mink parameters should be "http://localhost:8080/"
|
|
||||||
"""
|
|
||||||
And a context file "tests/SomeContext.php" containing:
|
|
||||||
"""
|
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Tests;
|
|
||||||
|
|
||||||
use Behat\Mink\Mink;
|
|
||||||
use Behat\MinkExtension\Context\MinkAwareContext;
|
|
||||||
|
|
||||||
final class SomeContext implements MinkAwareContext {
|
|
||||||
private $mink;
|
|
||||||
private $parameters;
|
|
||||||
|
|
||||||
public function setMink(Mink $mink): void
|
|
||||||
{
|
|
||||||
$this->mink = $mink;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setMinkParameters(array $minkParameters): void
|
|
||||||
{
|
|
||||||
$this->parameters = $minkParameters;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @When I visit the page :page */
|
|
||||||
public function visitPage(string $page): void
|
|
||||||
{
|
|
||||||
$this->mink->getSession()->visit($page);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @Then I should see :content on the page */
|
|
||||||
public function shouldSeeContentOnPage(string $content): void
|
|
||||||
{
|
|
||||||
assert(false !== strpos($this->mink->getSession()->getPage()->getContent(), $content));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @Then the base url from Mink parameters should be :expected */
|
|
||||||
public function baseUrlShouldBe(string $expected): void
|
|
||||||
{
|
|
||||||
assert(isset($this->parameters['base_url']));
|
|
||||||
assert($this->parameters['base_url'] === $expected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
When I run Behat
|
|
||||||
Then it should pass
|
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
Feature: Context initializer compatibility
|
||||||
|
|
||||||
|
Scenario: Using class resolvers while handling context environment
|
||||||
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
|
And a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
FriendsOfBehat\ServiceContainerExtension:
|
||||||
|
imports:
|
||||||
|
- "tests/context_initializer.yml"
|
||||||
|
|
||||||
|
suites:
|
||||||
|
default:
|
||||||
|
contexts:
|
||||||
|
- App\Tests\SomeContext
|
||||||
|
"""
|
||||||
|
And a Behat services definition file "tests/context_initializer.yml" containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\CustomContextInitializer:
|
||||||
|
tags: ["context.initializer"]
|
||||||
|
"""
|
||||||
|
And a Behat service implementation file "tests/CustomContextInitializer.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests;
|
||||||
|
|
||||||
|
use Behat\Behat\Context\Context;
|
||||||
|
use Behat\Behat\Context\Initializer\ContextInitializer;
|
||||||
|
|
||||||
|
final class CustomContextInitializer implements ContextInitializer
|
||||||
|
{
|
||||||
|
public function initializeContext(Context $context): void
|
||||||
|
{
|
||||||
|
$context->makeItPass(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
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 {
|
||||||
|
private $shouldPass = false;
|
||||||
|
|
||||||
|
public function makeItPass(bool $shouldPass)
|
||||||
|
{
|
||||||
|
$this->shouldPass = $shouldPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Then it should pass */
|
||||||
|
public function itShouldPass(): void
|
||||||
|
{
|
||||||
|
assert($this->shouldPass === true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
Reference in New Issue
Block a user