1
0

FunctionInjectorTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\AppFramework\Bootstrap;
  8. use OC\AppFramework\Bootstrap\FunctionInjector;
  9. use OC\AppFramework\Utility\SimpleContainer;
  10. use Test\TestCase;
  11. interface Foo {
  12. }
  13. class FunctionInjectorTest extends TestCase {
  14. /** @var SimpleContainer */
  15. private $container;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->container = new SimpleContainer();
  19. }
  20. public function testInjectFnNotRegistered(): void {
  21. $this->expectException(\OCP\AppFramework\QueryException::class);
  22. (new FunctionInjector($this->container))->injectFn(static function (Foo $p1): void {
  23. });
  24. }
  25. public function testInjectFnNotRegisteredButNullable(): void {
  26. (new FunctionInjector($this->container))->injectFn(static function (?Foo $p1): void {
  27. });
  28. // Nothing to assert. No errors means everything is fine.
  29. $this->addToAssertionCount(1);
  30. }
  31. public function testInjectFnByType(): void {
  32. $this->container->registerService(Foo::class, function () {
  33. $this->addToAssertionCount(1);
  34. return new class implements Foo {
  35. };
  36. });
  37. (new FunctionInjector($this->container))->injectFn(static function (Foo $p1): void {
  38. });
  39. // Nothing to assert. No errors means everything is fine.
  40. $this->addToAssertionCount(1);
  41. }
  42. public function testInjectFnByName(): void {
  43. $this->container->registerParameter('test', 'abc');
  44. (new FunctionInjector($this->container))->injectFn(static function ($test): void {
  45. });
  46. // Nothing to assert. No errors means everything is fine.
  47. $this->addToAssertionCount(1);
  48. }
  49. }