BrowserErrorPagePluginTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Tests\unit\DAV;
  25. use OCA\DAV\Files\BrowserErrorPagePlugin;
  26. use Sabre\DAV\Exception\NotFound;
  27. use Sabre\HTTP\Response;
  28. class BrowserErrorPagePluginTest extends \Test\TestCase {
  29. /**
  30. * @dataProvider providesExceptions
  31. * @param $expectedCode
  32. * @param $exception
  33. */
  34. public function test($expectedCode, $exception): void {
  35. /** @var BrowserErrorPagePlugin | \PHPUnit\Framework\MockObject\MockObject $plugin */
  36. $plugin = $this->getMockBuilder(BrowserErrorPagePlugin::class)->setMethods(['sendResponse', 'generateBody'])->getMock();
  37. $plugin->expects($this->once())->method('generateBody')->willReturn(':boom:');
  38. $plugin->expects($this->once())->method('sendResponse');
  39. /** @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject $server */
  40. $server = $this->getMockBuilder('Sabre\DAV\Server')->disableOriginalConstructor()->getMock();
  41. $server->expects($this->once())->method('on');
  42. $httpResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
  43. $httpResponse->expects($this->once())->method('addHeaders');
  44. $httpResponse->expects($this->once())->method('setStatus')->with($expectedCode);
  45. $httpResponse->expects($this->once())->method('setBody')->with(':boom:');
  46. $server->httpResponse = $httpResponse;
  47. $plugin->initialize($server);
  48. $plugin->logException($exception);
  49. }
  50. public function providesExceptions() {
  51. return [
  52. [ 404, new NotFound()],
  53. [ 500, new \RuntimeException()],
  54. ];
  55. }
  56. }