From c07a96e61cef8315881b5a82de7acc9e6074a7e8 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Thu, 23 Jan 2014 22:19:50 -0800
Subject: [PATCH] Issue #2171315 by diarmy, tim.plunkett: Cleanup the container
 in UnitTestCase::tearDown.

---
 .../Drupal/comment/Tests/Entity/CommentLockTest.php  |  9 ---------
 .../Tests/Core/Menu/LocalTaskIntegrationTest.php     |  6 ------
 .../Drupal/Tests/Core/Session/UserSessionTest.php    |  9 ---------
 core/tests/Drupal/Tests/UnitTestCase.php             | 12 ++++++++++++
 4 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php b/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
index 48cc862cc854..10053017ebe0 100644
--- a/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
+++ b/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
@@ -74,15 +74,6 @@ public function testLocks() {
     $comment->postSave($storage_controller);
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  protected function tearDown() {
-    parent::tearDown();
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
 }
 }
 namespace {
diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
index aaaa6259fc9a..d590e06a2fec 100644
--- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
+++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
@@ -46,12 +46,6 @@ protected function setUp() {
     \Drupal::setContainer($container);
   }
 
-  protected function tearDown() {
-    // Passes in an empty container.
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
   /**
    * Sets up the local task manager for the test.
    */
diff --git a/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php b/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
index 8309852de3ea..dfaa5d3fc5ad 100644
--- a/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
+++ b/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
@@ -119,15 +119,6 @@ protected function setUp() {
     $this->users['user_last'] = $this->createUserSession();
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  protected function tearDown() {
-    parent::tearDown();
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
   /**
    * Tests the has permission method.
    *
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index 958ffde7ef98..e869b9fc9655 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -8,6 +8,7 @@
 namespace Drupal\Tests;
 
 use Drupal\Component\Utility\Random;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 
 /**
  * Provides a base class and helpers for Drupal unit tests.
@@ -40,6 +41,17 @@ public static function getInfo() {
     throw new \RuntimeException("Sub-class must implement the getInfo method!");
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function tearDown() {
+    parent::tearDown();
+    if (\Drupal::getContainer()) {
+      $container = new ContainerBuilder();
+      \Drupal::setContainer($container);
+    }
+  }
+
   /**
    * Generates a unique random string containing letters and numbers.
    *
-- 
GitLab