Skip to content

Commit 0f906e1

Browse files
committed
Browser integration
1 parent c9e5db9 commit 0f906e1

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

features/browserkit_integration/browserkit_integration.feature

+88-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ Feature: BrowserKit integration
2222
When I visit the page "/hello-world"
2323
Then I should see "Hello world!" on the page
2424
"""
25+
26+
Scenario: Injecting KernelBrowser manually
27+
Given a YAML services file containing:
28+
"""
29+
services:
30+
App\Tests\SomeContext:
31+
public: true
32+
arguments:
33+
- '@test.client'
34+
"""
2535
And a context file "tests/SomeContext.php" containing:
2636
"""
2737
<?php
@@ -31,14 +41,13 @@ Feature: BrowserKit integration
3141
use Behat\Behat\Context\Context;
3242
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
3343
use Psr\Container\ContainerInterface;
34-
use Symfony\Component\BrowserKit\AbstractBrowser;
35-
use Symfony\Component\BrowserKit\Client;
44+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
3645
3746
final class SomeContext implements Context {
38-
/** @var Client|AbstractBrowser */
47+
/** @var KernelBrowser */
3948
private $client;
4049
41-
public function __construct($client)
50+
public function __construct(KernelBrowser $client)
4251
{
4352
$this->client = $client;
4453
}
@@ -56,31 +65,97 @@ Feature: BrowserKit integration
5665
}
5766
}
5867
"""
68+
When I run Behat
69+
Then it should pass
5970

60-
Scenario: Injecting BrowserKit client
71+
Scenario: Autowiring and autoconfiguring KernelBrowser client
6172
Given a YAML services file containing:
6273
"""
6374
services:
64-
App\Tests\SomeContext:
65-
public: true
66-
arguments:
67-
- '@test.client'
75+
_defaults:
76+
autowire: true
77+
autoconfigure: true
78+
79+
App\Tests\SomeContext: ~
6880
"""
81+
And a context file "tests/SomeContext.php" containing:
82+
"""
83+
<?php
84+
85+
namespace App\Tests;
86+
87+
use Behat\Behat\Context\Context;
88+
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
89+
use Psr\Container\ContainerInterface;
90+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
91+
92+
final class SomeContext implements Context {
93+
/** @var KernelBrowser */
94+
private $client;
95+
96+
public function __construct(KernelBrowser $client)
97+
{
98+
$this->client = $client;
99+
}
100+
101+
/** @When I visit the page :page */
102+
public function visitPage(string $page): void
103+
{
104+
$this->client->request('GET', $page);
105+
}
106+
107+
/** @Then I should see :content on the page */
108+
public function shouldSeeContentOnPage(string $content): void
109+
{
110+
assert(false !== strpos($this->client->getResponse()->getContent(), $content));
111+
}
112+
}
113+
"""
69114
When I run Behat
70115
Then it should pass
71116

72-
Scenario: Autowiring and autoconfiguring BrowserKit client
117+
Scenario: Autowiring and autoconfiguring HttpKernelBrowser client
73118
Given a YAML services file containing:
74119
"""
75120
services:
76121
_defaults:
77122
autowire: true
78123
autoconfigure: true
79124
80-
bind:
81-
$client: "@test.client"
82-
83125
App\Tests\SomeContext: ~
84126
"""
127+
And a context file "tests/SomeContext.php" containing:
128+
"""
129+
<?php
130+
131+
namespace App\Tests;
132+
133+
use Behat\Behat\Context\Context;
134+
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
135+
use Psr\Container\ContainerInterface;
136+
use Symfony\Component\HttpKernel\HttpKernelBrowser;
137+
138+
final class SomeContext implements Context {
139+
/** @var HttpKernelBrowser */
140+
private $client;
141+
142+
public function __construct(HttpKernelBrowser $client)
143+
{
144+
$this->client = $client;
145+
}
146+
147+
/** @When I visit the page :page */
148+
public function visitPage(string $page): void
149+
{
150+
$this->client->request('GET', $page);
151+
}
152+
153+
/** @Then I should see :content on the page */
154+
public function shouldSeeContentOnPage(string $content): void
155+
{
156+
assert(false !== strpos($this->client->getResponse()->getContent(), $content));
157+
}
158+
}
159+
"""
85160
When I run Behat
86161
Then it should pass

src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
use Behat\Mink\Session;
1010
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
1111
use FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension;
12+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1213
use Symfony\Component\BrowserKit\Client;
1314
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Reference;
1819
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20+
use Symfony\Component\HttpKernel\HttpKernelBrowser;
1921
use Symfony\Component\HttpKernel\KernelInterface;
2022

2123
final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements CompilerPassInterface
@@ -73,11 +75,13 @@ private function registerDriverBehatContainer(ContainerBuilder $container): void
7375

7476
private function provideBrowserKitIntegration(ContainerBuilder $container): void
7577
{
76-
if (!class_exists(Client::class) || !$container->has('test.client')) {
78+
if (!$container->has('test.client')) {
7779
return;
7880
}
7981

80-
$container->setAlias(Client::class, 'test.client');
82+
foreach ([Client::class, KernelBrowser::class, HttpKernelBrowser::class] as $class) {
83+
$container->setAlias($class, 'test.client');
84+
}
8185
}
8286

8387
private function provideMinkIntegration(ContainerBuilder $container): void

0 commit comments

Comments
 (0)