ExceptionLoggerPluginTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OC\SystemConfig;
  9. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  10. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  11. use OCA\DAV\Exception\ServerMaintenanceMode;
  12. use Psr\Log\LoggerInterface;
  13. use Sabre\DAV\Exception\NotFound;
  14. use Sabre\DAV\Server;
  15. use Test\TestCase;
  16. class ExceptionLoggerPluginTest extends TestCase {
  17. /** @var Server */
  18. private $server;
  19. /** @var ExceptionLoggerPlugin */
  20. private $plugin;
  21. /** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
  22. private $logger;
  23. private function init(): void {
  24. $config = $this->createMock(SystemConfig::class);
  25. $config->expects($this->any())
  26. ->method('getValue')
  27. ->willReturnCallback(function ($key, $default) {
  28. switch ($key) {
  29. case 'loglevel':
  30. return 0;
  31. default:
  32. return $default;
  33. }
  34. });
  35. $this->server = new Server();
  36. $this->logger = $this->createMock(LoggerInterface::class);
  37. $this->plugin = new ExceptionLoggerPlugin('unit-test', $this->logger);
  38. $this->plugin->initialize($this->server);
  39. }
  40. /**
  41. * @dataProvider providesExceptions
  42. */
  43. public function testLogging(string $expectedLogLevel, \Throwable $e): void {
  44. $this->init();
  45. $this->logger->expects($this->once())
  46. ->method($expectedLogLevel)
  47. ->with($e->getMessage(), ['app' => 'unit-test','exception' => $e]);
  48. $this->plugin->logException($e);
  49. }
  50. public function providesExceptions() {
  51. return [
  52. ['debug', new NotFound()],
  53. ['debug', new ServerMaintenanceMode('System is in maintenance mode.')],
  54. // Faking a translation
  55. ['debug', new ServerMaintenanceMode('Syst3m 1s 1n m41nt3n4nc3 m0d3.')],
  56. ['debug', new ServerMaintenanceMode('Upgrade needed')],
  57. ['critical', new InvalidPath('This path leads to nowhere')]
  58. ];
  59. }
  60. }