From 19baa4e10245a16cbd9e3a40a509d36aab51515c Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Tue, 5 Mar 2024 09:13:40 +0000 Subject: [PATCH] Issue #3421482 by andypost, longwave, smustgrave: Remove ContainerAwareTrait from test classes --- .../service_provider_test.services.yml | 1 - .../service_provider_test/src/TestClass.php | 15 ++++----- core/phpstan-baseline.neon | 32 ------------------- .../Controller/ControllerResolverTest.php | 17 ++++++++-- .../Core/Utility/CallableResolverTest.php | 13 ++++++-- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/core/modules/system/tests/modules/service_provider_test/service_provider_test.services.yml b/core/modules/system/tests/modules/service_provider_test/service_provider_test.services.yml index 42bb2799e1bf..614e90029ace 100644 --- a/core/modules/system/tests/modules/service_provider_test/service_provider_test.services.yml +++ b/core/modules/system/tests/modules/service_provider_test/service_provider_test.services.yml @@ -5,5 +5,4 @@ services: class: Drupal\service_provider_test\TestClass tags: - { name: needs_destruction } - parent: container.trait arguments: ['@state'] diff --git a/core/modules/system/tests/modules/service_provider_test/src/TestClass.php b/core/modules/system/tests/modules/service_provider_test/src/TestClass.php index 14de650795b7..b99ec3731198 100644 --- a/core/modules/system/tests/modules/service_provider_test/src/TestClass.php +++ b/core/modules/system/tests/modules/service_provider_test/src/TestClass.php @@ -4,16 +4,12 @@ use Drupal\Core\State\StateInterface; use Drupal\Core\DestructableInterface; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; -class TestClass implements EventSubscriberInterface, DestructableInterface, ContainerAwareInterface { - - use ContainerAwareTrait; +class TestClass implements EventSubscriberInterface, DestructableInterface { /** * The state keyvalue collection. @@ -43,11 +39,12 @@ public function onKernelRequestTest(RequestEvent $event) { * Flags the response in case a rebuild indicator is used. */ public function onKernelResponseTest(ResponseEvent $event) { - if ($this->container->hasParameter('container_rebuild_indicator')) { - $event->getResponse()->headers->set('container_rebuild_indicator', $this->container->getParameter('container_rebuild_indicator')); + $container = \Drupal::getContainer(); + if ($container->hasParameter('container_rebuild_indicator')) { + $event->getResponse()->headers->set('container_rebuild_indicator', $container->getParameter('container_rebuild_indicator')); } - if ($this->container->hasParameter('container_rebuild_test_parameter')) { - $event->getResponse()->headers->set('container_rebuild_test_parameter', $this->container->getParameter('container_rebuild_test_parameter')); + if ($container->hasParameter('container_rebuild_test_parameter')) { + $event->getResponse()->headers->set('container_rebuild_test_parameter', $container->getParameter('container_rebuild_test_parameter')); } } diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index ee57eb05b4b0..0037711f1db5 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -1623,22 +1623,6 @@ parameters: count: 1 path: modules/system/tests/modules/plugin_test/src/Plugin/TestPluginManager.php - - - message: """ - #^Class Drupal\\\\service_provider_test\\\\TestClass implements deprecated interface Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface\\: - since Symfony 6\\.4, use dependency injection instead$# - """ - count: 1 - path: modules/system/tests/modules/service_provider_test/src/TestClass.php - - - - message: """ - #^Usage of deprecated trait Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareTrait in class Drupal\\\\service_provider_test\\\\TestClass\\: - since Symfony 6\\.4, use dependency injection instead$# - """ - count: 1 - path: modules/system/tests/modules/service_provider_test/src/TestClass.php - - message: "#^Access to an undefined property Drupal\\\\Tests\\\\system\\\\Functional\\\\FileTransfer\\\\TestFileTransfer\\:\\:\\$connection\\.$#" count: 5 @@ -2423,14 +2407,6 @@ parameters: count: 1 path: tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php - - - message: """ - #^Usage of deprecated trait Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareTrait in class Drupal\\\\Tests\\\\Core\\\\Controller\\\\MockContainerAware\\: - since Symfony 6\\.4, use dependency injection instead$# - """ - count: 1 - path: tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php - - message: """ #^Call to deprecated method expectError\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\: @@ -2569,14 +2545,6 @@ parameters: count: 1 path: tests/Drupal/Tests/Core/Utility/CallableResolverTest.php - - - message: """ - #^Usage of deprecated trait Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareTrait in class Drupal\\\\Tests\\\\Core\\\\Utility\\\\MockContainerAware\\: - since Symfony 6\\.4, use dependency injection instead$# - """ - count: 1 - path: tests/Drupal/Tests/Core/Utility/CallableResolverTest.php - - message: """ #^Call to deprecated method getConfig\\(\\) of class GuzzleHttp\\\\ClientInterface\\: diff --git a/core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php b/core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php index 7ba733c18fa3..d58ff9d2ddb4 100644 --- a/core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php @@ -12,7 +12,6 @@ use Drupal\Tests\UnitTestCase; use Psr\Http\Message\ServerRequestInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -221,9 +220,23 @@ public function getResult() { } class MockContainerAware implements ContainerAwareInterface { - use ContainerAwareTrait; + + /** + * The service container. + */ + protected ContainerInterface $container; + + /** + * Sets the service container. + */ + public function setContainer(?ContainerInterface $container): void { + $this->container = $container; + } public function getResult() { + if (empty($this->container)) { + throw new \Exception('Container was not injected.'); + } return 'This is container aware.'; } diff --git a/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php b/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php index 66da0ca7eda6..eedf6a3d7151 100644 --- a/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php @@ -10,7 +10,6 @@ use Drupal\Core\Utility\CallableResolver; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -252,7 +251,17 @@ class NoMethodCallable { class MockContainerAware implements ContainerAwareInterface { - use ContainerAwareTrait; + /** + * The service container. + */ + protected ContainerInterface $container; + + /** + * Sets the service container. + */ + public function setContainer(?ContainerInterface $container): void { + $this->container = $container; + } public function getResult($suffix) { if (empty($this->container)) { -- GitLab