Problem/Motivation
#3584793: Use PHP attributes for form route discovery makes it possible to use the Symfony Route attribute applied to Controller classes and their methods to define routes.
#3390193: Add a drupalGet() method to KernelTestBase makes it possible to make HTTP requests in kernel tests using drupalGet().
With these two things in mind, it would be useful for kernel test classes to be able to define their own routes. Often functional tests are used to check content in a controller, and often such tests also make use of database storage to set and retrieve values between the test method and the controller. In certain cases, this could be simplified if the controller method was in the kernel class itself, and values could be set and retrieved between test method and controller method via class properites
Steps to reproduce
Proposed resolution
In KernelTestBase, register an event subscriber (extend AttributeRouteDiscovery) that discovers routes from the attributes in the kernel test class
Remaining tasks
Add change record (or add to https://www.drupal.org/node/3324758 if in before 11.4).
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3586832
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
godotislateMR is ready: https://git.drupalcode.org/project/drupal/-/merge_requests/15571
Comment #4
godotislateThis will need a CR, but going to leave that open for now, because if we get this in for 11.4, it's probably better that we add to https://www.drupal.org/node/3324758.
Comment #5
joachim commentedWhich routes are you thinking would benefit from this?
I know there are a lot of test routes inside test modules, but those are often useful in other circumstances -- e.g. I've been reusing some of them for the drupalGet() in core conversions.
Comment #6
catchThis is a great idea - would be good to have an example of an existing test we can convert - not in this issue because we're still working through kernel http request things but for an example of the sort of boilerplate we should be able to remove.
Comment #7
godotislateI'm fairly certain I've seen test modules that define one-off routes for a single test. I'll look for an example.
Comment #8
godotislateHere's one: the
basic_auth_testmodule. Its only use AFAICT is to provide routes forDrupal\Tests\basic_auth\Functional\BasicAuthTest::testControllerNotCalledBeforeAuth()that aren't used anywhere else. I haven't checked the whole test class to see if all the methods could be converted to a Kernel Test, but that one method can at least.Comment #9
godotislateI haven't completely thought this through, but one possible way to be able use the same route across multiple kernel tests would be to put the
#[Route]method in a trait.Comment #10
joachim commented> but one possible way to be able use the same route across multiple kernel tests would be to put the #[Route] method in a trait.
True.
Though one drawback of this occurs to me -- if the route is defined in a kernel test, then you can't ever test that route manually. That's sometimes useful when developing or debugging tests.
Comment #11
godotislateSince this has missed 11.4, added CR https://www.drupal.org/node/3605544.
Comment #12
smustgrave commentedShould the existing test you mentioned in #8 be converted?
Comment #13
godotislatePer #6, I'm thinking no, a conversion or a batch of conversions can be do in their own issues.
Comment #14
smustgrave commented10-4
I ran the test-only locally since I can't run the job anymore (thank you gitlab)
MR is small and don't see any open threads. No objections.