1
0

RegistryTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2017 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. */
  24. namespace Test\Support\CrashReport;
  25. use Exception;
  26. use OC\Support\CrashReport\Registry;
  27. use OCP\AppFramework\QueryException;
  28. use OCP\IServerContainer;
  29. use OCP\Support\CrashReport\ICollectBreadcrumbs;
  30. use OCP\Support\CrashReport\IMessageReporter;
  31. use OCP\Support\CrashReport\IReporter;
  32. use Test\TestCase;
  33. class RegistryTest extends TestCase {
  34. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  35. private $serverContainer;
  36. /** @var Registry */
  37. private $registry;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->serverContainer = $this->createMock(IServerContainer::class);
  41. $this->registry = new Registry(
  42. $this->serverContainer
  43. );
  44. }
  45. /**
  46. * Doesn't assert anything, just checks whether anything "explodes"
  47. */
  48. public function testDelegateToNone(): void {
  49. $exception = new Exception('test');
  50. $this->registry->delegateReport($exception);
  51. $this->addToAssertionCount(1);
  52. }
  53. public function testRegisterLazyCantLoad(): void {
  54. $reporterClass = '\OCA\MyApp\Reporter';
  55. $reporter = $this->createMock(IReporter::class);
  56. $this->serverContainer->expects($this->once())
  57. ->method('query')
  58. ->with($reporterClass)
  59. ->willReturn($reporter);
  60. $reporter->expects($this->once())
  61. ->method('report');
  62. $exception = new Exception('test');
  63. $this->registry->registerLazy($reporterClass);
  64. $this->registry->delegateReport($exception);
  65. }
  66. public function testRegisterLazy(): void {
  67. $reporterClass = '\OCA\MyApp\Reporter';
  68. $this->serverContainer->expects($this->once())
  69. ->method('query')
  70. ->with($reporterClass)
  71. ->willThrowException(new QueryException());
  72. $exception = new Exception('test');
  73. $this->registry->registerLazy($reporterClass);
  74. $this->registry->delegateReport($exception);
  75. }
  76. public function testDelegateBreadcrumbCollection(): void {
  77. $reporter1 = $this->createMock(IReporter::class);
  78. $reporter2 = $this->createMock(ICollectBreadcrumbs::class);
  79. $message = 'hello';
  80. $category = 'log';
  81. $reporter2->expects($this->once())
  82. ->method('collect')
  83. ->with($message, $category);
  84. $this->registry->register($reporter1);
  85. $this->registry->register($reporter2);
  86. $this->registry->delegateBreadcrumb($message, $category);
  87. }
  88. public function testDelegateToAll(): void {
  89. $reporter1 = $this->createMock(IReporter::class);
  90. $reporter2 = $this->createMock(IReporter::class);
  91. $exception = new Exception('test');
  92. $reporter1->expects($this->once())
  93. ->method('report')
  94. ->with($exception);
  95. $reporter2->expects($this->once())
  96. ->method('report')
  97. ->with($exception);
  98. $this->registry->register($reporter1);
  99. $this->registry->register($reporter2);
  100. $this->registry->delegateReport($exception);
  101. }
  102. public function testDelegateMessage(): void {
  103. $reporter1 = $this->createMock(IReporter::class);
  104. $reporter2 = $this->createMock(IMessageReporter::class);
  105. $message = 'hello';
  106. $reporter2->expects($this->once())
  107. ->method('reportMessage')
  108. ->with($message, []);
  109. $this->registry->register($reporter1);
  110. $this->registry->register($reporter2);
  111. $this->registry->delegateMessage($message);
  112. }
  113. }