From be75c67f414ac275b4cb3fb1a6e7d2116ca03542 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= <adam@phenaproxima.net>
Date: Tue, 2 Nov 2021 15:02:54 -0400
Subject: [PATCH] Refactor pm_bypass

---
 .../package_manager_bypass.services.yml       |  9 --------
 .../PackageManagerBypassServiceProvider.php   | 23 +++++++++++++++----
 .../src/Functional/ExcludedPathsTest.php      |  6 ++++-
 3 files changed, 24 insertions(+), 14 deletions(-)
 delete mode 100644 package_manager/tests/modules/package_manager_bypass/package_manager_bypass.services.yml

diff --git a/package_manager/tests/modules/package_manager_bypass/package_manager_bypass.services.yml b/package_manager/tests/modules/package_manager_bypass/package_manager_bypass.services.yml
deleted file mode 100644
index 42e9234669..0000000000
--- a/package_manager/tests/modules/package_manager_bypass/package_manager_bypass.services.yml
+++ /dev/null
@@ -1,9 +0,0 @@
-services:
-  package_manager_bypass.committer:
-    public: false
-    class: Drupal\package_manager_bypass\Committer
-    decorates: package_manager.committer
-    arguments:
-      - '@package_manager_bypass.committer.inner'
-    properties:
-      - { _serviceId: package_manager.committer }
diff --git a/package_manager/tests/modules/package_manager_bypass/src/PackageManagerBypassServiceProvider.php b/package_manager/tests/modules/package_manager_bypass/src/PackageManagerBypassServiceProvider.php
index e9b95a50ef..3394cf4e15 100644
--- a/package_manager/tests/modules/package_manager_bypass/src/PackageManagerBypassServiceProvider.php
+++ b/package_manager/tests/modules/package_manager_bypass/src/PackageManagerBypassServiceProvider.php
@@ -5,6 +5,7 @@ namespace Drupal\package_manager_bypass;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\DependencyInjection\ServiceProviderBase;
 use Drupal\Core\Site\Settings;
+use Symfony\Component\DependencyInjection\Reference;
 
 /**
  * Defines services to bypass Package Manager's core functionality.
@@ -17,10 +18,24 @@ class PackageManagerBypassServiceProvider extends ServiceProviderBase {
   public function alter(ContainerBuilder $container) {
     parent::alter($container);
 
-    $container->getDefinition('package_manager.beginner')
-      ->setClass(Beginner::class);
-    $container->getDefinition('package_manager.stager')
-      ->setClass(Stager::class);
+    // By default, bypass the Composer Stager library. This can be disabled for
+    // tests that want to use the real library, but only need to disable
+    // validators.
+    if (Settings::get('package_manager_bypass_stager', TRUE)) {
+      $container->getDefinition('package_manager.beginner')
+        ->setClass(Beginner::class);
+      $container->getDefinition('package_manager.stager')
+        ->setClass(Stager::class);
+
+      $container->register('package_manager_bypass.committer')
+        ->setClass(Committer::class)
+        ->setPublic(FALSE)
+        ->setDecoratedService('package_manager.committer')
+        ->setArguments([
+          new Reference('package_manager_bypass.committer.inner'),
+        ])
+        ->setProperty('_serviceId', 'package_manager.committer');
+    }
 
     // Allow functional tests to disable specific validators as necessary.
     // Kernel tests can override the ::register() method and modify the
diff --git a/package_manager/tests/src/Functional/ExcludedPathsTest.php b/package_manager/tests/src/Functional/ExcludedPathsTest.php
index 97ed57a8a2..f340b96a90 100644
--- a/package_manager/tests/src/Functional/ExcludedPathsTest.php
+++ b/package_manager/tests/src/Functional/ExcludedPathsTest.php
@@ -41,6 +41,10 @@ class ExcludedPathsTest extends BrowserTestBase {
     // @see \Drupal\Tests\package_manager\Kernel\WritableFileSystemValidatorTest
     $this->writeSettings([
       'settings' => [
+        'package_manager_bypass_stager' => (object) [
+          'value' => FALSE,
+          'required' => TRUE,
+        ],
         'package_manager_bypass_validators' => (object) [
           'value' => ['package_manager.validator.file_system'],
           'required' => TRUE,
@@ -77,7 +81,7 @@ class ExcludedPathsTest extends BrowserTestBase {
     $stage = new Stage(
       $path_locator->reveal(),
       $this->container->get('package_manager.beginner'),
-      $this->container->get('package_manager.stager'),
+      $stager,
       $this->container->get('package_manager.committer'),
       $this->container->get('package_manager.cleaner'),
       $this->container->get('event_dispatcher')
-- 
GitLab