From 0a65cd6076dbb2579634f2b4390f1532db11d9f6 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 11 Mar 2020 21:33:30 +0000
Subject: [PATCH] Issue #3118477 by mondrake, Mile23: RegistryTest,
 RegistryLegacyTest both define the same class, use mock instead

---
 .../Drupal/Tests/Core/Test/PhpUnitCliTest.php | 35 ++++++++++++
 .../Tests/Core/Theme/RegistryLegacyTest.php   | 57 +++++--------------
 .../Drupal/Tests/Core/Theme/RegistryTest.php  | 33 +++++------
 3 files changed, 62 insertions(+), 63 deletions(-)
 create mode 100644 core/tests/Drupal/Tests/Core/Test/PhpUnitCliTest.php

diff --git a/core/tests/Drupal/Tests/Core/Test/PhpUnitCliTest.php b/core/tests/Drupal/Tests/Core/Test/PhpUnitCliTest.php
new file mode 100644
index 000000000000..135302f6c64c
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Test/PhpUnitCliTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\Tests\Core\Test;
+
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\Process\Process;
+
+/**
+ * @group TestSuites
+ * @group Test
+ */
+class PhpUnitCliTest extends UnitTestCase {
+
+  /**
+   * Ensure that the test suites are able to discover tests without incident.
+   */
+  public function testPhpUnitListTests() {
+    // Generate the list of tests for all the tests the suites can discover.
+    // The goal here is to successfully generate the list, without any
+    // duplicate namespace errors or so forth. This keeps us from committing
+    // tests which don't break under run-tests.sh, but do break under the
+    // phpunit test runner tool.
+    $process = new Process('vendor/bin/phpunit --configuration core --verbose --list-tests');
+    $process->setWorkingDirectory($this->root)
+      ->setTimeout(300)
+      ->setIdleTimeout(300);
+    $process->run();
+    $this->assertEquals(0, $process->getExitCode(),
+      'COMMAND: ' . $process->getCommandLine() . "\n" .
+      'OUTPUT: ' . $process->getOutput() . "\n" .
+      'ERROR: ' . $process->getErrorOutput() . "\n"
+    );
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Theme/RegistryLegacyTest.php b/core/tests/Drupal/Tests/Core/Theme/RegistryLegacyTest.php
index 0ee637fd39d1..d6d87baaa1e8 100644
--- a/core/tests/Drupal/Tests/Core/Theme/RegistryLegacyTest.php
+++ b/core/tests/Drupal/Tests/Core/Theme/RegistryLegacyTest.php
@@ -1,10 +1,5 @@
 <?php
 
-/**
- * @file
- * Contains \Drupal\Tests\Core\Theme\RegistryLegacyTest.
- */
-
 namespace Drupal\Tests\Core\Theme;
 
 use Drupal\Core\Theme\ActiveTheme;
@@ -21,9 +16,9 @@
 class RegistryLegacyTest extends UnitTestCase {
 
   /**
-   * The tested theme registry.
+   * The mocked theme registry.
    *
-   * @var \Drupal\Tests\Core\Theme\TestRegistry
+   * @var \Drupal\Core\Theme\Registry|PHPUnit\Framework\MockObject\MockObject
    */
   protected $registry;
 
@@ -69,13 +64,6 @@ class RegistryLegacyTest extends UnitTestCase {
    */
   protected $themeManager;
 
-  /**
-   * The list of functions that get_defined_functions() should provide.
-   *
-   * @var array
-   */
-  public static $functions = [];
-
   /**
    * {@inheritdoc}
    */
@@ -92,14 +80,6 @@ protected function setUp() {
     $this->setupTheme();
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  protected function tearDown() {
-    parent::tearDown();
-    static::$functions = [];
-  }
-
   /**
    * Tests getting legacy theme function registry data defined by a module.
    *
@@ -152,29 +132,18 @@ public function testGetLegacyThemeFunctionRegistryForModule() {
   }
 
   protected function setupTheme() {
-    $this->registry = new TestRegistry($this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization);
+    $this->registry = $this->getMockBuilder(Registry::class)
+      ->setMethods(['getPath'])
+      ->setConstructorArgs([$this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization])
+      ->getMock();
+    $this->registry->expects($this->any())
+      ->method('getPath')
+      ->willReturnCallback(function ($module) {
+        if ($module == 'theme_legacy_test') {
+          return 'core/modules/system/tests/modules/theme_legacy_test';
+        }
+      });
     $this->registry->setThemeManager($this->themeManager);
   }
 
 }
-
-class TestRegistry extends Registry {
-
-  protected function getPath($module) {
-    if ($module == 'theme_legacy_test') {
-      return 'core/modules/system/tests/modules/theme_legacy_test';
-    }
-  }
-
-}
-
-namespace Drupal\Core\Theme;
-
-use Drupal\Tests\Core\Theme\RegistryLegacyTest;
-
-/**
- * Overrides get_defined_functions() with a configurable mock.
- */
-function get_defined_functions() {
-  return RegistryLegacyTest::$functions ?: \get_defined_functions();
-}
diff --git a/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php b/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php
index c1f996293687..8e1adb0d02e1 100644
--- a/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php
+++ b/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php
@@ -1,10 +1,5 @@
 <?php
 
-/**
- * @file
- * Contains \Drupal\Tests\Core\Theme\RegistryTest.
- */
-
 namespace Drupal\Tests\Core\Theme;
 
 use Drupal\Core\Theme\ActiveTheme;
@@ -18,9 +13,9 @@
 class RegistryTest extends UnitTestCase {
 
   /**
-   * The tested theme registry.
+   * The mocked theme registry.
    *
-   * @var \Drupal\Tests\Core\Theme\TestRegistry
+   * @var \Drupal\Core\Theme\Registry|PHPUnit\Framework\MockObject\MockObject
    */
   protected $registry;
 
@@ -190,7 +185,7 @@ public function testPostProcessExtension($defined_functions, $hooks, $expected)
       ->method('getModuleList')
       ->willReturn([]);
 
-    $class = new \ReflectionClass(TestRegistry::class);
+    $class = new \ReflectionClass(Registry::class);
     $reflection_method = $class->getMethod('postProcessExtension');
     $reflection_method->setAccessible(TRUE);
     $reflection_method->invokeArgs($this->registry, [&$hooks, $theme->reveal()]);
@@ -476,22 +471,22 @@ public function providerTestPostProcessExtension() {
   }
 
   protected function setupTheme() {
-    $this->registry = new TestRegistry($this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization);
+    $this->registry = $this->getMockBuilder(Registry::class)
+      ->setMethods(['getPath'])
+      ->setConstructorArgs([$this->root, $this->cache, $this->lock, $this->moduleHandler, $this->themeHandler, $this->themeInitialization])
+      ->getMock();
+    $this->registry->expects($this->any())
+      ->method('getPath')
+      ->willReturnCallback(function ($module) {
+        if ($module == 'theme_test') {
+          return 'core/modules/system/tests/modules/theme_test';
+        }
+      });
     $this->registry->setThemeManager($this->themeManager);
   }
 
 }
 
-class TestRegistry extends Registry {
-
-  protected function getPath($module) {
-    if ($module == 'theme_test') {
-      return 'core/modules/system/tests/modules/theme_test';
-    }
-  }
-
-}
-
 namespace Drupal\Core\Theme;
 
 use Drupal\Tests\Core\Theme\RegistryTest;
-- 
GitLab