Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] KernelTestCase resets internal state on tearDown #45414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2022

Conversation

core23
Copy link
Contributor

@core23 core23 commented Feb 13, 2022

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

When using the KernelTestCase for multiple different test kernels, the KernelTestCase::$class is not reseted after the test has finished. All other class variables are set to the initial state, so this change should be a bugfix.

@carsonbot carsonbot added this to the 6.1 milestone Feb 13, 2022
@carsonbot carsonbot changed the title KernelTestCase resets internal state on tearDown [FrameworkBundle] KernelTestCase resets internal state on tearDown Feb 13, 2022
@nicolas-grekas
Copy link
Member

Did you experience any nasty side effect of this not being reset? Can you add a corresponding test case?

@nicolas-grekas nicolas-grekas modified the milestones: 6.1, 4.4 Feb 13, 2022
@core23
Copy link
Contributor Author

core23 commented Feb 13, 2022

Did you experience any nasty side effect of this not being reset?

IMHO it should only have a minimum performance impact, because the name is resolved for every test:

if (null === static::$class) {
static::$class = static::getKernelClass();
}

@nicolas-grekas
Copy link
Member

It could have a performance impact. That's why I'm asking. Why do you want to reset this property, if everything is fine when it is not reset?

@core23
Copy link
Contributor Author

core23 commented Feb 14, 2022

I have some projects that will perform web tests with different kernel configuration. This can also be the case, if you provide a bundle with optional dependencies that will result in a slightly different service configuration.

At the moment, I have to manuell reset the ::$class after every test with a local tearDown method. When I don't do this, the first (randomly selected) test will reserve the kernel class and all other test will fail, because they expect a different kernel class.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, that's what I was missing.

@nicolas-grekas
Copy link
Member

Thank you @core23.

@nicolas-grekas nicolas-grekas merged commit 3e9a6e3 into symfony:4.4 Feb 18, 2022
@core23 core23 deleted the reset-kernel-test branch February 18, 2022 09:54
This was referenced Feb 28, 2022
@alexander-schranz
Copy link
Contributor

This currently is now breaking in our cases the tests as we are defining the static::$class once at the top of the testcases: https://github.com/sulu/sulu/blob/b45f91ff36b468d200b254f894368cb0c3380671/src/Sulu/Bundle/MediaBundle/Tests/Functional/Media/Storage/S3StorageTest.php#L22

And now as it is set to null on teardown all tests after the first tests are failing.

@mwalczak
Copy link

mwalczak commented Mar 2, 2022

Can this be related?
After update 6.0.3->6.0.5

LogicException : Could not find service "test.service_container". Try updating the "framework.test" config to "true".
 /var/www/symfony/vendor/symfony/framework-bundle/Test/KernelTestCase.php:98
 /var/www/symfony/tests/Controller/DefaultControllerTest.php:70
 
 Caused by
 Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "kernel" service is synthetic, it needs to be set at boot time before it can be used.

I have already framework.test=true.

@nicolas-grekas
Copy link
Member

@mwalczak nope, see #45580 for this issue.

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Mar 2, 2022

I could workaround my issue by changing:

-    protected static $class = S3Kernel::class;

+    protected static function getKernelClass(): string
+    {
+        return S3Kernel::class;
+    }

EDIT: for prefer lowest tests I had to readd the static $class so now both is required in our case:

+    protected static $class = S3Kernel::class;
+
+    protected static function getKernelClass(): string
+    {
+        return S3Kernel::class;
+    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants