1
0

RegistrationContextTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program 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 License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace lib\AppFramework\Bootstrap;
  24. use OC\AppFramework\Bootstrap\RegistrationContext;
  25. use OC\AppFramework\Bootstrap\ServiceRegistration;
  26. use OC\Core\Middleware\TwoFactorMiddleware;
  27. use OCP\AppFramework\App;
  28. use OCP\AppFramework\IAppContainer;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Psr\Log\LoggerInterface;
  32. use Test\TestCase;
  33. class RegistrationContextTest extends TestCase {
  34. /** @var LoggerInterface|MockObject */
  35. private $logger;
  36. /** @var RegistrationContext */
  37. private $context;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->logger = $this->createMock(LoggerInterface::class);
  41. $this->context = new RegistrationContext(
  42. $this->logger
  43. );
  44. }
  45. public function testRegisterCapability(): void {
  46. $app = $this->createMock(App::class);
  47. $name = 'abc';
  48. $container = $this->createMock(IAppContainer::class);
  49. $app->method('getContainer')
  50. ->willReturn($container);
  51. $container->expects($this->once())
  52. ->method('registerCapability')
  53. ->with($name);
  54. $this->logger->expects($this->never())
  55. ->method('error');
  56. $this->context->for('myapp')->registerCapability($name);
  57. $this->context->delegateCapabilityRegistrations([
  58. 'myapp' => $app,
  59. ]);
  60. }
  61. public function testRegisterEventListener(): void {
  62. $event = 'abc';
  63. $service = 'def';
  64. $dispatcher = $this->createMock(IEventDispatcher::class);
  65. $dispatcher->expects($this->once())
  66. ->method('addServiceListener')
  67. ->with($event, $service, 0);
  68. $this->logger->expects($this->never())
  69. ->method('error');
  70. $this->context->for('myapp')->registerEventListener($event, $service);
  71. $this->context->delegateEventListenerRegistrations($dispatcher);
  72. }
  73. /**
  74. * @dataProvider dataProvider_TrueFalse
  75. */
  76. public function testRegisterService(bool $shared): void {
  77. $app = $this->createMock(App::class);
  78. $service = 'abc';
  79. $factory = function () {
  80. return 'def';
  81. };
  82. $container = $this->createMock(IAppContainer::class);
  83. $app->method('getContainer')
  84. ->willReturn($container);
  85. $container->expects($this->once())
  86. ->method('registerService')
  87. ->with($service, $factory, $shared);
  88. $this->logger->expects($this->never())
  89. ->method('error');
  90. $this->context->for('myapp')->registerService($service, $factory, $shared);
  91. $this->context->delegateContainerRegistrations([
  92. 'myapp' => $app,
  93. ]);
  94. }
  95. public function testRegisterServiceAlias(): void {
  96. $app = $this->createMock(App::class);
  97. $alias = 'abc';
  98. $target = 'def';
  99. $container = $this->createMock(IAppContainer::class);
  100. $app->method('getContainer')
  101. ->willReturn($container);
  102. $container->expects($this->once())
  103. ->method('registerAlias')
  104. ->with($alias, $target);
  105. $this->logger->expects($this->never())
  106. ->method('error');
  107. $this->context->for('myapp')->registerServiceAlias($alias, $target);
  108. $this->context->delegateContainerRegistrations([
  109. 'myapp' => $app,
  110. ]);
  111. }
  112. public function testRegisterParameter(): void {
  113. $app = $this->createMock(App::class);
  114. $name = 'abc';
  115. $value = 'def';
  116. $container = $this->createMock(IAppContainer::class);
  117. $app->method('getContainer')
  118. ->willReturn($container);
  119. $container->expects($this->once())
  120. ->method('registerParameter')
  121. ->with($name, $value);
  122. $this->logger->expects($this->never())
  123. ->method('error');
  124. $this->context->for('myapp')->registerParameter($name, $value);
  125. $this->context->delegateContainerRegistrations([
  126. 'myapp' => $app,
  127. ]);
  128. }
  129. public function testRegisterUserMigrator(): void {
  130. $appIdA = 'myapp';
  131. $migratorClassA = 'OCA\App\UserMigration\AppMigrator';
  132. $appIdB = 'otherapp';
  133. $migratorClassB = 'OCA\OtherApp\UserMigration\OtherAppMigrator';
  134. $serviceRegistrationA = new ServiceRegistration($appIdA, $migratorClassA);
  135. $serviceRegistrationB = new ServiceRegistration($appIdB, $migratorClassB);
  136. $this->context
  137. ->for($appIdA)
  138. ->registerUserMigrator($migratorClassA);
  139. $this->context
  140. ->for($appIdB)
  141. ->registerUserMigrator($migratorClassB);
  142. $this->assertEquals(
  143. [
  144. $serviceRegistrationA,
  145. $serviceRegistrationB,
  146. ],
  147. $this->context->getUserMigrators(),
  148. );
  149. }
  150. public function dataProvider_TrueFalse() {
  151. return[
  152. [true],
  153. [false]
  154. ];
  155. }
  156. public function testGetMiddlewareRegistrations(): void {
  157. $this->context->registerMiddleware('core', TwoFactorMiddleware::class, false);
  158. $registrations = $this->context->getMiddlewareRegistrations();
  159. self::assertNotEmpty($registrations);
  160. self::assertSame('core', $registrations[0]->getAppId());
  161. self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService());
  162. }
  163. }