3 namespace MediaWiki\Tests\Rest\PathTemplateMatcher
;
5 use MediaWiki\Rest\PathTemplateMatcher\PathConflict
;
6 use MediaWiki\Rest\PathTemplateMatcher\PathMatcher
;
9 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathMatcher
10 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathConflict
12 class PathMatcherTest
extends \MediaWikiUnitTestCase
{
13 private static $normalRoutes = [
21 public static function provideConflictingRoutes() {
23 [ '/a/b', 0, '/a/b' ],
24 [ '/a/{x}', 0, '/a/b' ],
25 [ '/{x}/c', 1, '/b/{x}' ],
26 [ '/b/a', 1, '/b/{x}' ],
27 [ '/b/{x}', 1, '/b/{x}' ],
28 [ '/{x}/{y}/d', 2, '/c/{x}/d' ],
32 public static function provideMatch() {
35 [ '/a/b', [ 'params' => [], 'userData' => 0 ] ],
37 [ '/b/1', [ 'params' => [ 'x' => '1' ], 'userData' => 1 ] ],
38 [ '/c/1/d', [ 'params' => [ 'x' => '1' ], 'userData' => 2 ] ],
39 [ '/c/1/e', [ 'params' => [ 'x' => '1' ], 'userData' => 3 ] ],
40 [ '/c/000/e', [ 'params' => [ 'x' => '000' ], 'userData' => 3 ] ],
42 [ '/c//e', [ 'params' => [ 'x' => '' ], 'userData' => 3 ] ],
47 public function createNormalRouter() {
48 $pm = new PathMatcher
;
49 foreach ( self
::$normalRoutes as $i => $route ) {
50 $pm->add( $route, $i );
55 /** @dataProvider provideConflictingRoutes */
56 public function testAddConflict( $attempt, $expectedUserData, $expectedTemplate ) {
57 $pm = $this->createNormalRouter();
58 $actualTemplate = null;
59 $actualUserData = null;
61 $pm->add( $attempt, 'conflict' );
62 } catch ( PathConflict
$pc ) {
63 $actualTemplate = $pc->existingTemplate
;
64 $actualUserData = $pc->existingUserData
;
66 $this->assertSame( $expectedUserData, $actualUserData );
67 $this->assertSame( $expectedTemplate, $actualTemplate );
70 /** @dataProvider provideMatch */
71 public function testMatch( $path, $expectedResult ) {
72 $pm = $this->createNormalRouter();
73 $result = $pm->match( $path );
74 $this->assertSame( $expectedResult, $result );