1
0

DIContainerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @author Morris Jobke
  7. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @copyright 2013 Morris Jobke <morris.jobke@gmail.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\DependencyInjection;
  25. use OC\AppFramework\Bootstrap\Coordinator;
  26. use OC\AppFramework\Bootstrap\MiddlewareRegistration;
  27. use OC\AppFramework\Bootstrap\RegistrationContext;
  28. use OC\AppFramework\DependencyInjection\DIContainer;
  29. use OC\AppFramework\Http\Request;
  30. use OC\AppFramework\Middleware\Security\SecurityMiddleware;
  31. use OCP\AppFramework\Middleware;
  32. use OCP\AppFramework\QueryException;
  33. use OCP\IConfig;
  34. use OCP\IRequestId;
  35. /**
  36. * @group DB
  37. */
  38. class DIContainerTest extends \Test\TestCase {
  39. /** @var DIContainer|\PHPUnit\Framework\MockObject\MockObject */
  40. private $container;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->container = $this->getMockBuilder(DIContainer::class)
  44. ->setMethods(['isAdminUser'])
  45. ->setConstructorArgs(['name'])
  46. ->getMock();
  47. }
  48. public function testProvidesRequest() {
  49. $this->assertTrue(isset($this->container['Request']));
  50. }
  51. public function testProvidesMiddlewareDispatcher() {
  52. $this->assertTrue(isset($this->container['MiddlewareDispatcher']));
  53. }
  54. public function testProvidesAppName() {
  55. $this->assertTrue(isset($this->container['AppName']));
  56. }
  57. public function testAppNameIsSetCorrectly() {
  58. $this->assertEquals('name', $this->container['AppName']);
  59. }
  60. public function testMiddlewareDispatcherIncludesSecurityMiddleware() {
  61. $this->container['Request'] = new Request(
  62. ['method' => 'GET'],
  63. $this->createMock(IRequestId::class),
  64. $this->createMock(IConfig::class)
  65. );
  66. $dispatcher = $this->container['MiddlewareDispatcher'];
  67. $middlewares = $dispatcher->getMiddlewares();
  68. $found = false;
  69. foreach ($middlewares as $middleware) {
  70. if ($middleware instanceof SecurityMiddleware) {
  71. $found = true;
  72. }
  73. }
  74. $this->assertTrue($found);
  75. }
  76. public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): void {
  77. $coordinator = $this->createMock(Coordinator::class);
  78. $this->container[Coordinator::class] = $coordinator;
  79. $this->container['Request'] = $this->createMock(Request::class);
  80. $registrationContext = $this->createMock(RegistrationContext::class);
  81. $registrationContext->method('getMiddlewareRegistrations')
  82. ->willReturn([
  83. new MiddlewareRegistration($this->container['appName'], 'foo', false),
  84. new MiddlewareRegistration('otherapp', 'bar', false),
  85. ]);
  86. $this->container['foo'] = new class extends Middleware {
  87. };
  88. $this->container['bar'] = new class extends Middleware {
  89. };
  90. $coordinator->method('getRegistrationContext')->willReturn($registrationContext);
  91. $dispatcher = $this->container['MiddlewareDispatcher'];
  92. $middlewares = $dispatcher->getMiddlewares();
  93. self::assertNotEmpty($middlewares);
  94. foreach ($middlewares as $middleware) {
  95. if ($middleware === $this->container['bar']) {
  96. $this->fail('Container must not register this middleware');
  97. }
  98. if ($middleware === $this->container['foo']) {
  99. // It is done
  100. return;
  101. }
  102. }
  103. $this->fail('Bootstrap registered middleware not found');
  104. }
  105. public function testMiddlewareDispatcherIncludesGlobalBootstrapMiddlewares(): void {
  106. $coordinator = $this->createMock(Coordinator::class);
  107. $this->container[Coordinator::class] = $coordinator;
  108. $this->container['Request'] = $this->createMock(Request::class);
  109. $registrationContext = $this->createMock(RegistrationContext::class);
  110. $registrationContext->method('getMiddlewareRegistrations')
  111. ->willReturn([
  112. new MiddlewareRegistration('otherapp', 'foo', true),
  113. new MiddlewareRegistration('otherapp', 'bar', false),
  114. ]);
  115. $this->container['foo'] = new class extends Middleware {
  116. };
  117. $this->container['bar'] = new class extends Middleware {
  118. };
  119. $coordinator->method('getRegistrationContext')->willReturn($registrationContext);
  120. $dispatcher = $this->container['MiddlewareDispatcher'];
  121. $middlewares = $dispatcher->getMiddlewares();
  122. self::assertNotEmpty($middlewares);
  123. foreach ($middlewares as $middleware) {
  124. if ($middleware === $this->container['bar']) {
  125. $this->fail('Container must not register this middleware');
  126. }
  127. if ($middleware === $this->container['foo']) {
  128. // It is done
  129. return;
  130. }
  131. }
  132. $this->fail('Bootstrap registered middleware not found');
  133. }
  134. public function testInvalidAppClass() {
  135. $this->expectException(QueryException::class);
  136. $this->container->query('\OCA\Name\Foo');
  137. }
  138. }