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,56 @@ 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: + """ + 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: @@ -77,10 +122,40 @@ 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 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\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\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 @@ 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