ExceptionLoggerPluginTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Log;
  31. use OC\SystemConfig;
  32. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  33. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  34. use OCA\DAV\Exception\ServerMaintenanceMode;
  35. use Psr\Log\LoggerInterface;
  36. use Sabre\DAV\Exception\NotFound;
  37. use Sabre\DAV\Exception\ServiceUnavailable;
  38. use Sabre\DAV\Server;
  39. use Test\TestCase;
  40. class ExceptionLoggerPluginTest extends TestCase {
  41. /** @var Server */
  42. private $server;
  43. /** @var ExceptionLoggerPlugin */
  44. private $plugin;
  45. /** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
  46. private $logger;
  47. private function init(): void {
  48. $config = $this->createMock(SystemConfig::class);
  49. $config->expects($this->any())
  50. ->method('getValue')
  51. ->willReturnCallback(function ($key, $default) {
  52. switch ($key) {
  53. case 'loglevel':
  54. return 0;
  55. default:
  56. return $default;
  57. }
  58. });
  59. $this->server = new Server();
  60. $this->logger = $this->createMock(LoggerInterface::class);
  61. $this->plugin = new ExceptionLoggerPlugin('unit-test', $this->logger);
  62. $this->plugin->initialize($this->server);
  63. }
  64. /**
  65. * @dataProvider providesExceptions
  66. */
  67. public function testLogging(string $expectedLogLevel, \Throwable $e): void {
  68. $this->init();
  69. $this->logger->expects($this->once())
  70. ->method($expectedLogLevel)
  71. ->with($e->getMessage(), ['app' => 'unit-test','exception' => $e]);
  72. $this->plugin->logException($e);
  73. }
  74. public function providesExceptions() {
  75. return [
  76. ['debug', new NotFound()],
  77. ['debug', new ServerMaintenanceMode('System is in maintenance mode.')],
  78. // Faking a translation
  79. ['debug', new ServerMaintenanceMode('Syst3m 1s 1n m41nt3n4nc3 m0d3.')],
  80. ['debug', new ServerMaintenanceMode('Upgrade needed')],
  81. ['critical', new InvalidPath('This path leads to nowhere')]
  82. ];
  83. }
  84. }