From 78842348b281a02374fd21ba12644e2a1bb5a99d Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 3 Nov 2023 11:53:43 +0100 Subject: feat(dependencyinjection): Allow optional (nullable) services Allows working with classes that might or might not be available. Signed-off-by: Christoph Wurst --- .../AppFramework/Utility/SimpleContainerTest.php | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests/lib/AppFramework/Utility/SimpleContainerTest.php') diff --git a/tests/lib/AppFramework/Utility/SimpleContainerTest.php b/tests/lib/AppFramework/Utility/SimpleContainerTest.php index 61b3299671b..054012bdd5d 100644 --- a/tests/lib/AppFramework/Utility/SimpleContainerTest.php +++ b/tests/lib/AppFramework/Utility/SimpleContainerTest.php @@ -50,6 +50,17 @@ class ClassComplexConstructor { } } +class ClassNullableUntypedConstructorArg { + public function __construct($class) { + } +} +class ClassNullableTypedConstructorArg { + public $class; + public function __construct(?\Some\Class $class) { + $this->class = $class; + } +} + interface IInterfaceConstructor { } class ClassInterfaceConstructor { @@ -243,4 +254,17 @@ class SimpleContainerTest extends \Test\TestCase { $this->assertNotSame( $this->container->query('test'), $this->container->query('test1')); } + + public function testQueryUntypedNullable(): void { + $this->expectException(\OCP\AppFramework\QueryException::class); + + $this->container->query(ClassNullableUntypedConstructorArg::class); + } + + public function testQueryTypedNullable(): void { + /** @var ClassNullableTypedConstructorArg $service */ + $service = $this->container->query(ClassNullableTypedConstructorArg::class); + + self::assertNull($service->class); + } } -- cgit v1.2.3