From f263719f6eb966701e835697ae9592c320f4cfcd Mon Sep 17 00:00:00 2001
From: Jean Valverde <moimog33@gmail.com>
Date: Fri, 20 Sep 2024 11:04:56 +0100
Subject: [PATCH 1/8] Issue #3464388 by mogtofu33, wim leers: SDC
 *.component.yml metadata is cached aggressively, gets in the way of component
 development

---
 core/core.services.yml                        |  1 +
 .../Core/Theme/ComponentPluginManager.php     | 20 +++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/core/core.services.yml b/core/core.services.yml
index 54fbf1579777..c8434454bac0 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1884,6 +1884,7 @@ services:
       - '@file_system'
       - '@Drupal\Core\Theme\Component\SchemaCompatibilityChecker'
       - '@Drupal\Core\Theme\Component\ComponentValidator'
+      - '@keyvalue'
       - '%app.root%'
   Drupal\Core\Theme\ComponentPluginManager: '@plugin.manager.sdc'
   Drupal\Core\Template\Loader\ComponentLoader:
diff --git a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
index a0c93317699f..d51ede1ad067 100644
--- a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
+++ b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
@@ -12,6 +12,7 @@
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Extension\ThemeHandlerInterface;
 use Drupal\Core\File\FileSystemInterface;
+use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
 use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
 use Drupal\Core\Plugin\DefaultPluginManager;
 use Drupal\Core\Plugin\Factory\ContainerFactory;
@@ -62,6 +63,8 @@ class ComponentPluginManager extends DefaultPluginManager implements Categorizin
    *   The compatibility checker.
    * @param \Drupal\Core\Theme\Component\ComponentValidator $componentValidator
    *   The component validator.
+   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValueFactory
+   *   The key value factory.
    * @param string $appRoot
    *   The application root.
    */
