ExceptionLoggerPluginTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  30. use OC\SystemConfig;
  31. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  32. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  33. use OCA\DAV\Exception\ServerMaintenanceMode;
  34. use Psr\Log\LoggerInterface;
  35. use Sabre\DAV\Exception\NotFound;
  36. use Sabre\DAV\Server;
  37. use Test\TestCase;
  38. class ExceptionLoggerPluginTest extends TestCase {
  39. /** @var Server */
  40. private $server;
  41. /** @var ExceptionLoggerPlugin */
  42. private $plugin;
  43. /** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
  44. private $logger;
  45. private function init(): void {
  46. $config = $this->createMock(SystemConfig::class);
  47. $config->expects($this->any())
  48. ->method('getValue')
  49. ->willReturnCallback(function ($key, $default) {
  50. switch ($key) {
  51. case 'loglevel':
  52. return 0;
  53. default:
  54. return $default;
  55. }
  56. });
  57. $this->server = new Server();
  58. $this->logger = $this->createMock(LoggerInterface::class);
  59. $this->plugin = new ExceptionLoggerPlugin('unit-test', $this->logger);
  60. $this->plugin->initialize($this->server);
  61. }
  62. /**
  63. * @dataProvider providesExceptions
  64. */
  65. public function testLogging(string $expectedLogLevel, \Throwable $e): void {
  66. $this->init();
  67. $this->logger->expects($this->once())
  68. ->method($expectedLogLevel)
  69. ->with($e->getMessage(), ['app' => 'unit-test','exception' => $e]);
  70. $this->plugin->logException($e);
  71. }
  72. public function providesExceptions() {
  73. return [
  74. ['debug', new NotFound()],
  75. ['debug', new ServerMaintenanceMode('System is in maintenance mode.')],
  76. // Faking a translation
  77. ['debug', new ServerMaintenanceMode('Syst3m 1s 1n m41nt3n4nc3 m0d3.')],
  78. ['debug', new ServerMaintenanceMode('Upgrade needed')],
  79. ['critical', new InvalidPath('This path leads to nowhere')]
  80. ];
  81. }
  82. }