diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-05-07 21:10:30 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-06-17 09:22:21 +0200 |
commit | 69571fb536c7ad231ea4e5d350f8112a5923c6e1 (patch) | |
tree | afcf6f455248ef99fcb2ee0f315e4b5db8ba8a59 /tests | |
parent | d1b03f5adf6d3371339bbcc3c7bfb8a5e2614789 (diff) | |
download | nextcloud-server-69571fb536c7ad231ea4e5d350f8112a5923c6e1.tar.gz nextcloud-server-69571fb536c7ad231ea4e5d350f8112a5923c6e1.zip |
Add dedicated API for apps' bootstrapping process
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
3 files changed, 345 insertions, 0 deletions
diff --git a/tests/lib/AppFramework/Bootstrap/BootContextTest.php b/tests/lib/AppFramework/Bootstrap/BootContextTest.php new file mode 100644 index 00000000000..219c6d7e782 --- /dev/null +++ b/tests/lib/AppFramework/Bootstrap/BootContextTest.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace lib\AppFramework\Bootstrap; + +use OC\AppFramework\Bootstrap\BootContext; +use OCP\AppFramework\IAppContainer; +use OCP\IServerContainer; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class BootContextTest extends TestCase { + + /** @var IAppContainer|MockObject */ + private $appContainer; + + /** @var BootContext */ + private $context; + + protected function setUp(): void { + parent::setUp(); + + $this->appContainer = $this->createMock(IAppContainer::class); + + $this->context = new BootContext( + $this->appContainer + ); + } + + public function testGetAppContainer(): void { + $container = $this->context->getAppContainer(); + + $this->assertSame($this->appContainer, $container); + } + + public function testGetServerContainer(): void { + $serverContainer = $this->createMock(IServerContainer::class); + $this->appContainer->method('getServer') + ->willReturn($serverContainer); + + $container = $this->context->getServerContainer(); + + $this->assertSame($serverContainer, $container); + } +} diff --git a/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php b/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php new file mode 100644 index 00000000000..114872ed54d --- /dev/null +++ b/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php @@ -0,0 +1,116 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace lib\AppFramework\Bootstrap; + +use OC\AppFramework\Bootstrap\Coordinator; +use OCP\App\IAppManager; +use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\QueryException; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\ILogger; +use OCP\IServerContainer; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class CoordinatorTest extends TestCase { + + /** @var IAppManager|MockObject */ + private $appManager; + + /** @var IServerContainer|MockObject */ + private $serverContainer; + + /** @var IEventDispatcher|MockObject */ + private $eventDispatcher; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var Coordinator */ + private $coordinator; + + protected function setUp(): void { + parent::setUp(); + + $this->appManager = $this->createMock(IAppManager::class); + $this->serverContainer = $this->createMock(IServerContainer::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->logger = $this->createMock(ILogger::class); + + $this->coordinator = new Coordinator( + $this->serverContainer, + $this->eventDispatcher, + $this->logger + ); + } + + public function testBootAppNotLoadable(): void { + $appId = 'settings'; + $this->serverContainer->expects($this->once()) + ->method('query') + ->with(\OCA\Settings\AppInfo\Application::class) + ->willThrowException(new QueryException("")); + $this->logger->expects($this->once()) + ->method('logException'); + + $this->coordinator->bootApp($appId); + } + + public function testBootAppNotBootable(): void { + $appId = 'settings'; + $mockApp = $this->createMock(\OCA\Settings\AppInfo\Application::class); + $this->serverContainer->expects($this->once()) + ->method('query') + ->with(\OCA\Settings\AppInfo\Application::class) + ->willReturn($mockApp); + + $this->coordinator->bootApp($appId); + } + + public function testBootApp(): void { + $appId = 'settings'; + $mockApp = new class extends App implements IBootstrap { + public function __construct() { + parent::__construct('test', []); + } + + public function register(IRegistrationContext $context): void { + } + + public function boot(IBootContext $context): void { + } + }; + $this->serverContainer->expects($this->once()) + ->method('query') + ->with(\OCA\Settings\AppInfo\Application::class) + ->willReturn($mockApp); + + $this->coordinator->bootApp($appId); + } +} diff --git a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php new file mode 100644 index 00000000000..8441b6e983d --- /dev/null +++ b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php @@ -0,0 +1,162 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace lib\AppFramework\Bootstrap; + +use OC\AppFramework\Bootstrap\RegistrationContext; +use OCP\AppFramework\App; +use OCP\AppFramework\IAppContainer; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class RegistrationContextTest extends TestCase { + + /** @var ILogger|MockObject */ + private $logger; + + /** @var RegistrationContext */ + private $context; + + protected function setUp(): void { + parent::setUp(); + + $this->logger = $this->createMock(ILogger::class); + + $this->context = new RegistrationContext( + $this->logger + ); + } + + public function testRegisterCapability(): void { + $app = $this->createMock(App::class); + $name = 'abc'; + $container = $this->createMock(IAppContainer::class); + $app->method('getContainer') + ->willReturn($container); + $container->expects($this->once()) + ->method('registerCapability') + ->with($name); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerCapability($name); + $this->context->delegateCapabilityRegistrations([ + 'myapp' => $app, + ]); + } + + public function testRegisterEventListener(): void { + $event = 'abc'; + $service = 'def'; + $dispatcher = $this->createMock(IEventDispatcher::class); + $dispatcher->expects($this->once()) + ->method('addServiceListener') + ->with($event, $service, 0); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerEventListener($event, $service); + $this->context->delegateEventListenerRegistrations($dispatcher); + } + + public function testRegisterService(): void { + $app = $this->createMock(App::class); + $service = 'abc'; + $factory = function () { + return 'def'; + }; + $container = $this->createMock(IAppContainer::class); + $app->method('getContainer') + ->willReturn($container); + $container->expects($this->once()) + ->method('registerService') + ->with($service, $factory, true); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerService($service, $factory); + $this->context->delegateContainerRegistrations([ + 'myapp' => $app, + ]); + } + + public function testRegisterServiceAlias(): void { + $app = $this->createMock(App::class); + $alias = 'abc'; + $target = 'def'; + $container = $this->createMock(IAppContainer::class); + $app->method('getContainer') + ->willReturn($container); + $container->expects($this->once()) + ->method('registerAlias') + ->with($alias, $target); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerServiceAlias($alias, $target); + $this->context->delegateContainerRegistrations([ + 'myapp' => $app, + ]); + } + + public function testRegisterParameter(): void { + $app = $this->createMock(App::class); + $name = 'abc'; + $value = 'def'; + $container = $this->createMock(IAppContainer::class); + $app->method('getContainer') + ->willReturn($container); + $container->expects($this->once()) + ->method('registerParameter') + ->with($name, $value); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerParameter($name, $value); + $this->context->delegateContainerRegistrations([ + 'myapp' => $app, + ]); + } + + public function testRegisterMiddleware(): void { + $app = $this->createMock(App::class); + $name = 'abc'; + $container = $this->createMock(IAppContainer::class); + $app->method('getContainer') + ->willReturn($container); + $container->expects($this->once()) + ->method('registerMiddleware') + ->with($name); + $this->logger->expects($this->never()) + ->method('logException'); + + $this->context->for('myapp')->registerMiddleware($name); + $this->context->delegateMiddlewareRegistrations([ + 'myapp' => $app, + ]); + } +} |