1
0

AddServerMiddlewareTest.php 2.6 KB

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