]> source.dussan.org Git - nextcloud-server.git/commitdiff
Restore all services when a tests failed/finished for better testcase isolation 1822/head
authorJoas Schilling <coding@schilljs.com>
Thu, 20 Oct 2016 13:15:54 +0000 (15:15 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 20 Oct 2016 13:19:41 +0000 (15:19 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
tests/lib/TestCase.php

index 7ccff3823572e4c7687bbb74167bc2c35d010649..61372d4ae90ccbc8f6b90aa30235661e8a82fabb 100644 (file)
@@ -102,6 +102,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
                return false;
        }
 
+       public function restoreAllServices() {
+               if (!empty($this->services)) {
+                       if (!empty($this->services)) {
+                               foreach ($this->services as $name => $service) {
+                                       $this->restoreService($name);
+                               }
+                       }
+               }
+       }
+
        protected function getTestTraits() {
                $traits = [];
                $class = $this;
@@ -132,9 +142,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
 
                // overwrite the command bus with one we can run ourselves
                $this->commandBus = new QueueBus();
-               \OC::$server->registerService('AsyncCommandBus', function () {
-                       return $this->commandBus;
-               });
+               $this->overwriteService('AsyncCommandBus', $this->commandBus);
 
                $traits = $this->getTestTraits();
                foreach ($traits as $trait) {
@@ -145,7 +153,22 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
                }
        }
 
+       protected function onNotSuccessfulTest($e) {
+               $this->restoreAllServices();
+
+               // restore database connection
+               if (!$this->IsDatabaseAccessAllowed()) {
+                       \OC::$server->registerService('DatabaseConnection', function () {
+                               return self::$realDatabase;
+                       });
+               }
+
+               parent::onNotSuccessfulTest($e);
+       }
+
        protected function tearDown() {
+               $this->restoreAllServices();
+
                // restore database connection
                if (!$this->IsDatabaseAccessAllowed()) {
                        \OC::$server->registerService('DatabaseConnection', function () {