ExceptionLoggerPluginTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 PHPUnit_Framework_MockObject_MockObject;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\Exception\ServiceUnavailable;
  36. use Sabre\DAV\Server;
  37. use Test\TestCase;
  38. class TestLogger extends Log {
  39. public $message;
  40. public $level;
  41. public function writeLog(string $app, $entry, int $level) {
  42. $this->level = $level;
  43. $this->message = $entry;
  44. }
  45. }
  46. class ExceptionLoggerPluginTest extends TestCase {
  47. /** @var Server */
  48. private $server;
  49. /** @var PluginToTest */
  50. private $plugin;
  51. /** @var TestLogger | PHPUnit_Framework_MockObject_MockObject */
  52. private $logger;
  53. private function init() {
  54. $config = $this->createMock(SystemConfig::class);
  55. $config->expects($this->any())
  56. ->method('getValue')
  57. ->willReturnCallback(function ($key, $default) {
  58. switch ($key) {
  59. case 'loglevel':
  60. return 0;
  61. default:
  62. return $default;
  63. }
  64. });
  65. $this->server = new Server();
  66. $this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $config), $config);
  67. $this->plugin = new PluginToTest('unit-test', $this->logger);
  68. $this->plugin->initialize($this->server);
  69. }
  70. /**
  71. * @dataProvider providesExceptions
  72. */
  73. public function testLogging($expectedLogLevel, $expectedMessage, $exception) {
  74. $this->init();
  75. $this->plugin->logException($exception);
  76. $this->assertEquals($expectedLogLevel, $this->logger->level);
  77. $this->assertEquals(get_class($exception), $this->logger->message['Exception']);
  78. $this->assertEquals($expectedMessage, $this->logger->message['Message']);
  79. }
  80. public function providesExceptions() {
  81. return [
  82. [0, '', new NotFound()],
  83. [0, 'System in maintenance mode.', new ServiceUnavailable('System in maintenance mode.')],
  84. [4, 'Upgrade needed', new ServiceUnavailable('Upgrade needed')],
  85. [4, 'This path leads to nowhere', new InvalidPath('This path leads to nowhere')]
  86. ];
  87. }
  88. }