3 namespace MediaWiki\Tests\Rest\PathTemplateMatcher
;
5 use MediaWiki\Rest\PathTemplateMatcher\PathConflict
;
6 use MediaWiki\Rest\PathTemplateMatcher\PathMatcher
;
10 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathMatcher
11 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathConflict
13 class PathMatcherTest
extends MediaWikiTestCase
{
14 private static $normalRoutes = [
22 public static function provideConflictingRoutes() {
24 [ '/a/b', 0, '/a/b' ],
25 [ '/a/{x}', 0, '/a/b' ],
26 [ '/{x}/c', 1, '/b/{x}' ],
27 [ '/b/a', 1, '/b/{x}' ],
28 [ '/b/{x}', 1, '/b/{x}' ],
29 [ '/{x}/{y}/d', 2, '/c/{x}/d' ],
33 public static function provideMatch() {
36 [ '/a/b', [ 'params' => [], 'userData' => 0 ] ],
38 [ '/b/1', [ 'params' => [ 'x' => '1' ], 'userData' => 1 ] ],
39 [ '/c/1/d', [ 'params' => [ 'x' => '1' ], 'userData' => 2 ] ],
40 [ '/c/1/e', [ 'params' => [ 'x' => '1' ], 'userData' => 3 ] ],
41 [ '/c/000/e', [ 'params' => [ 'x' => '000' ], 'userData' => 3 ] ],
43 [ '/c//e', [ 'params' => [ 'x' => '' ], 'userData' => 3 ] ],
48 public function createNormalRouter() {
49 $pm = new PathMatcher
;
50 foreach ( self
::$normalRoutes as $i => $route ) {
51 $pm->add( $route, $i );
56 /** @dataProvider provideConflictingRoutes */
57 public function testAddConflict( $attempt, $expectedUserData, $expectedTemplate ) {
58 $pm = $this->createNormalRouter();
59 $actualTemplate = null;
60 $actualUserData = null;
62 $pm->add( $attempt, 'conflict' );
63 } catch ( PathConflict
$pc ) {
64 $actualTemplate = $pc->existingTemplate
;
65 $actualUserData = $pc->existingUserData
;
67 $this->assertSame( $expectedUserData, $actualUserData );
68 $this->assertSame( $expectedTemplate, $actualTemplate );
71 /** @dataProvider provideMatch */
72 public function testMatch( $path, $expectedResult ) {
73 $pm = $this->createNormalRouter();
74 $result = $pm->match( $path );
75 $this->assertSame( $expectedResult, $result );