RegistryTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Support\CrashReport;
  8. use Exception;
  9. use OC\Support\CrashReport\Registry;
  10. use OCP\AppFramework\QueryException;
  11. use OCP\IServerContainer;
  12. use OCP\Support\CrashReport\ICollectBreadcrumbs;
  13. use OCP\Support\CrashReport\IMessageReporter;
  14. use OCP\Support\CrashReport\IReporter;
  15. use Test\TestCase;
  16. class RegistryTest extends TestCase {
  17. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  18. private $serverContainer;
  19. /** @var Registry */
  20. private $registry;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->serverContainer = $this->createMock(IServerContainer::class);
  24. $this->registry = new Registry(
  25. $this->serverContainer
  26. );
  27. }
  28. /**
  29. * Doesn't assert anything, just checks whether anything "explodes"
  30. */
  31. public function testDelegateToNone(): void {
  32. $exception = new Exception('test');
  33. $this->registry->delegateReport($exception);
  34. $this->addToAssertionCount(1);
  35. }
  36. public function testRegisterLazyCantLoad(): void {
  37. $reporterClass = '\OCA\MyApp\Reporter';
  38. $reporter = $this->createMock(IReporter::class);
  39. $this->serverContainer->expects($this->once())
  40. ->method('query')
  41. ->with($reporterClass)
  42. ->willReturn($reporter);
  43. $reporter->expects($this->once())
  44. ->method('report');
  45. $exception = new Exception('test');
  46. $this->registry->registerLazy($reporterClass);
  47. $this->registry->delegateReport($exception);
  48. }
  49. public function testRegisterLazy(): void {
  50. $reporterClass = '\OCA\MyApp\Reporter';
  51. $this->serverContainer->expects($this->once())
  52. ->method('query')
  53. ->with($reporterClass)
  54. ->willThrowException(new QueryException());
  55. $exception = new Exception('test');
  56. $this->registry->registerLazy($reporterClass);
  57. $this->registry->delegateReport($exception);
  58. }
  59. public function testDelegateBreadcrumbCollection(): void {
  60. $reporter1 = $this->createMock(IReporter::class);
  61. $reporter2 = $this->createMock(ICollectBreadcrumbs::class);
  62. $message = 'hello';
  63. $category = 'log';
  64. $reporter2->expects($this->once())
  65. ->method('collect')
  66. ->with($message, $category);
  67. $this->registry->register($reporter1);
  68. $this->registry->register($reporter2);
  69. $this->registry->delegateBreadcrumb($message, $category);
  70. }
  71. public function testDelegateToAll(): void {
  72. $reporter1 = $this->createMock(IReporter::class);
  73. $reporter2 = $this->createMock(IReporter::class);
  74. $exception = new Exception('test');
  75. $reporter1->expects($this->once())
  76. ->method('report')
  77. ->with($exception);
  78. $reporter2->expects($this->once())
  79. ->method('report')
  80. ->with($exception);
  81. $this->registry->register($reporter1);
  82. $this->registry->register($reporter2);
  83. $this->registry->delegateReport($exception);
  84. }
  85. public function testDelegateMessage(): void {
  86. $reporter1 = $this->createMock(IReporter::class);
  87. $reporter2 = $this->createMock(IMessageReporter::class);
  88. $message = 'hello';
  89. $reporter2->expects($this->once())
  90. ->method('reportMessage')
  91. ->with($message, []);
  92. $this->registry->register($reporter1);
  93. $this->registry->register($reporter2);
  94. $this->registry->delegateMessage($message);
  95. }
  96. }