From 20fcfb573951a594f71afaf97678d38d8b05c9f2 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 15 Dec 2022 10:37:27 +0100 Subject: feat(app framework)!: Inject services into controller methods Usually Nextcloud DI goes through constructor injection. This has the implication that each instance of a class builds the full DI tree. That is the injected services, their services, etc. Occasionally there is a service that is only needed for one controller method. Then the DI tree is build regardless if used or not. If services are injected into the method, we only build the DI tree if that method gets executed. This is also how Laravel allows injection. Signed-off-by: Christoph Wurst --- tests/lib/AppFramework/Http/DispatcherTest.php | 29 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'tests/lib/AppFramework/Http/DispatcherTest.php') diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php index 016bd7efb58..7f81701a8b3 100644 --- a/tests/lib/AppFramework/Http/DispatcherTest.php +++ b/tests/lib/AppFramework/Http/DispatcherTest.php @@ -36,6 +36,7 @@ use OCP\Diagnostics\IEventLogger; use OCP\IConfig; use OCP\IRequest; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use OCP\IRequestId; @@ -104,10 +105,10 @@ class DispatcherTest extends \Test\TestCase { private $config; /** @var LoggerInterface|MockObject */ private $logger; - /** - * @var IEventLogger|MockObject - */ + /** @var IEventLogger|MockObject */ private $eventLogger; + /** @var ContainerInterface|MockObject */ + private $container; protected function setUp(): void { parent::setUp(); @@ -116,6 +117,7 @@ class DispatcherTest extends \Test\TestCase { $this->config = $this->createMock(IConfig::class); $this->logger = $this->createMock(LoggerInterface::class); $this->eventLogger = $this->createMock(IEventLogger::class); + $this->container = $this->createMock(ContainerInterface::class); $app = $this->getMockBuilder( 'OC\AppFramework\DependencyInjection\DIContainer') ->disableOriginalConstructor() @@ -154,7 +156,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container, ); $this->response = $this->createMock(Response::class); @@ -330,7 +333,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); @@ -362,7 +366,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); @@ -397,7 +402,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); @@ -431,7 +437,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); @@ -466,7 +473,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); @@ -503,7 +511,8 @@ class DispatcherTest extends \Test\TestCase { $this->config, \OC::$server->getDatabaseConnection(), $this->logger, - $this->eventLogger + $this->eventLogger, + $this->container ); $controller = new TestController('app', $this->request); -- cgit v1.2.3