diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-11-03 11:53:43 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-11-03 11:53:43 +0100 |
commit | 78842348b281a02374fd21ba12644e2a1bb5a99d (patch) | |
tree | 34cfe503e36a0222c2df58f0b9a7af69cb3836dd /tests/lib | |
parent | ba93afbddd4930408129e863957fd7b631582ae2 (diff) | |
download | nextcloud-server-78842348b281a02374fd21ba12644e2a1bb5a99d.tar.gz nextcloud-server-78842348b281a02374fd21ba12644e2a1bb5a99d.zip |
feat(dependencyinjection): Allow optional (nullable) services
Allows working with classes that might or might not be available.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/AppFramework/Utility/SimpleContainerTest.php | 24 |
1 files changed, 24 insertions, 0 deletions
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); + } } |