3939 * @package Test\Route
4040 */
4141class RouterTest extends TestCase {
42- private Router $ router ;
43- protected function setUp (): void {
44- parent ::setUp ();
42+
43+ public function testGenerate (): void {
4544 /** @var LoggerInterface $logger */
4645 $ logger = $ this ->createMock (LoggerInterface::class);
4746 $ logger ->method ('info ' )
@@ -50,27 +49,30 @@ function (string $message, array $data) {
5049 $ this ->fail ('Unexpected info log: ' .(string )($ data ['exception ' ] ?? $ message ));
5150 }
5251 );
53- $ this -> router = new Router (
52+ $ router = new Router (
5453 $ logger ,
5554 $ this ->createMock (IRequest::class),
5655 $ this ->createMock (IConfig::class),
5756 $ this ->createMock (IEventLogger::class),
5857 $ this ->createMock (ContainerInterface::class),
5958 );
60- }
6159
62- public function testHeartbeat (): void {
63- $ this ->assertEquals ('/index.php/heartbeat ' , $ this ->router ->generate ('heartbeat ' ));
64- }
60+ /**
61+ * Testing the router is tricky because the routing files are loaded with include_once:
62+ * https://github.com/nextcloud/server/blob/b97c7dfe7f162680ffd9720ece12cdcd3ab656cd/lib/private/Route/Router.php#L461-L469
63+ *
64+ * It's not possible to create multiple router instances because a second instance won't find the routes
65+ * because the appinfo/routes.php files aren't included because they were included by the previous one.
66+ */
6567
66- public function testGenerateConsecutively (): void {
68+ $ this -> assertEquals ( ' /index.php/heartbeat ' , $ router -> generate ( ' heartbeat ' ));
6769
68- $ this ->assertEquals ('/index.php/apps/files/ ' , $ this -> router ->generate ('files.view.index ' ));
70+ $ this ->assertEquals ('/index.php/apps/files/ ' , $ router ->generate ('files.view.index ' ));
6971
7072 // the OCS route is the prefixed one for the AppFramework - see /ocs/v1.php for routing details
71- $ this ->assertEquals ('/index.php/ocsapp/apps/dav/api/v1/direct ' , $ this -> router ->generate ('ocs.dav.direct.getUrl ' ));
73+ $ this ->assertEquals ('/index.php/ocsapp/apps/dav/api/v1/direct ' , $ router ->generate ('ocs.dav.direct.getUrl ' ));
7274
7375 // test caching
74- $ this ->assertEquals ('/index.php/apps/files/ ' , $ this -> router ->generate ('files.view.index ' ));
76+ $ this ->assertEquals ('/index.php/apps/files/ ' , $ router ->generate ('files.view.index ' ));
7577 }
7678}
0 commit comments