1
0

AddServerMiddlewareTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Tests\Middleware;
  8. use OCA\Federation\Controller\SettingsController;
  9. use OCA\Federation\Middleware\AddServerMiddleware;
  10. use OCP\AppFramework\Http;
  11. use OCP\HintException;
  12. use OCP\IL10N;
  13. use Psr\Log\LoggerInterface;
  14. use Test\TestCase;
  15. class AddServerMiddlewareTest extends TestCase {
  16. /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
  17. private $logger;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
  19. private $l10n;
  20. private AddServerMiddleware $middleware;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject | SettingsController */
  22. private $controller;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  26. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  27. $this->controller = $this->getMockBuilder(SettingsController::class)
  28. ->disableOriginalConstructor()->getMock();
  29. $this->middleware = new AddServerMiddleware(
  30. 'AddServerMiddlewareTest',
  31. $this->l10n,
  32. $this->logger
  33. );
  34. }
  35. /**
  36. * @dataProvider dataTestAfterException
  37. *
  38. * @param \Exception $exception
  39. * @param string $hint
  40. */
  41. public function testAfterException($exception, $hint): void {
  42. $this->logger->expects($this->once())->method('error');
  43. $this->l10n->expects($this->any())->method('t')
  44. ->willReturnCallback(
  45. function (string $message): string {
  46. return $message;
  47. }
  48. );
  49. $result = $this->middleware->afterException($this->controller, 'method', $exception);
  50. $this->assertSame(Http::STATUS_BAD_REQUEST,
  51. $result->getStatus()
  52. );
  53. $data = $result->getData();
  54. $this->assertSame($hint,
  55. $data['message']
  56. );
  57. }
  58. public function dataTestAfterException() {
  59. return [
  60. [new HintException('message', 'hint'), 'hint'],
  61. [new \Exception('message'), 'message'],
  62. ];
  63. }
  64. }