AddServerMiddlewareTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation\Tests\Middleware;
  28. use OCA\Federation\Controller\SettingsController;
  29. use OCA\Federation\Middleware\AddServerMiddleware;
  30. use OCP\AppFramework\Http;
  31. use OCP\HintException;
  32. use OCP\IL10N;
  33. use Psr\Log\LoggerInterface;
  34. use Test\TestCase;
  35. class AddServerMiddlewareTest extends TestCase {
  36. /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
  37. private $logger;
  38. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
  39. private $l10n;
  40. private AddServerMiddleware $middleware;
  41. /** @var \PHPUnit\Framework\MockObject\MockObject | SettingsController */
  42. private $controller;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  46. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  47. $this->controller = $this->getMockBuilder(SettingsController::class)
  48. ->disableOriginalConstructor()->getMock();
  49. $this->middleware = new AddServerMiddleware(
  50. 'AddServerMiddlewareTest',
  51. $this->l10n,
  52. $this->logger
  53. );
  54. }
  55. /**
  56. * @dataProvider dataTestAfterException
  57. *
  58. * @param \Exception $exception
  59. * @param string $hint
  60. */
  61. public function testAfterException($exception, $hint) {
  62. $this->logger->expects($this->once())->method('error');
  63. $this->l10n->expects($this->any())->method('t')
  64. ->willReturnCallback(
  65. function (string $message): string {
  66. return $message;
  67. }
  68. );
  69. $result = $this->middleware->afterException($this->controller, 'method', $exception);
  70. $this->assertSame(Http::STATUS_BAD_REQUEST,
  71. $result->getStatus()
  72. );
  73. $data = $result->getData();
  74. $this->assertSame($hint,
  75. $data['message']
  76. );
  77. }
  78. public function dataTestAfterException() {
  79. return [
  80. [new HintException('message', 'hint'), 'hint'],
  81. [new \Exception('message'), 'message'],
  82. ];
  83. }
  84. }