1
0

RouterTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Route;
  8. use OC\Route\Router;
  9. use OCP\App\IAppManager;
  10. use OCP\Diagnostics\IEventLogger;
  11. use OCP\IConfig;
  12. use OCP\IRequest;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Container\ContainerInterface;
  15. use Psr\Log\LoggerInterface;
  16. use Test\TestCase;
  17. /**
  18. * Class RouterTest
  19. *
  20. * @group RoutingWeirdness
  21. *
  22. * @package Test\Route
  23. */
  24. class RouterTest extends TestCase {
  25. private Router $router;
  26. private IAppManager&MockObject $appManager;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. /** @var LoggerInterface $logger */
  30. $logger = $this->createMock(LoggerInterface::class);
  31. $logger->method('info')
  32. ->willReturnCallback(
  33. function (string $message, array $data) {
  34. $this->fail('Unexpected info log: ' . (string)($data['exception'] ?? $message));
  35. }
  36. );
  37. $this->appManager = $this->createMock(IAppManager::class);
  38. $this->router = new Router(
  39. $logger,
  40. $this->createMock(IRequest::class),
  41. $this->createMock(IConfig::class),
  42. $this->createMock(IEventLogger::class),
  43. $this->createMock(ContainerInterface::class),
  44. $this->appManager,
  45. );
  46. }
  47. public function testHeartbeat(): void {
  48. $this->assertEquals('/index.php/heartbeat', $this->router->generate('heartbeat'));
  49. }
  50. public function testGenerateConsecutively(): void {
  51. $this->appManager->expects(self::atLeastOnce())
  52. ->method('cleanAppId')
  53. ->willReturnArgument(0);
  54. $this->appManager->expects(self::atLeastOnce())
  55. ->method('getAppPath')
  56. ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid);
  57. $this->assertEquals('/index.php/apps/files/', $this->router->generate('files.view.index'));
  58. // the OCS route is the prefixed one for the AppFramework - see /ocs/v1.php for routing details
  59. $this->assertEquals('/index.php/ocsapp/apps/dav/api/v1/direct', $this->router->generate('ocs.dav.direct.getUrl'));
  60. // test caching
  61. $this->assertEquals('/index.php/apps/files/', $this->router->generate('files.view.index'));
  62. }
  63. }