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

Command icon 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

godotislate created an issue. See original summary.

godotislate’s picture

Status: Active » Needs review
godotislate’s picture

Issue summary: View changes
Issue tags: +Needs CR

This 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.

joachim’s picture

Which 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.

catch’s picture

This 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.

godotislate’s picture

I'm fairly certain I've seen test modules that define one-off routes for a single test. I'll look for an example.

godotislate’s picture

Here's one: the basic_auth_test module. Its only use AFAICT is to provide routes for Drupal\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.

godotislate’s picture

I know there are a lot of test routes inside test modules, but those are often useful in other circumstances

I 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.

joachim’s picture

> 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.

godotislate’s picture

Issue tags: -Needs CR

Since this has missed 11.4, added CR https://www.drupal.org/node/3605544.

smustgrave’s picture

Should the existing test you mentioned in #8 be converted?

godotislate’s picture

Per #6, I'm thinking no, a conversion or a batch of conversions can be do in their own issues.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

10-4

I ran the test-only locally since I can't run the job anymore (thank you gitlab)

Behat\Mink\Exception\ExpectationException: Current response status code is 500, but 200 expected.
/var/www/html/vendor/behat/mink/src/WebAssert.php:888
/var/www/html/vendor/behat/mink/src/WebAssert.php:145
/var/www/html/core/tests/Drupal/KernelTests/KernelTestHttpRequestTest.php:111


Test code or tested code printed unexpected output: Uncaught PHP Exception Error: "Call to private method Drupal\Core\Routing\AttributeRouteDiscovery::createRouteCollection() from scope Drupal\KernelTests\KernelTestAttributeRouteDiscovery" at /var/www/html/core/tests/Drupal/KernelTests/KernelTestAttributeRouteDiscovery.php line 22

MR is small and don't see any open threads. No objections.