Skip to content

Commit

Permalink
feature FriendsOfBehat#82 Provide simple BrowserKit integration (pamil)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.1-dev branch.

Discussion
----------

There's already a service for BrowserKit's client, this PR adds acceptance scenarios and autowires it. The architecture might need some rework after introducing Panther support.

Commits
-------

ab61dba Provide simple BrowserKit integration
  • Loading branch information
lchrusciel authored Sep 9, 2019
2 parents 2f038b2 + ab61dba commit 509b2e2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ install:
- composer require symfony/dependency-injection:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer require symfony/http-kernel:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer require symfony/proxy-manager-bridge:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer require --dev symfony/browser-kit:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer require --dev symfony/framework-bundle:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer require --dev symfony/yaml:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
- composer update --prefer-dist
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"friends-of-behat/service-container-extension": "^1.0",
"phpstan/phpstan-shim": "^0.11",
"sylius-labs/coding-standard": "^3.0",
"symfony/browser-kit": "^3.4|^4.2",
"symfony/framework-bundle": "^3.4|^4.2",
"symfony/yaml": "^3.4|^4.2"
},
Expand Down
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 @@ -27,6 +28,8 @@ public function load(array $configs, ContainerBuilder $container): void

public function process(ContainerBuilder $container): void
{
$this->provideBrowserKitIntegration($container);

foreach ($container->findTaggedServiceIds('fob.context') as $serviceId => $attributes) {
$serviceDefinition = $container->findDefinition($serviceId);

Expand All @@ -44,8 +47,21 @@ private function registerBehatContainer(ContainerBuilder $container): void
$container->setDefinition('behat.service_container', $behatServiceContainerDefinition);
}

private function provideBrowserKitIntegration(ContainerBuilder $container): void
{
if (!class_exists(Client::class) || !$container->has('test.client')) {
return;
}

$container->setAlias(Client::class, 'test.client');
}

private function provideMinkIntegration(ContainerBuilder $container): void
{
if (!class_exists(Mink::class)) {
return;
}

$this->registerMink($container);
$this->registerMinkDefaultSession($container);
$this->registerMinkParameters($container);
Expand Down

0 comments on commit 509b2e2

Please sign in to comment.