diff --git a/features/mink_integration/mink_integration_with_context_initializer.feature b/features/mink_integration/mink_integration_with_context_initializer.feature new file mode 100644 index 0000000..e2264c6 --- /dev/null +++ b/features/mink_integration/mink_integration_with_context_initializer.feature @@ -0,0 +1,78 @@ +Feature: Mink integration with context initializer + + Scenario: Passing Mink instance and parameters through context initializer + 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: + """ + mink = $mink; + } + + public function setMinkParameters(array $minkParameters): void + { + $this->parameters = $minkParameters; + } + + /** @When I visit the page :page */ + public function visitPage(string $page): void + { + $this->mink->getSession()->visit($page); + } + + /** @Then I should see :content on the page */ + public function shouldSeeContentOnPage(string $content): void + { + assert(false !== strpos($this->mink->getSession()->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); + } + } + """ + When I run Behat + Then it should pass diff --git a/features/mink_integration/mink_integration.feature b/features/mink_integration/mink_integration_with_dependency_injection.feature similarity index 98% rename from features/mink_integration/mink_integration.feature rename to features/mink_integration/mink_integration_with_dependency_injection.feature index 7dad88e..808e413 100644 --- a/features/mink_integration/mink_integration.feature +++ b/features/mink_integration/mink_integration_with_dependency_injection.feature @@ -1,4 +1,4 @@ -Feature: Mink integration +Feature: Mink integration with dependency injection Background: Given a working Symfony application with SymfonyExtension configured diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index fc61b20..fe20b2e 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -88,6 +88,7 @@ public function load(ContainerBuilder $container, array $config): void public function process(ContainerBuilder $container): void { + $this->processEnvironmentHandler($container); } private function registerMinkDriver(ExtensionManager $extensionManager): void @@ -139,10 +140,6 @@ private function loadEnvironmentHandler(ContainerBuilder $container): void ]); $definition->addTag(EnvironmentExtension::HANDLER_TAG, ['priority' => 128]); - foreach ($container->findTaggedServiceIds(ContextExtension::INITIALIZER_TAG) as $serviceId => $tags) { - $definition->addMethodCall('registerContextInitializer', [$container->getDefinition($serviceId)]); - } - $container->setDefinition('fob_symfony.environment_handler.context_service', $definition); } @@ -248,4 +245,13 @@ private function autodiscoverBootstrap($bootstrap): ?string return is_string($bootstrap) ? $bootstrap : null; } + + private function processEnvironmentHandler(ContainerBuilder $container): void + { + $definition = $container->findDefinition('fob_symfony.environment_handler.context_service'); + + foreach ($container->findTaggedServiceIds(ContextExtension::INITIALIZER_TAG) as $serviceId => $tags) { + $definition->addMethodCall('registerContextInitializer', [$container->getDefinition($serviceId)]); + } + } }