]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix overwriteService() for apps 22027/head
authorJoas Schilling <coding@schilljs.com>
Tue, 28 Jul 2020 07:38:43 +0000 (09:38 +0200)
committerJoas Schilling <coding@schilljs.com>
Tue, 28 Jul 2020 07:38:43 +0000 (09:38 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/ServerContainer.php
tests/lib/TestCase.php

index b1bf38265b864d3c96a78d6ddc8aa4a796b1073f..72275ac12057711c6ecb61ceb7e0b2e399c78243 100644 (file)
@@ -156,7 +156,12 @@ class ServerContainer extends SimpleContainer {
                return parent::query($name, $autoload);
        }
 
-       private function getAppContainerForService(string $id): ?DIContainer {
+       /**
+        * @internal
+        * @param string $id
+        * @return DIContainer|null
+        */
+       public function getAppContainerForService(string $id): ?DIContainer {
                if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
                        return null;
                }
index 88c5b468543ffb22a8661c8474ade1fc60c513aa..38cfc4a1c8fde95659d763f6a134bf4bf5a0a678 100644 (file)
@@ -51,13 +51,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
         * @param mixed $newService
         * @return bool
         */
-       public function overwriteService($name, $newService) {
+       public function overwriteService(string $name, $newService): bool {
                if (isset($this->services[$name])) {
                        return false;
                }
 
                $this->services[$name] = \OC::$server->query($name);
-               \OC::$server->registerService($name, function () use ($newService) {
+               $container = \OC::$server->getAppContainerForService($name);
+               $container = $container ?? \OC::$server;
+
+               $container->registerService($name, function () use ($newService) {
                        return $newService;
                });
 
@@ -68,10 +71,14 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
         * @param string $name
         * @return bool
         */
-       public function restoreService($name) {
+       public function restoreService(string $name): bool {
                if (isset($this->services[$name])) {
                        $oldService = $this->services[$name];
-                       \OC::$server->registerService($name, function () use ($oldService) {
+
+                       $container = \OC::$server->getAppContainerForService($name);
+                       $container = $container ?? \OC::$server;
+
+                       $container->registerService($name, function () use ($oldService) {
                                return $oldService;
                        });