| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
abstract class HookRunnerTestBase extends MediaWikiUnitTestCase { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 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 |
|
| 167 |
}; |
| 168 |
} |
| 169 |
return $this->createNoOpMock( $paramName ); |
| 170 |
} |
| 171 |
} |