Merge "Hard deprecrate Language::viewPrevNext()"
[lhc/web/wiklou.git] / tests / phpunit / includes / Rest / PathTemplateMatcher / PathMatcherTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Rest\PathTemplateMatcher;
4
5 use MediaWiki\Rest\PathTemplateMatcher\PathConflict;
6 use MediaWiki\Rest\PathTemplateMatcher\PathMatcher;
7 use MediaWikiTestCase;
8
9 /**
10 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathMatcher
11 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathConflict
12 */
13 class PathMatcherTest extends MediaWikiTestCase {
14 private static $normalRoutes = [
15 '/a/b',
16 '/b/{x}',
17 '/c/{x}/d',
18 '/c/{x}/e',
19 '/c/{x}/{y}/d',
20 ];
21
22 public static function provideConflictingRoutes() {
23 return [
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' ],
30 ];
31 }
32
33 public static function provideMatch() {
34 return [
35 [ '', false ],
36 [ '/a/b', [ 'params' => [], 'userData' => 0 ] ],
37 [ '/b', false ],
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 ] ],
42 [ '/c/1/f', false ],
43 [ '/c//e', [ 'params' => [ 'x' => '' ], 'userData' => 3 ] ],
44 [ '/c///e', false ],
45 ];
46 }
47
48 public function createNormalRouter() {
49 $pm = new PathMatcher;
50 foreach ( self::$normalRoutes as $i => $route ) {
51 $pm->add( $route, $i );
52 }
53 return $pm;
54 }
55
56 /** @dataProvider provideConflictingRoutes */
57 public function testAddConflict( $attempt, $expectedUserData, $expectedTemplate ) {
58 $pm = $this->createNormalRouter();
59 $actualTemplate = null;
60 $actualUserData = null;
61 try {
62 $pm->add( $attempt, 'conflict' );
63 } catch ( PathConflict $pc ) {
64 $actualTemplate = $pc->existingTemplate;
65 $actualUserData = $pc->existingUserData;
66 }
67 $this->assertSame( $expectedUserData, $actualUserData );
68 $this->assertSame( $expectedTemplate, $actualTemplate );
69 }
70
71 /** @dataProvider provideMatch */
72 public function testMatch( $path, $expectedResult ) {
73 $pm = $this->createNormalRouter();
74 $result = $pm->match( $path );
75 $this->assertSame( $expectedResult, $result );
76 }
77 }