From 0f906e1156546095f4287be7cd5ceede2d3e22cc Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Sat, 4 Apr 2020 22:07:00 +0200 Subject: [PATCH] Browser integration --- .../browserkit_integration.feature | 109 +++++++++++++++--- ...riendsOfBehatSymfonyExtensionExtension.php | 8 +- 2 files changed, 98 insertions(+), 19 deletions(-) diff --git a/features/browserkit_integration/browserkit_integration.feature b/features/browserkit_integration/browserkit_integration.feature index 2e0a040..12b6f92 100644 --- a/features/browserkit_integration/browserkit_integration.feature +++ b/features/browserkit_integration/browserkit_integration.feature @@ -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: """ client = $client; } @@ -56,20 +65,10 @@ Feature: BrowserKit integration } } """ - - 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 + Scenario: Autowiring and autoconfiguring KernelBrowser client Given a YAML services file containing: """ services: @@ -77,10 +76,86 @@ Feature: BrowserKit integration autowire: true autoconfigure: true - bind: - $client: "@test.client" + App\Tests\SomeContext: ~ + """ + And a context file "tests/SomeContext.php" containing: + """ + 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 HttpKernelBrowser client + Given a YAML services file containing: + """ + services: + _defaults: + autowire: true + autoconfigure: true App\Tests\SomeContext: ~ """ + And a context file "tests/SomeContext.php" containing: + """ + 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 diff --git a/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php b/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php index cb8c4ff..88cfeac 100644 --- a/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php +++ b/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php @@ -9,6 +9,7 @@ use Behat\Mink\Mink; 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; @@ -16,6 +17,7 @@ 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 @@ -73,11 +75,13 @@ final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements 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