]> source.dussan.org Git - nextcloud-server.git/commitdiff
Restore DB connection after failure
authorVincent Petry <pvince81@owncloud.com>
Mon, 21 Dec 2015 17:08:08 +0000 (18:08 +0100)
committerVincent Petry <pvince81@owncloud.com>
Mon, 21 Dec 2015 17:14:29 +0000 (18:14 +0100)
In case of failure, PHPUnit seems to skip `tearDown`, so any useful
assertion messages cannot be shown because `tearDownAfterClass` is
throwing an error because of database usage.

This commit makes sure we also restore the database in
`tearDownAfterClass` to prevent the data root restoration to fail

tests/lib/testcase.php

index 1ee0c85b98aec72f625e302bc43d010203813b00..93b354863a98c59d700e00e4796280769254220c 100644 (file)
@@ -33,7 +33,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
        private $commandBus;
 
        /** @var IDBConnection */
-       static private $realDatabase;
+       static protected $realDatabase = null;
+       static private $wasDatabaseAllowed = false;
 
        protected function getTestTraits() {
                $traits = [];
@@ -52,7 +53,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
 
        protected function setUp() {
                // detect database access
+               self::$wasDatabaseAllowed = true;
                if (!$this->IsDatabaseAccessAllowed()) {
+                       self::$wasDatabaseAllowed = false;
                        if (is_null(self::$realDatabase)) {
                                self::$realDatabase = \OC::$server->getDatabaseConnection();
                        }
@@ -155,8 +158,15 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
        }
 
        public static function tearDownAfterClass() {
+               if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) {
+                       // in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass,
+                       // so we need the database again
+                       \OC::$server->registerService('DatabaseConnection', function () {
+                               return self::$realDatabase;
+                       });
+               }
                $dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
-               if (\OC::$server->getDatabaseConnection()) {
+               if (self::$wasDatabaseAllowed && \OC::$server->getDatabaseConnection()) {
                        $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
 
                        self::tearDownAfterClassCleanShares($queryBuilder);