Skip to content

Commit

Permalink
Provide simple BrowserKit integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed May 17, 2019
1 parent 24a318d commit 9d5b241
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
81 changes: 81 additions & 0 deletions features/browserkit_integration/browserkit_integration.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Feature: BrowserKit integration

Background:
Given a working Symfony application with SymfonyExtension configured
And a Behat configuration containing:
"""
default:
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
# Doubling the scenario to account for some weird error
Scenario:
When I visit the page "/hello-world"
Then I should see "Hello world!" on the page
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php
namespace App\Tests;
use Behat\Behat\Context\Context;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use Psr\Container\ContainerInterface;
use Symfony\Component\BrowserKit\Client;
final class SomeContext implements Context {
private $client;
public function __construct(Client $client)
{
$this->client = $client;
}
/** @When I visit the page :page */
public function visitPage(string $page): void
{
$this->client->request('GET', $page);
}
/** @Then I should see :content on the page */
public function shouldSeeContentOnPage(string $content): void
{
assert(false !== strpos($this->client->getResponse()->getContent(), $content));
}
}
"""

Scenario: Injecting BrowserKit client
Given a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- '@test.client'
"""
When I run Behat
Then it should pass

Scenario: Autowiring and autoconfiguring BrowserKit client
Given a YAML services file containing:
"""
services:
_defaults:
autowire: true
autoconfigure: true
App\Tests\SomeContext: ~
"""
When I run Behat
Then it should pass
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Behat\Mink\Mink;
use Behat\Mink\Session;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -19,7 +20,14 @@ final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements
{
public function load(array $configs, ContainerBuilder $container): void
{
$this->provideMinkIntegration($container);
if (class_exists(Mink::class)) {
$this->provideMinkIntegration($container);
}

if (class_exists(Client::class)) {
$this->provideBrowserKitIntegration($container);
}

$this->registerBehatContainer($container);

$container->registerForAutoconfiguration(Context::class)->addTag('fob.context');
Expand All @@ -44,6 +52,11 @@ private function registerBehatContainer(ContainerBuilder $container): void
$container->setDefinition('behat.service_container', $behatServiceContainerDefinition);
}

private function provideBrowserKitIntegration(ContainerBuilder $container): void
{
$container->setAlias(Client::class, 'test.client');
}

private function provideMinkIntegration(ContainerBuilder $container): void
{
$this->registerMink($container);
Expand Down
1 change: 0 additions & 1 deletion src/ServiceContainer/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace FriendsOfBehat\SymfonyExtension\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Behat\MinkExtension\ServiceContainer\MinkExtension;
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
Expand Down

0 comments on commit 9d5b241

Please sign in to comment.