RegistrationContextTest.php 4.8 KB

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