@@ -75,6 +78,7 @@ public function __construct(
     protected FileSystemInterface $fileSystem,
     protected SchemaCompatibilityChecker $compatibilityChecker,
     protected ComponentValidator $componentValidator,
+    protected KeyValueFactoryInterface $keyValueFactory,
     protected string $appRoot,
   ) {
     // We are skipping the call to the parent constructor to avoid initializing
@@ -124,6 +128,22 @@ public function createInstance($plugin_id, array $configuration = []): Component
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getDefinitions() {
+    $definitions = parent::getDefinitions();
+
+    $development_settings = $this->keyValueFactory->get('development_settings');
+    $twig_debug = $development_settings->get('twig_debug', FALSE);
+    $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
+    if ($twig_debug || $twig_cache_disable) {
+      return $this->findDefinitions();
+    }
+
+    return $definitions;
+  }
+
   /**
    * Gets a component for rendering.
    *
-- 
GitLab


From 37f8289afb8a237235b542ec8fa27d7111b80bfd Mon Sep 17 00:00:00 2001
From: Stephen Mustgrave <38930-smustgrave@users.noreply.drupalcode.org>
Date: Mon, 23 Sep 2024 14:11:22 +0000
Subject: [PATCH 2/8] Apply 1 suggestion(s) to 1 file(s)

---
 core/lib/Drupal/Core/Theme/ComponentPluginManager.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
index d51ede1ad067..b90b6fa03ce4 100644
--- a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
+++ b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
@@ -131,7 +131,7 @@ public function createInstance($plugin_id, array $configuration = []): Component
   /**
    * {@inheritdoc}
    */
-  public function getDefinitions() {
+  public function getDefinitions(): array {
     $definitions = parent::getDefinitions();
 
     $development_settings = $this->keyValueFactory->get('development_settings');
-- 
GitLab


From 2f5f4c7622d0b41f33fe6cc06f64637fa6bfc8c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Mon, 24 Feb 2025 15:44:39 +0100
Subject: [PATCH 3/8] Avoid an unnecessary discover and caching step

---
 core/lib/Drupal/Core/Theme/ComponentPluginManager.php | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
index b90b6fa03ce4..3e1b501e2241 100644
--- a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
+++ b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
@@ -132,16 +132,13 @@ public function createInstance($plugin_id, array $configuration = []): Component
    * {@inheritdoc}
    */
   public function getDefinitions(): array {
-    $definitions = parent::getDefinitions();
-
     $development_settings = $this->keyValueFactory->get('development_settings');
     $twig_debug = $development_settings->get('twig_debug', FALSE);
     $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
     if ($twig_debug || $twig_cache_disable) {
       return $this->findDefinitions();
     }
-
-    return $definitions;
+    return parent::getDefinitions();
   }
 
   /**
-- 
GitLab


From 55031062fc3000186dedd3538cf6eecdccaeaf21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Mon, 24 Feb 2025 15:46:00 +0100
Subject: [PATCH 4/8] Add a kernel test

---
 .../Components/ComponentPluginManagerTest.php | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
index 1f110e36b4b5..5cf629135678 100644
--- a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
+++ b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
@@ -5,6 +5,7 @@
 namespace Drupal\KernelTests\Components;
 
 use Drupal\Core\Render\Component\Exception\ComponentNotFoundException;
+use Drupal\Core\Cache\CacheBackendInterface;
 
 /**
  * Tests the component plugin manager.
@@ -49,4 +50,37 @@ public function testMismatchingFolderName(): void {
     $this->manager->find('sdc_theme_test:mismatching-folder-name');
   }
 
+  /**
+   * Test component definitions caching depending on twig debug/cache settings.
+   *
+   * @dataProvider providerTestComponentCachingDependingOnDevelopmentSettings
+   */
+  public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDebug, bool $cacheEnabled, int $expectedCacheGets): void {
+    // Set the development settings.
+    $developmentSettings = $this->keyValue->get('development_settings');
+    $developmentSettings->set('twig_debug', $twigDebug);
+    $developmentSettings->set('twig_cache_disable', !$cacheEnabled);
+
+    // Set the cache backend as a spy mock.
+    $cacheBackend = $this->createMock(CacheBackendInterface::class);
+    $cacheBackend->expects($this->exactly($expectedCacheGets))->method('get')->with('cache_key');
+    $this->manager->setCacheBackend($cacheBackend, 'cache_key');
+
+    // Make two calls to getDefinitions() to ensure cache is called if needed.
+    $this->manager->getDefinitions();
+    $this->manager->getDefinitions();
+  }
+
+  /**
+   * Data provider for testComponentCachingDependingOnDevelopmentSettings().
+   */
+  public static function providerTestComponentCachingDependingOnDevelopmentSettings(): array {
+    return [
+      'Debug enabled, cache enabled' => [TRUE, TRUE, 0],
+      'Debug enabled, cache disabled' => [TRUE, FALSE, 0],
+      'Debug disabled, cache enabled' => [FALSE, TRUE, 1],
+      'Debug disabled, cache disabled' => [FALSE, FALSE, 0],
+    ];
+  }
+
 }
-- 
GitLab


From b25a58ab8e21362d1f9220cd15d06c1b11218c38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Mon, 24 Feb 2025 21:02:00 +0100
Subject: [PATCH 5/8] Improve a bit a test

---
 .../Components/ComponentPluginManagerTest.php | 24 +++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
index 5cf629135678..59393564944d 100644
--- a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
+++ b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
@@ -53,9 +53,16 @@ public function testMismatchingFolderName(): void {
   /**
    * Test component definitions caching depending on twig debug/cache settings.
    *
+   * @param bool $twigDebug
+   *   Whether twig debug is enabled.
+   * @param bool $cacheEnabled
+   *   Whether cache is enabled.
+   * @param bool $expectCacheGet
+   *   Whether we expect the cache to be called.
+   *
    * @dataProvider providerTestComponentCachingDependingOnDevelopmentSettings
    */
-  public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDebug, bool $cacheEnabled, int $expectedCacheGets): void {
+  public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDebug, bool $cacheEnabled, bool $expectCacheGet): void {
     // Set the development settings.
     $developmentSettings = $this->keyValue->get('development_settings');
     $developmentSettings->set('twig_debug', $twigDebug);
@@ -63,10 +70,13 @@ public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDeb
 
     // Set the cache backend as a spy mock.
     $cacheBackend = $this->createMock(CacheBackendInterface::class);
-    $cacheBackend->expects($this->exactly($expectedCacheGets))->method('get')->with('cache_key');
+    $cacheBackend->expects($expectCacheGet ? $this->once() : $this->never())
+      ->method('get')
+      ->with('cache_key');
     $this->manager->setCacheBackend($cacheBackend, 'cache_key');
 
-    // Make two calls to getDefinitions() to ensure cache is called if needed.
+    // Make two calls to getDefinitions() to ensure the
+    // cache is/isn't called if it should/shouldn't be.
     $this->manager->getDefinitions();
     $this->manager->getDefinitions();
   }
@@ -76,10 +86,10 @@ public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDeb
    */
   public static function providerTestComponentCachingDependingOnDevelopmentSettings(): array {
     return [
-      'Debug enabled, cache enabled' => [TRUE, TRUE, 0],
-      'Debug enabled, cache disabled' => [TRUE, FALSE, 0],
-      'Debug disabled, cache enabled' => [FALSE, TRUE, 1],
-      'Debug disabled, cache disabled' => [FALSE, FALSE, 0],
+      'Debug enabled, cache enabled' => [TRUE, TRUE, FALSE],
+      'Debug enabled, cache disabled' => [TRUE, FALSE, FALSE],
+      'Debug disabled, cache enabled' => [FALSE, TRUE, TRUE],
+      'Debug disabled, cache disabled' => [FALSE, FALSE, FALSE],
     ];
   }
 
-- 
GitLab


From 243b0c8559f0945d09287a6c2badd8153e25332e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Wed, 5 Mar 2025 17:17:17 +0100
Subject: [PATCH 6/8] Get twig config from container parameters instead of
 keyvalue development_settings

This way, we get the correct configuration, being it set either through the DevelopmentSettings UI or development.services.yml file
---
 core/core.services.yml                          |  2 +-
 .../Core/Theme/ComponentPluginManager.php       | 17 +++++++++--------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index c8434454bac0..21a5a029b339 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1884,7 +1884,7 @@ services:
       - '@file_system'
       - '@Drupal\Core\Theme\Component\SchemaCompatibilityChecker'
       - '@Drupal\Core\Theme\Component\ComponentValidator'
-      - '@keyvalue'
+      - '@service_container'
       - '%app.root%'
   Drupal\Core\Theme\ComponentPluginManager: '@plugin.manager.sdc'
   Drupal\Core\Template\Loader\ComponentLoader:
diff --git a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
index 3e1b501e2241..0241b494020a 100644
--- a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
+++ b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
@@ -12,7 +12,6 @@
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Extension\ThemeHandlerInterface;
 use Drupal\Core\File\FileSystemInterface;
-use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
 use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
 use Drupal\Core\Plugin\DefaultPluginManager;
 use Drupal\Core\Plugin\Factory\ContainerFactory;
@@ -22,6 +21,7 @@
 use Drupal\Core\Render\Component\Exception\ComponentNotFoundException;
 use Drupal\Core\Render\Component\Exception\IncompatibleComponentSchema;
 use Drupal\Core\Plugin\Discovery\DirectoryWithMetadataPluginDiscovery;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Defines a plugin manager to deal with components.
@@ -63,8 +63,8 @@ class ComponentPluginManager extends DefaultPluginManager implements Categorizin
    *   The compatibility checker.
    * @param \Drupal\Core\Theme\Component\ComponentValidator $componentValidator
    *   The component validator.
-   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValueFactory
-   *   The key value factory.
+   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
+   *   The container.
    * @param string $appRoot
    *   The application root.
    */
@@ -78,7 +78,7 @@ public function __construct(
     protected FileSystemInterface $fileSystem,
     protected SchemaCompatibilityChecker $compatibilityChecker,
     protected ComponentValidator $componentValidator,
-    protected KeyValueFactoryInterface $keyValueFactory,
+    protected ContainerInterface $container,
     protected string $appRoot,
   ) {
     // We are skipping the call to the parent constructor to avoid initializing
@@ -132,12 +132,13 @@ public function createInstance($plugin_id, array $configuration = []): Component
    * {@inheritdoc}
    */
   public function getDefinitions(): array {
-    $development_settings = $this->keyValueFactory->get('development_settings');
-    $twig_debug = $development_settings->get('twig_debug', FALSE);
-    $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
-    if ($twig_debug || $twig_cache_disable) {
+    $twig_config = $this->container->getParameter('twig.config');
+    $twig_debug = $twig_config['debug'] ?? NULL;
+    $twig_cache = $twig_config['cache'] ?? NULL;
+    if ($twig_debug === TRUE || $twig_cache === FALSE) {
       return $this->findDefinitions();
     }
+
     return parent::getDefinitions();
   }
 
-- 
GitLab


From ef6671c630f3d71fa7367671a79259be4ecc21dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Wed, 5 Mar 2025 17:39:56 +0100
Subject: [PATCH 7/8] Set also $this->definitions when retrieving fresh
 uncached definitions

---
 core/lib/Drupal/Core/Theme/ComponentPluginManager.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
index 0241b494020a..e69d32c74a5c 100644
--- a/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
+++ b/core/lib/Drupal/Core/Theme/ComponentPluginManager.php
@@ -136,7 +136,8 @@ public function getDefinitions(): array {
     $twig_debug = $twig_config['debug'] ?? NULL;
     $twig_cache = $twig_config['cache'] ?? NULL;
     if ($twig_debug === TRUE || $twig_cache === FALSE) {
-      return $this->findDefinitions();
+      $this->definitions = $this->findDefinitions();
+      return $this->definitions;
     }
 
     return parent::getDefinitions();
-- 
GitLab


From 475c4a208985dcf2d3d76a92025387d801bea54e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20Dorado?= <victor.dorado.asensio@gmail.com>
Date: Wed, 5 Mar 2025 17:41:31 +0100
Subject: [PATCH 8/8] Improve the test to not rely on the cache being called,
 but in a more real behavior

---
 .../sdc_test_plugin_manager.info.yml          |  3 +
 .../SdcTestPluginManagerServiceProvider.php   | 28 +++++++++
 .../src/Theme/TestComponentPluginManager.php  | 36 +++++++++++
 .../Components/ComponentPluginManagerTest.php | 61 +++++++++++++------
 4 files changed, 109 insertions(+), 19 deletions(-)
 create mode 100644 core/modules/system/tests/modules/sdc_test_plugin_manager/sdc_test_plugin_manager.info.yml
 create mode 100644 core/modules/system/tests/modules/sdc_test_plugin_manager/src/SdcTestPluginManagerServiceProvider.php
 create mode 100644 core/modules/system/tests/modules/sdc_test_plugin_manager/src/Theme/TestComponentPluginManager.php

diff --git a/core/modules/system/tests/modules/sdc_test_plugin_manager/sdc_test_plugin_manager.info.yml b/core/modules/system/tests/modules/sdc_test_plugin_manager/sdc_test_plugin_manager.info.yml
new file mode 100644
index 000000000000..621b13a6a1b6
--- /dev/null
+++ b/core/modules/system/tests/modules/sdc_test_plugin_manager/sdc_test_plugin_manager.info.yml
@@ -0,0 +1,3 @@
+name: 'Components Plugin Manager Test'
+type: module
+package: Testing
diff --git a/core/modules/system/tests/modules/sdc_test_plugin_manager/src/SdcTestPluginManagerServiceProvider.php b/core/modules/system/tests/modules/sdc_test_plugin_manager/src/SdcTestPluginManagerServiceProvider.php
new file mode 100644
index 000000000000..7523d5cd1664
--- /dev/null
+++ b/core/modules/system/tests/modules/sdc_test_plugin_manager/src/SdcTestPluginManagerServiceProvider.php
@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\sdc_test_plugin_manager;
+
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\DependencyInjection\ServiceProviderBase;
+
+use Drupal\sdc_test_plugin_manager\Theme\TestComponentPluginManager;
+
+/**
+ * Modifies the component plugin manager service.
+ */
+class SdcTestPluginManagerServiceProvider extends ServiceProviderBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function alter(ContainerBuilder $container): void {
+    // Overrides plugin.manager.sdc class to facilitate testing.
+    if ($container->hasDefinition('plugin.manager.sdc')) {
+      $definition = $container->getDefinition('plugin.manager.sdc');
+      $definition->setClass(TestComponentPluginManager::class);
+    }
+  }
+
+}
diff --git a/core/modules/system/tests/modules/sdc_test_plugin_manager/src/Theme/TestComponentPluginManager.php b/core/modules/system/tests/modules/sdc_test_plugin_manager/src/Theme/TestComponentPluginManager.php
new file mode 100644
index 000000000000..8834f691d467
--- /dev/null
+++ b/core/modules/system/tests/modules/sdc_test_plugin_manager/src/Theme/TestComponentPluginManager.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\sdc_test_plugin_manager\Theme;
+
+use Drupal\Component\DependencyInjection\ContainerInterface;
+use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
+use Drupal\Core\Theme\ComponentPluginManager;
+
+/**
+ * Provides a test entity type manager.
+ */
+class TestComponentPluginManager extends ComponentPluginManager {
+
+  /**
+   * Sets the discovery for the manager.
+   *
+   * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
+   *   The discovery object.
+   */
+  public function setDiscovery(DiscoveryInterface $discovery): void {
+    $this->discovery = $discovery;
+  }
+
+  /**
+   * Sets the container for the manager.
+   *
+   * @param \Drupal\Component\DependencyInjection\ContainerInterface $container
+   *   The container object.
+   */
+  public function setContainer(ContainerInterface $container): void {
+    $this->container = $container;
+  }
+
+}
diff --git a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
index 59393564944d..5b689c81cff7 100644
--- a/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
+++ b/core/tests/Drupal/KernelTests/Components/ComponentPluginManagerTest.php
@@ -4,8 +4,9 @@
 
 namespace Drupal\KernelTests\Components;
 
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\Plugin\Discovery\DirectoryWithMetadataPluginDiscovery;
 use Drupal\Core\Render\Component\Exception\ComponentNotFoundException;
-use Drupal\Core\Cache\CacheBackendInterface;
 
 /**
  * Tests the component plugin manager.
@@ -17,7 +18,12 @@ class ComponentPluginManagerTest extends ComponentKernelTestBase {
   /**
    * {@inheritdoc}
    */
-  protected static $modules = ['system', 'sdc_test', 'sdc_test_replacements'];
+  protected static $modules = [
+    'system',
+    'sdc_test',
+    'sdc_test_replacements',
+    'sdc_test_plugin_manager',
+  ];
 
   /**
    * {@inheritdoc}
@@ -57,28 +63,45 @@ public function testMismatchingFolderName(): void {
    *   Whether twig debug is enabled.
    * @param bool $cacheEnabled
    *   Whether cache is enabled.
-   * @param bool $expectCacheGet
-   *   Whether we expect the cache to be called.
+   * @param bool $expectCached
+   *   Whether we expect the definitions are cached or not.
    *
    * @dataProvider providerTestComponentCachingDependingOnDevelopmentSettings
    */
-  public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDebug, bool $cacheEnabled, bool $expectCacheGet): void {
-    // Set the development settings.
-    $developmentSettings = $this->keyValue->get('development_settings');
-    $developmentSettings->set('twig_debug', $twigDebug);
-    $developmentSettings->set('twig_cache_disable', !$cacheEnabled);
+  public function testComponentCachingDependingOnDevelopmentSettings(bool $twigDebug, bool $cacheEnabled, bool $expectCached): void {
+    // Set the manager to a local variable, so we can type hint it.
+    /** @var \Drupal\sdc_test_plugin_manager\Theme\TestComponentPluginManager $manager */
+    $manager = $this->manager;
 
-    // Set the cache backend as a spy mock.
-    $cacheBackend = $this->createMock(CacheBackendInterface::class);
-    $cacheBackend->expects($expectCacheGet ? $this->once() : $this->never())
-      ->method('get')
-      ->with('cache_key');
-    $this->manager->setCacheBackend($cacheBackend, 'cache_key');
+    $firstObtainedDefinitions = $manager->getDefinitions();
 
-    // Make two calls to getDefinitions() to ensure the
-    // cache is/isn't called if it should/shouldn't be.
-    $this->manager->getDefinitions();
-    $this->manager->getDefinitions();
+    // Set a mock discovery in the manager.
+    $discovery = $this->createMock(DirectoryWithMetadataPluginDiscovery::class);
+    $discovery->method('getDefinitions')
+      ->willReturn(array_slice($firstObtainedDefinitions, 0, -1, TRUE));
+    $manager->setDiscovery($discovery);
+
+    // Set a mock container in the manager.
+    $container = $this->createMock(ContainerBuilder::class);
+    $container->method('getParameter')
+      ->with('twig.config')
+      ->willReturn([
+        'debug' => $twigDebug,
+        'cache' => $cacheEnabled,
+      ]);
+    $manager->setContainer($container);
+
+    // Assert over definition keys, since it's the cleanest
+    // way to check if the definitions are the same after we
+    // removed one of them.
+    $firstObtainedDefinitionKeys = array_keys($firstObtainedDefinitions);
+    $secondObtainedDefinitionKeys = array_keys($manager->getDefinitions());
+    if ($expectCached) {
+      $this->assertEquals($firstObtainedDefinitionKeys, $secondObtainedDefinitionKeys);
+    }
+    else {
+      $this->assertNotEquals($firstObtainedDefinitionKeys, $secondObtainedDefinitionKeys);
+    }
   }
 
   /**
-- 
GitLab