Debian Codegraph @

tests/phpunit/unit/includes/HookContainer/HookRunnerTestBase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\HookContainer;
4
5 use Generator;
6 use MediaWiki\HookContainer\HookContainer;
7 use MediaWikiUnitTestCase;
8 use ReflectionClass;
9 use ReflectionMethod;
10 use ReflectionParameter;
11
12 /**
13 * Tests that all arguments passed into HookRunner are passed along to HookContainer.
14 * @stable to extend
15 * @since 1.36
16 */
17 abstract class HookRunnerTestBase extends MediaWikiUnitTestCase {
18 /**
19 * @return Generator|array
20 */
21 // abstract public static function provideHookRunners();
22
23 /**
24 * Temporary override to make provideHookRunners static.
25 * See T332865.
26 *
27 * @return Generator|array
28 */
29 final public static function provideHookRunnersStatically() {
30 $reflectionMethod = new ReflectionMethod( static::class, 'provideHookRunners' );
31 if ( $reflectionMethod->isStatic() ) {
32 return $reflectionMethod->invoke( null );
33 }
34
35 trigger_error(
36 'overriding provideHookRunners as an instance method is deprecated. (' .
37 $reflectionMethod->getFileName() . ':' . $reflectionMethod->getEndLine() . ')',
38 E_USER_DEPRECATED
39 );
40
41 return $reflectionMethod->invoke( new static() );
42 }
43
44 /**
45 * @dataProvider provideHookRunnersStatically
46 */
47 public function testAllMethodsInheritedFromInterface( string $hookRunnerClass ) {
48 $hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
49 $hookMethods = $hookRunnerReflectionClass->getMethods();
50 $hookInterfaces = $hookRunnerReflectionClass->getInterfaces();
51 foreach ( $hookMethods as $method ) {
52 if ( $method->isConstructor() ) {
53 continue;
54 }
55 $interfacesWithMethod = array_filter(
56 $hookInterfaces,
57 static function ( ReflectionClass $interface ) use ( $method ) {
58 return $interface->hasMethod( $method->getName() );
59 }
60 );
61 $this->assertCount( 1, $interfacesWithMethod,
62 'Exactly one hook interface must have method ' . $method->getName() );
63 }
64 }
65
66 /**
67 * @dataProvider provideHookRunnersStatically
68 */
69 public function testHookInterfacesConvention( string $hookRunnerClass ) {
70 $hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
71 $hookInterfaces = $hookRunnerReflectionClass->getInterfaces();
72 $hookMethods = [];
73 foreach ( $hookInterfaces as $interface ) {
74 $name = $interface->getName();
75
76 $this->assertStringEndsWith( 'Hook', $name,
77 "Interface name '$name' must have the suffix 'Hook'." );
78
79 $methods = $interface->getMethods();
80 $this->assertCount( 1, $methods,
81 'Hook interface should have one method' );
82
83 $method = $methods[0];
84 $methodName = $method->getName();
85 $this->assertStringStartsWith( 'on', $methodName,
86 "Interface method '$methodName' must have prefix 'on'." );
87 $this->assertTrue( $method->isPublic(), "Interface method '$methodName' should public" );
88 $this->assertFalse( $method->isStatic(), "Interface method '$methodName' should not static." );
89
90 $hookMethods[] = $methodName;
91 }
92 $this->assertArrayEquals( $hookMethods, array_unique( $hookMethods ) );
93 }
94
95 public static function provideHookMethods() {
96 foreach ( self::provideHookRunnersStatically() as $name => [ $hookRunnerClass ] ) {
97 $hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
98 foreach ( $hookRunnerReflectionClass->getInterfaces() as $hookInterface ) {
99 yield $name . ':' . $hookInterface->getName()
100 => [ $hookRunnerClass, $hookInterface->getMethods()[0] ];
101 }
102 }
103 }
104
105 /**
106 * @dataProvider provideHookMethods
107 */
108 public function testHookContainerArguments(
109 string $hookRunnerClass,
110 ReflectionMethod $hookMethod
111 ) {
112 $params = [];
113 foreach ( $hookMethod->getParameters() as $param ) {
114 $bogusValue = $this->getMockedParamValue( $param );
115 if ( $param->isPassedByReference() ) {
116 $params[] = &$bogusValue;
117 unset( $bogusValue );
118 } else {
119 $params[] = $bogusValue;
120 }
121 }
122 $hookMethodName = $hookMethod->getName();
123 $mockContainer = $this->createNoOpMock( HookContainer::class, [ 'run' ] );
124 $mockContainer
125 ->expects( $this->once() )
126 ->method( 'run' )
127 ->willReturnCallback( function ( string $hookName, array $hookCallParams ) use ( $hookMethodName, $params ) {
128 // HookContainer builds the method from the hook name with some normalisation,
129 // so the passed hook name and the method must be equal
130 // This is not a function in HookContainer as hooks are hot path
131 // and just avoid the extra call for performance
132 $expectedFuncName = 'on' . strtr( ucfirst( $hookName ), ':-', '__' );
133 $this->assertSame( $expectedFuncName, $hookMethodName,
134 'Interface function must named "on<hook name>" with : or - replaced by _' );
135 $this->assertSame( $params, $hookCallParams );
136 return true;
137 } );
138 $hookRunner = new $hookRunnerClass( $mockContainer );
139 $hookRunner->$hookMethodName( ...$params );
140 }
141
142 protected function getMockedParamValue( ReflectionParameter $param ) {
143 $paramType = $param->getType();
144 if ( !$paramType ) {
145 // Return a string for all the untyped parameters, good enough for our purposes.
146 return $param->getName();
147 }
148 $paramName = $paramType->getName();
149 if ( $paramName === 'string' ) {
150 return $param->getName();
151 }
152 if ( $paramName === 'array' ) {
153 return [];
154 }
155 if ( $paramName === 'bool' ) {
156 return false;
157 }
158 if ( $paramName === 'int' ) {
159 return 42;
160 }
161 if ( $paramName === 'float' ) {
162 return 42.0;
163 }
164 if ( $paramName === 'callable' ) {
165 return static function () {
166 // No-op
167 };
168 }
169 return $this->createNoOpMock( $paramName );
170 }
171 }