From 2b7b7144d44cdb323039d8384f7a94fc6422e923 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 17 Jun 2020 15:17:59 +0200 Subject: Allow crash reporters registration during app bootstrap Signed-off-by: Christoph Wurst --- .../lib/AppFramework/Bootstrap/CoordinatorTest.php | 6 +++ tests/lib/Support/CrashReport/RegistryTest.php | 48 +++++++++++++++++++--- 2 files changed, 49 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php b/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php index 114872ed54d..c12e5eeb150 100644 --- a/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php +++ b/tests/lib/AppFramework/Bootstrap/CoordinatorTest.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace lib\AppFramework\Bootstrap; use OC\AppFramework\Bootstrap\Coordinator; +use OC\Support\CrashReport\Registry; use OCP\App\IAppManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; @@ -46,6 +47,9 @@ class CoordinatorTest extends TestCase { /** @var IServerContainer|MockObject */ private $serverContainer; + /** @var Registry|MockObject */ + private $crashReporterRegistry; + /** @var IEventDispatcher|MockObject */ private $eventDispatcher; @@ -60,11 +64,13 @@ class CoordinatorTest extends TestCase { $this->appManager = $this->createMock(IAppManager::class); $this->serverContainer = $this->createMock(IServerContainer::class); + $this->crashReporterRegistry = $this->createMock(Registry::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->logger = $this->createMock(ILogger::class); $this->coordinator = new Coordinator( $this->serverContainer, + $this->crashReporterRegistry, $this->eventDispatcher, $this->logger ); diff --git a/tests/lib/Support/CrashReport/RegistryTest.php b/tests/lib/Support/CrashReport/RegistryTest.php index d85b006509e..f88902d7065 100644 --- a/tests/lib/Support/CrashReport/RegistryTest.php +++ b/tests/lib/Support/CrashReport/RegistryTest.php @@ -1,5 +1,7 @@ * @@ -26,6 +28,8 @@ namespace Test\Support\CrashReport; use Exception; use OC\Support\CrashReport\Registry; +use OCP\AppFramework\QueryException; +use OCP\IServerContainer; use OCP\Support\CrashReport\ICollectBreadcrumbs; use OCP\Support\CrashReport\IMessageReporter; use OCP\Support\CrashReport\IReporter; @@ -33,26 +37,60 @@ use Test\TestCase; class RegistryTest extends TestCase { + /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */ + private $serverContainer; + /** @var Registry */ private $registry; protected function setUp(): void { parent::setUp(); - $this->registry = new Registry(); + $this->serverContainer = $this->createMock(IServerContainer::class); + + $this->registry = new Registry( + $this->serverContainer + ); } /** * Doesn't assert anything, just checks whether anything "explodes" */ - public function testDelegateToNone() { + public function testDelegateToNone(): void { $exception = new Exception('test'); $this->registry->delegateReport($exception); $this->addToAssertionCount(1); } - public function testDelegateBreadcrumbCollection() { + public function testRegisterLazyCantLoad(): void { + $reporterClass = '\OCA\MyApp\Reporter'; + $reporter = $this->createMock(IReporter::class); + $this->serverContainer->expects($this->once()) + ->method('query') + ->with($reporterClass) + ->willReturn($reporter); + $reporter->expects($this->once()) + ->method('report'); + $exception = new Exception('test'); + + $this->registry->registerLazy($reporterClass); + $this->registry->delegateReport($exception); + } + + public function testRegisterLazy(): void { + $reporterClass = '\OCA\MyApp\Reporter'; + $this->serverContainer->expects($this->once()) + ->method('query') + ->with($reporterClass) + ->willThrowException(new QueryException()); + $exception = new Exception('test'); + + $this->registry->registerLazy($reporterClass); + $this->registry->delegateReport($exception); + } + + public function testDelegateBreadcrumbCollection(): void { $reporter1 = $this->createMock(IReporter::class); $reporter2 = $this->createMock(ICollectBreadcrumbs::class); $message = 'hello'; @@ -66,7 +104,7 @@ class RegistryTest extends TestCase { $this->registry->delegateBreadcrumb($message, $category); } - public function testDelegateToAll() { + public function testDelegateToAll(): void { $reporter1 = $this->createMock(IReporter::class); $reporter2 = $this->createMock(IReporter::class); $exception = new Exception('test'); @@ -82,7 +120,7 @@ class RegistryTest extends TestCase { $this->registry->delegateReport($exception); } - public function testDelegateMessage() { + public function testDelegateMessage(): void { $reporter1 = $this->createMock(IReporter::class); $reporter2 = $this->createMock(IMessageReporter::class); $message = 'hello'; -- cgit v1.2.3