1
0

RegistrationContextTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\RegistrationContext;
  9. use OC\AppFramework\Bootstrap\ServiceRegistration;
  10. use OC\Core\Middleware\TwoFactorMiddleware;
  11. use OCP\AppFramework\App;
  12. use OCP\AppFramework\IAppContainer;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Psr\Log\LoggerInterface;
  16. use Test\TestCase;
  17. class RegistrationContextTest extends TestCase {
  18. /** @var LoggerInterface|MockObject */
  19. private $logger;
  20. /** @var RegistrationContext */
  21. private $context;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->logger = $this->createMock(LoggerInterface::class);
  25. $this->context = new RegistrationContext(
  26. $this->logger
  27. );
  28. }
  29. public function testRegisterCapability(): void {
  30. $app = $this->createMock(App::class);
  31. $name = 'abc';
  32. $container = $this->createMock(IAppContainer::class);
  33. $app->method('getContainer')
  34. ->willReturn($container);
  35. $container->expects($this->once())
  36. ->method('registerCapability')
  37. ->with($name);
  38. $this->logger->expects($this->never())
  39. ->method('error');
  40. $this->context->for('myapp')->registerCapability($name);
  41. $this->context->delegateCapabilityRegistrations([
  42. 'myapp' => $app,
  43. ]);
  44. }
  45. public function testRegisterEventListener(): void {
  46. $event = 'abc';
  47. $service = 'def';
  48. $dispatcher = $this->createMock(IEventDispatcher::class);
  49. $dispatcher->expects($this->once())
  50. ->method('addServiceListener')
  51. ->with($event, $service, 0);
  52. $this->logger->expects($this->never())
  53. ->method('error');
  54. $this->context->for('myapp')->registerEventListener($event, $service);
  55. $this->context->delegateEventListenerRegistrations($dispatcher);
  56. }
  57. /**
  58. * @dataProvider dataProvider_TrueFalse
  59. */
  60. public function testRegisterService(bool $shared): void {
  61. $app = $this->createMock(App::class);
  62. $service = 'abc';
  63. $factory = function () {
  64. return 'def';
  65. };
  66. $container = $this->createMock(IAppContainer::class);
  67. $app->method('getContainer')
  68. ->willReturn($container);
  69. $container->expects($this->once())
  70. ->method('registerService')
  71. ->with($service, $factory, $shared);
  72. $this->logger->expects($this->never())
  73. ->method('error');
  74. $this->context->for('myapp')->registerService($service, $factory, $shared);
  75. $this->context->delegateContainerRegistrations([
  76. 'myapp' => $app,
  77. ]);
  78. }
  79. public function testRegisterServiceAlias(): void {
  80. $app = $this->createMock(App::class);
  81. $alias = 'abc';
  82. $target = 'def';
  83. $container = $this->createMock(IAppContainer::class);
  84. $app->method('getContainer')
  85. ->willReturn($container);
  86. $container->expects($this->once())
  87. ->method('registerAlias')
  88. ->with($alias, $target);
  89. $this->logger->expects($this->never())
  90. ->method('error');
  91. $this->context->for('myapp')->registerServiceAlias($alias, $target);
  92. $this->context->delegateContainerRegistrations([
  93. 'myapp' => $app,
  94. ]);
  95. }
  96. public function testRegisterParameter(): void {
  97. $app = $this->createMock(App::class);
  98. $name = 'abc';
  99. $value = 'def';
  100. $container = $this->createMock(IAppContainer::class);
  101. $app->method('getContainer')
  102. ->willReturn($container);
  103. $container->expects($this->once())
  104. ->method('registerParameter')
  105. ->with($name, $value);
  106. $this->logger->expects($this->never())
  107. ->method('error');
  108. $this->context->for('myapp')->registerParameter($name, $value);
  109. $this->context->delegateContainerRegistrations([
  110. 'myapp' => $app,
  111. ]);
  112. }
  113. public function testRegisterUserMigrator(): void {
  114. $appIdA = 'myapp';
  115. $migratorClassA = 'OCA\App\UserMigration\AppMigrator';
  116. $appIdB = 'otherapp';
  117. $migratorClassB = 'OCA\OtherApp\UserMigration\OtherAppMigrator';
  118. $serviceRegistrationA = new ServiceRegistration($appIdA, $migratorClassA);
  119. $serviceRegistrationB = new ServiceRegistration($appIdB, $migratorClassB);
  120. $this->context
  121. ->for($appIdA)
  122. ->registerUserMigrator($migratorClassA);
  123. $this->context
  124. ->for($appIdB)
  125. ->registerUserMigrator($migratorClassB);
  126. $this->assertEquals(
  127. [
  128. $serviceRegistrationA,
  129. $serviceRegistrationB,
  130. ],
  131. $this->context->getUserMigrators(),
  132. );
  133. }
  134. public function dataProvider_TrueFalse() {
  135. return[
  136. [true],
  137. [false]
  138. ];
  139. }
  140. public function testGetMiddlewareRegistrations(): void {
  141. $this->context->registerMiddleware('core', TwoFactorMiddleware::class, false);
  142. $registrations = $this->context->getMiddlewareRegistrations();
  143. self::assertNotEmpty($registrations);
  144. self::assertSame('core', $registrations[0]->getAppId());
  145. self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService());
  146. }
  147. }