1
0

DIContainerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\DependencyInjection;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\MiddlewareRegistration;
  10. use OC\AppFramework\Bootstrap\RegistrationContext;
  11. use OC\AppFramework\DependencyInjection\DIContainer;
  12. use OC\AppFramework\Http\Request;
  13. use OC\AppFramework\Middleware\Security\SecurityMiddleware;
  14. use OCP\AppFramework\Middleware;
  15. use OCP\AppFramework\QueryException;
  16. use OCP\IConfig;
  17. use OCP\IRequestId;
  18. /**
  19. * @group DB
  20. */
  21. class DIContainerTest extends \Test\TestCase {
  22. /** @var DIContainer|\PHPUnit\Framework\MockObject\MockObject */
  23. private $container;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->container = $this->getMockBuilder(DIContainer::class)
  27. ->setMethods(['isAdminUser'])
  28. ->setConstructorArgs(['name'])
  29. ->getMock();
  30. }
  31. public function testProvidesRequest() {
  32. $this->assertTrue(isset($this->container['Request']));
  33. }
  34. public function testProvidesMiddlewareDispatcher() {
  35. $this->assertTrue(isset($this->container['MiddlewareDispatcher']));
  36. }
  37. public function testProvidesAppName() {
  38. $this->assertTrue(isset($this->container['AppName']));
  39. }
  40. public function testAppNameIsSetCorrectly() {
  41. $this->assertEquals('name', $this->container['AppName']);
  42. }
  43. public function testMiddlewareDispatcherIncludesSecurityMiddleware() {
  44. $this->container['Request'] = new Request(
  45. ['method' => 'GET'],
  46. $this->createMock(IRequestId::class),
  47. $this->createMock(IConfig::class)
  48. );
  49. $dispatcher = $this->container['MiddlewareDispatcher'];
  50. $middlewares = $dispatcher->getMiddlewares();
  51. $found = false;
  52. foreach ($middlewares as $middleware) {
  53. if ($middleware instanceof SecurityMiddleware) {
  54. $found = true;
  55. }
  56. }
  57. $this->assertTrue($found);
  58. }
  59. public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): void {
  60. $coordinator = $this->createMock(Coordinator::class);
  61. $this->container[Coordinator::class] = $coordinator;
  62. $this->container['Request'] = $this->createMock(Request::class);
  63. $registrationContext = $this->createMock(RegistrationContext::class);
  64. $registrationContext->method('getMiddlewareRegistrations')
  65. ->willReturn([
  66. new MiddlewareRegistration($this->container['appName'], 'foo', false),
  67. new MiddlewareRegistration('otherapp', 'bar', false),
  68. ]);
  69. $this->container['foo'] = new class extends Middleware {
  70. };
  71. $this->container['bar'] = new class extends Middleware {
  72. };
  73. $coordinator->method('getRegistrationContext')->willReturn($registrationContext);
  74. $dispatcher = $this->container['MiddlewareDispatcher'];
  75. $middlewares = $dispatcher->getMiddlewares();
  76. self::assertNotEmpty($middlewares);
  77. foreach ($middlewares as $middleware) {
  78. if ($middleware === $this->container['bar']) {
  79. $this->fail('Container must not register this middleware');
  80. }
  81. if ($middleware === $this->container['foo']) {
  82. // It is done
  83. return;
  84. }
  85. }
  86. $this->fail('Bootstrap registered middleware not found');
  87. }
  88. public function testMiddlewareDispatcherIncludesGlobalBootstrapMiddlewares(): void {
  89. $coordinator = $this->createMock(Coordinator::class);
  90. $this->container[Coordinator::class] = $coordinator;
  91. $this->container['Request'] = $this->createMock(Request::class);
  92. $registrationContext = $this->createMock(RegistrationContext::class);
  93. $registrationContext->method('getMiddlewareRegistrations')
  94. ->willReturn([
  95. new MiddlewareRegistration('otherapp', 'foo', true),
  96. new MiddlewareRegistration('otherapp', 'bar', false),
  97. ]);
  98. $this->container['foo'] = new class extends Middleware {
  99. };
  100. $this->container['bar'] = new class extends Middleware {
  101. };
  102. $coordinator->method('getRegistrationContext')->willReturn($registrationContext);
  103. $dispatcher = $this->container['MiddlewareDispatcher'];
  104. $middlewares = $dispatcher->getMiddlewares();
  105. self::assertNotEmpty($middlewares);
  106. foreach ($middlewares as $middleware) {
  107. if ($middleware === $this->container['bar']) {
  108. $this->fail('Container must not register this middleware');
  109. }
  110. if ($middleware === $this->container['foo']) {
  111. // It is done
  112. return;
  113. }
  114. }
  115. $this->fail('Bootstrap registered middleware not found');
  116. }
  117. public function testInvalidAppClass() {
  118. $this->expectException(QueryException::class);
  119. $this->container->query('\OCA\Name\Foo');
  120. }
  121. }