fix: Fix issues and tests in DIContainer and friends

Some tests related to MiddlewareDispatcher are still failing.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2025-06-17 15:00:01 +02:00
parent 9913bdda90
commit ab310ce938
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
5 changed files with 23 additions and 33 deletions

View file

@ -23,12 +23,12 @@ use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use Psr\Container\ContainerInterface;
/**
* @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions
@ -44,7 +44,7 @@ class DeletedShareAPIController extends OCSController {
private IGroupManager $groupManager,
private IRootFolder $rootFolder,
private IAppManager $appManager,
private IServerContainer $serverContainer,
private ContainerInterface $serverContainer,
) {
parent::__construct($appName, $request);
}

View file

@ -44,6 +44,7 @@ use OCP\IL10N;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Security\Ip\IRemoteAddress;
@ -110,8 +111,8 @@ class DIContainer extends SimpleContainer implements IAppContainer {
$this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class);
// commonly used attributes
$this->registerService('userId', function (ContainerInterface $c): string {
return $c->get(IUserSession::class)->getSession()->get('user_id');
$this->registerService('userId', function (ContainerInterface $c): ?string {
return $c->get(ISession::class)->get('user_id');
});
$this->registerService('webRoot', function (ContainerInterface $c): string {
@ -359,6 +360,9 @@ class DIContainer extends SimpleContainer implements IAppContainer {
return parent::query($name);
} elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) {
return parent::query($name);
} elseif (str_starts_with($name, 'OC\\AppFramework\\Services\\')) {
/* AppFramework services are scoped to the application */
return parent::query($name);
}
throw new QueryException('Could not resolve ' . $name . '!'

View file

@ -6,11 +6,12 @@ declare(strict_types=1);
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Support\CrashReport;
use Exception;
use OCP\AppFramework\QueryException;
use OCP\IServerContainer;
use OCP\Server;
use OCP\Support\CrashReport\ICollectBreadcrumbs;
use OCP\Support\CrashReport\IMessageReporter;
use OCP\Support\CrashReport\IRegistry;
@ -26,17 +27,8 @@ class Registry implements IRegistry {
/** @var IReporter[] */
private $reporters = [];
/** @var IServerContainer */
private $serverContainer;
public function __construct(IServerContainer $serverContainer) {
$this->serverContainer = $serverContainer;
}
/**
* Register a reporter instance
*
* @param IReporter $reporter
*/
public function register(IReporter $reporter): void {
$this->reporters[] = $reporter;
@ -49,10 +41,6 @@ class Registry implements IRegistry {
/**
* Delegate breadcrumb collection to all registered reporters
*
* @param string $message
* @param string $category
* @param array $context
*
* @since 15.0.0
*/
public function delegateBreadcrumb(string $message, string $category, array $context = []): void {
@ -69,7 +57,6 @@ class Registry implements IRegistry {
* Delegate crash reporting to all registered reporters
*
* @param Exception|Throwable $exception
* @param array $context
*/
public function delegateReport($exception, array $context = []): void {
$this->loadLazyProviders();
@ -82,9 +69,6 @@ class Registry implements IRegistry {
/**
* Delegate a message to all reporters that implement IMessageReporter
*
* @param string $message
* @param array $context
*
* @return void
*/
public function delegateMessage(string $message, array $context = []): void {
@ -101,13 +85,13 @@ class Registry implements IRegistry {
while (($class = array_shift($this->lazyReporters)) !== null) {
try {
/** @var IReporter $reporter */
$reporter = $this->serverContainer->query($class);
$reporter = Server::get($class);
} catch (QueryException $e) {
/*
* There is a circular dependency between the logger and the registry, so
* we can not inject it. Thus the static call.
*/
\OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [
Server::get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [
'exception' => $e,
]);
return;
@ -123,7 +107,7 @@ class Registry implements IRegistry {
* There is a circular dependency between the logger and the registry, so
* we can not inject it. Thus the static call.
*/
\OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [
Server::get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [
'exception' => $e,
]);
}

View file

@ -29,7 +29,7 @@ function rrmdir($directory) {
class AppTest extends \Test\TestCase {
private $container;
private DIContainer $container;
private $io;
private $api;
private $controller;
@ -55,8 +55,8 @@ class AppTest extends \Test\TestCase {
$this->controllerMethod = 'method';
$this->container[$this->controllerName] = $this->controller;
$this->container['Dispatcher'] = $this->dispatcher;
$this->container['OCP\\AppFramework\\Http\\IOutput'] = $this->io;
$this->container[Dispatcher::class] = $this->dispatcher;
$this->container[IOutput::class] = $this->io;
$this->container['urlParams'] = ['_route' => 'not-profiler'];
$this->appPath = __DIR__ . '/../../../apps/namespacetestapp';
@ -165,7 +165,7 @@ class AppTest extends \Test\TestCase {
}
public function testCoreApp(): void {
$this->container['AppName'] = 'core';
$this->container['appName'] = 'core';
$this->container['OC\Core\Controller\Foo'] = $this->controller;
$this->container['urlParams'] = ['_route' => 'not-profiler'];
@ -183,7 +183,7 @@ class AppTest extends \Test\TestCase {
}
public function testSettingsApp(): void {
$this->container['AppName'] = 'settings';
$this->container['appName'] = 'settings';
$this->container['OCA\Settings\Controller\Foo'] = $this->controller;
$this->container['urlParams'] = ['_route' => 'not-profiler'];
@ -201,7 +201,7 @@ class AppTest extends \Test\TestCase {
}
public function testApp(): void {
$this->container['AppName'] = 'bar';
$this->container['appName'] = 'bar';
$this->container['OCA\Bar\Controller\Foo'] = $this->controller;
$this->container['urlParams'] = ['_route' => 'not-profiler'];

View file

@ -18,13 +18,13 @@ use OCP\AppFramework\Middleware;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IRequestId;
use PHPUnit\Framework\MockObject\MockObject;
/**
* @group DB
*/
class DIContainerTest extends \Test\TestCase {
/** @var DIContainer|\PHPUnit\Framework\MockObject\MockObject */
private $container;
private DIContainer&MockObject $container;
protected function setUp(): void {
parent::setUp();
@ -45,11 +45,13 @@ class DIContainerTest extends \Test\TestCase {
public function testProvidesAppName(): void {
$this->assertTrue(isset($this->container['AppName']));
$this->assertTrue(isset($this->container['appName']));
}
public function testAppNameIsSetCorrectly(): void {
$this->assertEquals('name', $this->container['AppName']);
$this->assertEquals('name', $this->container['appName']);
}
public function testMiddlewareDispatcherIncludesSecurityMiddleware(): void {