From 51e34b5d8b6d65e95707a7a4388cf2337fadddbc Mon Sep 17 00:00:00 2001
From: Thomas Steur <tsteur@users.noreply.github.com>
Date: Tue, 9 May 2017 20:29:18 +1200
Subject: [PATCH] Automatically load required plugins during test (#11675)

* Automatically load required plugins during test

When a plugin requires another plugin in `plugin.json`, this will make sure to load the required plugin as well (if it exists in filesystem) when running the tests.

* Load plugin json manually without metadata loader as container is not created yet

* only load plugin.json if it exists
---
 .../TestingEnvironmentManipulator.php         | 29 +++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/tests/PHPUnit/Framework/TestingEnvironmentManipulator.php b/tests/PHPUnit/Framework/TestingEnvironmentManipulator.php
index df6a011af5..1aa1a74de8 100644
--- a/tests/PHPUnit/Framework/TestingEnvironmentManipulator.php
+++ b/tests/PHPUnit/Framework/TestingEnvironmentManipulator.php
@@ -213,21 +213,46 @@ class TestingEnvironmentManipulator implements EnvironmentManipulator
                 Plugin::getPluginNameFromNamespace(get_called_class())
             )
         );
+
         foreach ($extraPlugins as $pluginName) {
             if (empty($pluginName)) {
                 continue;
             }
 
-            if (in_array($pluginName, $plugins)) {
-                continue;
+            $plugins = $this->getPluginAndRequiredPlugins($pluginName, $plugins);
+        }
+
+        return $plugins;
+    }
+
+    private function getPluginAndRequiredPlugins($pluginName, $plugins)
+    {
+        $pluginJsonPath = $this->makePathToPluginJson($pluginName);
+
+        if (file_exists($pluginJsonPath)) {
+            $pluginJson = json_decode(trim(file_get_contents($pluginJsonPath)), true);
+
+            if (!empty($pluginJson['require'])) {
+                foreach ($pluginJson['require'] as $possiblePluginName => $requiredVersion) {
+                    if (file_exists($this->makePathToPluginJson($possiblePluginName))) {
+                        $plugins = $this->getPluginAndRequiredPlugins($possiblePluginName, $plugins);
+                    }
+                }
             }
+        }
 
+        if (!in_array($pluginName, $plugins)) {
             $plugins[] = $pluginName;
         }
 
         return $plugins;
     }
 
+    private function makePathToPluginJson($pluginName)
+    {
+        return Plugin\Manager::getPluginsDirectory() . $pluginName . '/' . Plugin\MetadataLoader::PLUGIN_JSON_FILENAME;
+    }
+
     private function classExists($klass)
     {
         if (class_exists($klass)) {
-- 
GitLab