Debian Codegraph @

tests/phpunit/unit/includes/Rest/PathTemplateMatcher/PathMatcherTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\PathTemplateMatcher;
4
5 use MediaWiki\Rest\PathTemplateMatcher\PathConflict;
6 use MediaWiki\Rest\PathTemplateMatcher\PathMatcher;
7 use MediaWiki\Rest\PathTemplateMatcher\PathSegmentException;
8 use MediaWikiUnitTestCase;
9
10 /**
11 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathMatcher
12 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathConflict
13 * @covers \MediaWiki\Rest\PathTemplateMatcher\PathSegmentException
14 */
15 class PathMatcherTest extends MediaWikiUnitTestCase {
16 private const NORMAL_ROUTES = [
17 '/a/b',
18 '/b/{x}',
19 '/c/{x}/d',
20 '/c/{x}/e',
21 '/c/{x}/{y}/d',
22 '/d/',
23 '/d/{x}',
24 '/',
25 '/{x}'
26 ];
27
28 public static function provideErrorRoutes() {
29 return [
30 [ '/d//e' ]
31 ];
32 }
33
34 public static function provideConflictingRoutes() {
35 return [
36 [ '/a/b', 0, '/a/b' ],
37 [ '/a/{x}', 0, '/a/b' ],
38 [ '/{x}/c', 1, '/b/{x}' ],
39 [ '/b/a', 1, '/b/{x}' ],
40 [ '/b/{x}', 1, '/b/{x}' ],
41 [ '/{x}/{y}/d', 2, '/c/{x}/d' ]
42 ];
43 }
44
45 public static function provideMatch() {
46 return [
47 [ '', false ],
48 [ '/a/', false ],
49 [ '/a/b', [ 'params' => [], 'userData' => 0 ] ],
50 [ '/b/1', [ 'params' => [ 'x' => '1' ], 'userData' => 1 ] ],
51 [ '/c/1', false ],
52 [ '/c/1/d', [ 'params' => [ 'x' => '1' ], 'userData' => 2 ] ],
53 [ '/c/1/e', [ 'params' => [ 'x' => '1' ], 'userData' => 3 ] ],
54 [ '/c/000/e', [ 'params' => [ 'x' => '000' ], 'userData' => 3 ] ],
55 [ '/c/1/f', false ],
56 [ '/c//e', [ 'params' => [ 'x' => '' ], 'userData' => 3 ] ],
57 [ '/c///e', false ],
58 [ '/d/', [ 'params' => [], 'userData' => 5 ] ],
59 [ '/d/1', [ 'params' => [ 'x' => '1' ], 'userData' => 6 ] ],
60 [ '/', [ 'params' => [], 'userData' => 7 ] ],
61 [ '/1', [ 'params' => [ 'x' => '1' ], 'userData' => 8 ] ],
62 [ '/1/', false ]
63 ];
64 }
65
66 public function createNormalRouter() {
67 $pm = new PathMatcher;
68 foreach ( self::NORMAL_ROUTES as $i => $route ) {
69 $pm->add( $route, $i );
70 }
71 return $pm;
72 }
73
74 /** @dataProvider provideErrorRoutes */
75 public function testAddError( $attempt ) {
76 $pm = $this->createNormalRouter();
77
78 $this->expectException( PathSegmentException::class );
79 $pm->add( $attempt, 'error' );
80 }
81
82 /** @dataProvider provideConflictingRoutes */
83 public function testAddConflict( $attempt, $expectedUserData, $expectedTemplate ) {
84 $pm = $this->createNormalRouter();
85 $actualTemplate = null;
86 $actualUserData = null;
87 try {
88 $pm->add( $attempt, 'conflict' );
89 } catch ( PathConflict $pc ) {
90 $actualTemplate = $pc->existingTemplate;
91 $actualUserData = $pc->existingUserData;
92 }
93 $this->assertSame( $expectedUserData, $actualUserData );
94 $this->assertSame( $expectedTemplate, $actualTemplate );
95 }
96
97 /** @dataProvider provideMatch */
98 public function testMatch( $path, $expectedResult ) {
99 $pm = $this->createNormalRouter();
100 $result = $pm->match( $path );
101 $this->assertSame( $expectedResult, $result );
102 }
103 }