You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExceptionLoggerPluginTest.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OC\Log;
  30. use OC\SystemConfig;
  31. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  32. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin as PluginToTest;
  33. use Sabre\DAV\Exception\NotFound;
  34. use Sabre\DAV\Exception\ServiceUnavailable;
  35. use Sabre\DAV\Server;
  36. use Test\TestCase;
  37. class TestLogger extends Log {
  38. public $message;
  39. public $level;
  40. public function writeLog(string $app, $entry, int $level) {
  41. $this->level = $level;
  42. $this->message = $entry;
  43. }
  44. }
  45. class ExceptionLoggerPluginTest extends TestCase {
  46. /** @var Server */
  47. private $server;
  48. /** @var PluginToTest */
  49. private $plugin;
  50. /** @var TestLogger | \PHPUnit\Framework\MockObject\MockObject */
  51. private $logger;
  52. private function init() {
  53. $config = $this->createMock(SystemConfig::class);
  54. $config->expects($this->any())
  55. ->method('getValue')
  56. ->willReturnCallback(function ($key, $default) {
  57. switch ($key) {
  58. case 'loglevel':
  59. return 0;
  60. default:
  61. return $default;
  62. }
  63. });
  64. $this->server = new Server();
  65. $this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $config), $config);
  66. $this->plugin = new PluginToTest('unit-test', $this->logger);
  67. $this->plugin->initialize($this->server);
  68. }
  69. /**
  70. * @dataProvider providesExceptions
  71. */
  72. public function testLogging($expectedLogLevel, $expectedMessage, $exception) {
  73. $this->init();
  74. $this->plugin->logException($exception);
  75. $this->assertEquals($expectedLogLevel, $this->logger->level);
  76. $this->assertEquals(get_class($exception), $this->logger->message['Exception']);
  77. $this->assertEquals($expectedMessage, $this->logger->message['Message']);
  78. }
  79. public function providesExceptions() {
  80. return [
  81. [0, '', new NotFound()],
  82. [0, 'System in maintenance mode.', new ServiceUnavailable('System in maintenance mode.')],
  83. [4, 'Upgrade needed', new ServiceUnavailable('Upgrade needed')],
  84. [4, 'This path leads to nowhere', new InvalidPath('This path leads to nowhere')]
  85. ];
  86. }
  87. }