ErrorPagePluginTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\DAV\Tests\unit\DAV;
  8. use OCA\DAV\Files\ErrorPagePlugin;
  9. use Sabre\DAV\Exception\NotFound;
  10. use Sabre\HTTP\Response;
  11. class ErrorPagePluginTest extends \Test\TestCase {
  12. /**
  13. * @dataProvider providesExceptions
  14. * @param $expectedCode
  15. * @param $exception
  16. */
  17. public function test($expectedCode, $exception): void {
  18. /** @var ErrorPagePlugin | \PHPUnit\Framework\MockObject\MockObject $plugin */
  19. $plugin = $this->getMockBuilder(ErrorPagePlugin::class)->disableOriginalConstructor()->setMethods(['sendResponse', 'generateBody'])->getMock();
  20. $plugin->expects($this->once())->method('generateBody')->willReturn(':boom:');
  21. $plugin->expects($this->once())->method('sendResponse');
  22. /** @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject $server */
  23. $server = $this->getMockBuilder('Sabre\DAV\Server')->disableOriginalConstructor()->getMock();
  24. $server->expects($this->once())->method('on');
  25. $httpResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
  26. $httpResponse->expects($this->once())->method('addHeaders');
  27. $httpResponse->expects($this->once())->method('setStatus')->with($expectedCode);
  28. $httpResponse->expects($this->once())->method('setBody')->with(':boom:');
  29. $server->httpResponse = $httpResponse;
  30. $plugin->initialize($server);
  31. $plugin->logException($exception);
  32. }
  33. public function providesExceptions() {
  34. return [
  35. [ 404, new NotFound()],
  36. [ 500, new \RuntimeException()],
  37. ];
  38. }
  39. }