Skip to content

Commit

Permalink
Merge branch '2.1'
Browse files Browse the repository at this point in the history
* 2.1:
  Browser integration
  • Loading branch information
pamil committed Apr 4, 2020
2 parents c27497f + 0f906e1 commit d881779
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
101 changes: 88 additions & 13 deletions features/browserkit_integration/browserkit_integration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Feature: BrowserKit integration
When I visit the page "/hello-world"
Then I should see "Hello world!" on the page
"""

Scenario: Injecting KernelBrowser manually
Given a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- '@test.client'
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php
Expand All @@ -31,14 +41,13 @@ Feature: BrowserKit integration
use Behat\Behat\Context\Context;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use Psr\Container\ContainerInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Client;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
final class SomeContext implements Context {
/** @var Client|AbstractBrowser */
/** @var KernelBrowser */
private $client;
public function __construct($client)
public function __construct(KernelBrowser $client)
{
$this->client = $client;
}
Expand All @@ -56,31 +65,97 @@ Feature: BrowserKit integration
}
}
"""
When I run Behat
Then it should pass

Scenario: Injecting BrowserKit client
Scenario: Autowiring and autoconfiguring KernelBrowser client
Given a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- '@test.client'
_defaults:
autowire: true
autoconfigure: true
App\Tests\SomeContext: ~
"""
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\Bundle\FrameworkBundle\KernelBrowser;
final class SomeContext implements Context {
/** @var KernelBrowser */
private $client;
public function __construct(KernelBrowser $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));
}
}
"""
When I run Behat
Then it should pass

Scenario: Autowiring and autoconfiguring BrowserKit client
Scenario: Autowiring and autoconfiguring HttpKernelBrowser client
Given a YAML services file containing:
"""
services:
_defaults:
autowire: true
autoconfigure: true
bind:
$client: "@test.client"
App\Tests\SomeContext: ~
"""
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\HttpKernel\HttpKernelBrowser;
final class SomeContext implements Context {
/** @var HttpKernelBrowser */
private $client;
public function __construct(HttpKernelBrowser $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));
}
}
"""
When I run Behat
Then it should pass
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Behat\Mink\Session;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\KernelInterface;

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

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

$container->setAlias(Client::class, 'test.client');
foreach ([Client::class, KernelBrowser::class, HttpKernelBrowser::class] as $class) {
$container->setAlias($class, 'test.client');
}
}

private function provideMinkIntegration(ContainerBuilder $container): void
Expand Down

0 comments on commit d881779

Please sign in to comment.