Skip to content

Commit 509b2e2

Browse files
authored
feature #82 Provide simple BrowserKit integration (pamil)
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
2 parents 2f038b2 + ab61dba commit 509b2e2

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ install:
2020
- composer require symfony/dependency-injection:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
2121
- composer require symfony/http-kernel:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
2222
- composer require symfony/proxy-manager-bridge:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
23+
- composer require --dev symfony/browser-kit:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
2324
- composer require --dev symfony/framework-bundle:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
2425
- composer require --dev symfony/yaml:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist
2526
- composer update --prefer-dist

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"friends-of-behat/service-container-extension": "^1.0",
2626
"phpstan/phpstan-shim": "^0.11",
2727
"sylius-labs/coding-standard": "^3.0",
28+
"symfony/browser-kit": "^3.4|^4.2",
2829
"symfony/framework-bundle": "^3.4|^4.2",
2930
"symfony/yaml": "^3.4|^4.2"
3031
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Feature: BrowserKit integration
2+
3+
Background:
4+
Given a working Symfony application with SymfonyExtension configured
5+
And a Behat configuration containing:
6+
"""
7+
default:
8+
suites:
9+
default:
10+
contexts:
11+
- App\Tests\SomeContext
12+
"""
13+
And a feature file containing:
14+
"""
15+
Feature:
16+
Scenario:
17+
When I visit the page "/hello-world"
18+
Then I should see "Hello world!" on the page
19+
20+
# Doubling the scenario to account for some weird error
21+
Scenario:
22+
When I visit the page "/hello-world"
23+
Then I should see "Hello world!" on the page
24+
"""
25+
And a context file "tests/SomeContext.php" containing:
26+
"""
27+
<?php
28+
29+
namespace App\Tests;
30+
31+
use Behat\Behat\Context\Context;
32+
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
33+
use Psr\Container\ContainerInterface;
34+
use Symfony\Component\BrowserKit\Client;
35+
36+
final class SomeContext implements Context {
37+
private $client;
38+
39+
public function __construct(Client $client)
40+
{
41+
$this->client = $client;
42+
}
43+
44+
/** @When I visit the page :page */
45+
public function visitPage(string $page): void
46+
{
47+
$this->client->request('GET', $page);
48+
}
49+
50+
/** @Then I should see :content on the page */
51+
public function shouldSeeContentOnPage(string $content): void
52+
{
53+
assert(false !== strpos($this->client->getResponse()->getContent(), $content));
54+
}
55+
}
56+
"""
57+
58+
Scenario: Injecting BrowserKit client
59+
Given a YAML services file containing:
60+
"""
61+
services:
62+
App\Tests\SomeContext:
63+
public: true
64+
arguments:
65+
- '@test.client'
66+
"""
67+
When I run Behat
68+
Then it should pass
69+
70+
Scenario: Autowiring and autoconfiguring BrowserKit client
71+
Given a YAML services file containing:
72+
"""
73+
services:
74+
_defaults:
75+
autowire: true
76+
autoconfigure: true
77+
78+
App\Tests\SomeContext: ~
79+
"""
80+
When I run Behat
81+
Then it should pass

src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Behat\Mink\Mink;
99
use Behat\Mink\Session;
1010
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
11+
use Symfony\Component\BrowserKit\Client;
1112
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1213
use Symfony\Component\DependencyInjection\ContainerBuilder;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -27,6 +28,8 @@ public function load(array $configs, ContainerBuilder $container): void
2728

2829
public function process(ContainerBuilder $container): void
2930
{
31+
$this->provideBrowserKitIntegration($container);
32+
3033
foreach ($container->findTaggedServiceIds('fob.context') as $serviceId => $attributes) {
3134
$serviceDefinition = $container->findDefinition($serviceId);
3235

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

50+
private function provideBrowserKitIntegration(ContainerBuilder $container): void
51+
{
52+
if (!class_exists(Client::class) || !$container->has('test.client')) {
53+
return;
54+
}
55+
56+
$container->setAlias(Client::class, 'test.client');
57+
}
58+
4759
private function provideMinkIntegration(ContainerBuilder $container): void
4860
{
61+
if (!class_exists(Mink::class)) {
62+
return;
63+
}
64+
4965
$this->registerMink($container);
5066
$this->registerMinkDefaultSession($container);
5167
$this->registerMinkParameters($container);

0 commit comments

Comments
 (0)