From 933333991b0da956036093414664dadb46052e87 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 27 Nov 2022 10:10:12 +0000 Subject: [PATCH] Add remarks regarding accessing the latest instances of services in the driver's container after a reboot --- DOCUMENTATION.md | 2 ++ .../resetting_service_container_state.feature | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 64b43cf..a8888dd 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -372,6 +372,8 @@ In your contexts, you can inject the `behat.driver.service_container` service (o * Both kernels and containers will be shut down and rebooted after every single scenario and/or example (for scenario outlines), in order to provide a clean separation between scenarios. * When making multiple Mink requests within a single scenario, the second kernel and container (`behat.driver.service_container`) needs to be reset to provide a clean state for the second and every additional request. This reset will happen immediately before the second and any subsequent request is handed to the kernel. So, while in general it is possible to inspect the driver's container state _after_ requests, setting it up (bringing it into desired state) easily is only possible for the _first_ request. +In order to get the right (current) instances of services after such a reset has happened, make sure you call `ContainerInterface::get()` and related methods again after the request. Do not fetch services from the driver's container e. g. in your context constructors, since that will not give you the latest instances of those services. + # Configuration reference By default, if no confguration is passed, _SymfonyExtension_ will try its best to guess it. diff --git a/features/mink_integration/resetting_service_container_state.feature b/features/mink_integration/resetting_service_container_state.feature index fb2473c..4ad5e1b 100644 --- a/features/mink_integration/resetting_service_container_state.feature +++ b/features/mink_integration/resetting_service_container_state.feature @@ -163,11 +163,21 @@ Feature: Resetting the driver's service container in the right places Feature: Scenario: Given the counter service is zeroed + # Increment the counter before the first step, so that we can + # really observe a difference (i. e. the container being reset + # and the answer being "1" instead of "2") after the second request. + And I increment the counter + When I visit the page "/hello-world" + Then the counter service should return 2 + # This will reset the driver's container: When I visit the page "/hello-world" Then the counter service should return 1 - # This will reset the driver's container, so we will see "1" again - When I visit the page "/hello-world" - Then the counter service should return 1 + # Remark: Our context had the driver's container constructor-injected + # and thus that container instance cannot/did not change (!). However, + # due to the way things work internally in Symfony (related to lazy + # loading? using the test container?), after the Kernel reboot we + # still get the _current_ service instances from that container, at least + # as long as we get() the services again. """ When I run Behat Then it should pass