3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use FastRoute\Dispatcher
;
8 use PhpMyAdmin\Controllers\HomeController
;
9 use PhpMyAdmin\Routing
;
18 * @covers \PhpMyAdmin\Routing
20 class RoutingTest
extends AbstractTestCase
23 * Test for Routing::getDispatcher
25 public function testGetDispatcher(): void
27 $expected = [Dispatcher
::FOUND
, HomeController
::class, []];
28 $cacheFilename = CACHE_DIR
. 'routes.cache.php';
29 $validCacheFilename = TEST_PATH
. 'test/test_data/routes/routes-valid.cache.txt';
30 $invalidCacheFilename = TEST_PATH
. 'test/test_data/routes/routes-invalid.cache.txt';
31 $GLOBALS['cfg']['environment'] = null;
33 $this->assertDirectoryIsWritable(CACHE_DIR
);
36 $this->assertTrue(copy($validCacheFilename, $cacheFilename));
37 $dispatcher = Routing
::getDispatcher();
38 $this->assertInstanceOf(Dispatcher
::class, $dispatcher);
39 $this->assertSame($expected, $dispatcher->dispatch('GET', '/'));
40 $this->assertFileEquals($validCacheFilename, $cacheFilename);
42 // Invalid cache file.
43 $this->assertTrue(copy($invalidCacheFilename, $cacheFilename));
44 $dispatcher = Routing
::getDispatcher();
45 $this->assertInstanceOf(Dispatcher
::class, $dispatcher);
46 $this->assertSame($expected, $dispatcher->dispatch('GET', '/'));
47 $this->assertFileNotEquals($invalidCacheFilename, $cacheFilename);
49 // Create new cache file.
50 $this->assertTrue(unlink($cacheFilename));
51 $this->assertFileDoesNotExist($cacheFilename);
53 $dispatcher = Routing
::getDispatcher();
54 $this->assertInstanceOf(Dispatcher
::class, $dispatcher);
55 $this->assertSame($expected, $dispatcher->dispatch('GET', '/'));
56 $this->assertFileExists($cacheFilename);
58 // Without a cache file.
59 $GLOBALS['cfg']['environment'] = 'development';
60 $dispatcher = Routing
::getDispatcher();
61 $this->assertInstanceOf(Dispatcher
::class, $dispatcher);
62 $this->assertSame($expected, $dispatcher->dispatch('GET', '/'));
66 * Test for Routing::getCurrentRoute
68 public function testGetCurrentRouteNoParams(): void
70 $this->assertSame('/', Routing
::getCurrentRoute());
74 * Test for Routing::getCurrentRoute
76 public function testGetCurrentRouteGet(): void
78 $_GET['route'] = '/test';
79 $this->assertSame('/test', Routing
::getCurrentRoute());
83 * Test for Routing::getCurrentRoute
85 public function testGetCurrentRoutePost(): void
87 unset($_GET['route']);
88 $_POST['route'] = '/testpost';
89 $this->assertSame('/testpost', Routing
::getCurrentRoute());
93 * Test for Routing::getCurrentRoute
95 public function testGetCurrentRouteGetIsOverPost(): void
97 $_GET['route'] = '/testget';
98 $_POST['route'] = '/testpost';
99 $this->assertSame('/testget', Routing
::getCurrentRoute());
103 * Test for Routing::getCurrentRoute
105 public function testGetCurrentRouteRedirectDbStructure(): void
107 unset($_POST['route']);
108 unset($_GET['route']);
109 $_GET['db'] = 'testDB';
110 $this->assertSame('/database/structure', Routing
::getCurrentRoute());
114 * Test for Routing::getCurrentRoute
116 public function testGetCurrentRouteRedirectSql(): void
118 $_GET['db'] = 'testDB';
119 $_GET['table'] = 'tableTest';
120 $this->assertSame('/sql', Routing
::getCurrentRoute());