diff --git a/features/mink_integration.feature b/features/mink_integration.feature new file mode 100644 index 0000000..7dad88e --- /dev/null +++ b/features/mink_integration.feature @@ -0,0 +1,107 @@ +Feature: Mink integration + + Background: + Given a working Symfony application with SymfonyExtension configured + And a Behat configuration containing: + """ + default: + extensions: + Behat\MinkExtension: + base_url: "http://localhost:8080/" + default_session: symfony + sessions: + symfony: + symfony: ~ + suites: + default: + contexts: + - App\Tests\SomeContext + """ + And a feature file containing: + """ + Feature: + Scenario: + When I visit the page "/hello-world" + Then I should see "Hello world!" on the page + And the base url from Mink parameters should be "http://localhost:8080/" + + # Doubling the scenario to account for some weird error connected to Mink's session + Scenario: + When I visit the page "/hello-world" + Then I should see "Hello world!" on the page + And the base url from Mink parameters should be "http://localhost:8080/" + """ + And a context file "tests/SomeContext.php" containing: + """ + session = $session; + $this->parameters = $minkParameters; + } + + /** @When I visit the page :page */ + public function visitPage(string $page): void + { + $this->session->visit($page); + } + + /** @Then I should see :content on the page */ + public function shouldSeeContentOnPage(string $content): void + { + assert(false !== strpos($this->session->getPage()->getContent(), $content)); + } + + /** @Then the base url from Mink parameters should be :expected */ + public function baseUrlShouldBe(string $expected): void + { + assert(isset($this->parameters['base_url'])); + assert($this->parameters['base_url'] === $expected); + } + } + """ + + Scenario: Injecting Mink session and Mink parameters + Given a YAML services file containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - '@behat.mink.default_session' + - '@behat.mink.parameters' + """ + When I run Behat + Then it should pass + + Scenario: Autowiring and autoconfiguring Mink session and Mink parameters + Given a YAML services file containing: + """ + services: + _defaults: + autowire: true + autoconfigure: true + + App\Tests\SomeContext: ~ + """ + When I run Behat + Then it should pass diff --git a/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php b/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php index 36091d1..6dc032f 100644 --- a/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php +++ b/src/Bundle/DependencyInjection/FriendsOfBehatSymfonyExtensionExtension.php @@ -24,6 +24,9 @@ final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements $container ->registerForAutoconfiguration(Context::class) ->addTag('fob.context') + ->setBindings([ + '$minkParameters' => new Reference('behat.mink.parameters'), + ]) ; } diff --git a/tests/Behat/Context/TestContext.php b/tests/Behat/Context/TestContext.php index f626634..95e91d9 100644 --- a/tests/Behat/Context/TestContext.php +++ b/tests/Behat/Context/TestContext.php @@ -91,13 +91,23 @@ CON namespace App; -use Symfony\Component\HttpKernel\Kernel as HttpKernel; -use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Kernel as HttpKernel; +use Symfony\Component\Routing\RouteCollectionBuilder; class Kernel extends HttpKernel { - public function registerBundles() + use MicroKernelTrait; + + public function helloWorld(): Response + { + return new Response('Hello world!'); + } + + public function registerBundles(): iterable { return [ new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), @@ -105,17 +115,20 @@ class Kernel extends HttpKernel ]; } - public function registerContainerConfiguration(LoaderInterface $loader) + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { - $loader->load(function (ContainerBuilder $container): void { - $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', - 'secret' => 'Pigeon', - ]); - }); + $container->loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); $loader->load(__DIR__ . '/../config/services.yaml'); } + + protected function configureRoutes(RouteCollectionBuilder $routes) + { + $routes->add('/hello-world', 'kernel::helloWorld'); + } } CON ); @@ -151,25 +164,6 @@ CON self::$filesystem->dumpFile($mainConfigFile, Yaml::dump($mainBehatConfiguration)); } - - /** - * @Given /^a Behat configuration with the minimal working configuration for MinkExtension$/ - */ - public function thereIsConfigurationWithMinimalWorkingConfigurationForMinkExtension(): void - { - $this->thereIsConfiguration(<<<'CON' -default: - extensions: - Behat\MinkExtension: - base_url: "http://localhost:8080/" - default_session: symfony - sessions: - symfony: - symfony: ~ -CON - ); - } - /** * @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/ */