From d17424f173f3d6eed7d30040e61301b8b0e7e703 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 20 Oct 2016 15:15:54 +0200 Subject: [PATCH] Restore all services when a tests failed/finished for better testcase isolation Signed-off-by: Joas Schilling --- tests/lib/TestCase.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index 514cb6ea3a5..0896b1f3a81 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -83,6 +83,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; @@ -113,9 +123,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) { @@ -126,7 +134,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 () { -- 2.39.5