Add structure tests for HookRunner

Change-Id: Ic3f7106cf1ca19d4f965092b668b5d6d11e6aae4
This commit is contained in:
Petr Pchelko 2020-11-02 16:44:19 -07:00
parent 229bb3f2ab
commit 51957175b2

View file

@ -7,6 +7,7 @@ use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWikiUnitTestCase;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
/**
@ -17,13 +18,56 @@ use ReflectionParameter;
*/
class HookRunnerTest extends MediaWikiUnitTestCase {
public function provideHookRunners() {
yield ApiHookRunner::class => [ ApiHookRunner::class ];
yield HookRunner::class => [ HookRunner::class ];
}
/**
* @dataProvider provideHookRunners
* @param string $hookRunnerClass
*/
public function testAllMethodsInheritedFromInterface( string $hookRunnerClass ) {
$hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
$hookMethods = $hookRunnerReflectionClass->getMethods();
$hookInterfaces = $hookRunnerReflectionClass->getInterfaces();
foreach ( $hookMethods as $method ) {
if ( $method->isConstructor() ) {
continue;
}
$interfacesWithMethod = array_filter(
$hookInterfaces,
function ( ReflectionClass $interface ) use ( $method ) {
return $interface->hasMethod( $method->getName() );
}
);
$this->assertCount( 1, $interfacesWithMethod,
"Exactly one hook interface must have method {$method->getName()}" );
}
}
/**
* @dataProvider provideHookRunners
* @param string $hookRunnerClass
*/
public function testHookInterfacesHaveUniqueMethods( string $hookRunnerClass ) {
$hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
$hookInterfaces = $hookRunnerReflectionClass->getInterfaces();
$hookMethods = [];
foreach ( $hookInterfaces as $interface ) {
$this->assertCount( 1, $interface->getMethods(),
'Hook interface should have one method' );
array_push( $hookMethods, $interface->getMethods()[0]->getName() );
}
$this->assertArrayEquals( $hookMethods, array_unique( $hookMethods ) );
}
public function provideHookMethods() {
$hookRunnerClasses = [ ApiHookRunner::class, HookRunner::class ];
foreach ( $hookRunnerClasses as $hookRunnerClass ) {
foreach ( $this->provideHookRunners() as $name => [ $hookRunnerClass ] ) {
$hookRunnerReflectionClass = new ReflectionClass( $hookRunnerClass );
foreach ( $hookRunnerReflectionClass->getInterfaces() as $hookInterface ) {
yield $hookRunnerClass . ':' . $hookInterface->getName()
=> [ $hookRunnerClass, $hookInterface ];
yield $name . ':' . $hookInterface->getName()
=> [ $hookRunnerClass, $hookInterface->getMethods()[0] ];
}
}
}
@ -31,15 +75,12 @@ class HookRunnerTest extends MediaWikiUnitTestCase {
/**
* @dataProvider provideHookMethods
* @param string $hookRunnerClass
* @param ReflectionClass $hookInterface
* @param ReflectionMethod $hookMethod
*/
public function testAllArgumentsArePassed(
string $hookRunnerClass,
ReflectionClass $hookInterface
ReflectionMethod $hookMethod
) {
$this->assertSame( 1, count( $hookInterface->getMethods() ),
'Hook interface should have one method' );
$hookMethod = $hookInterface->getMethods()[0];
$params = [];
foreach ( $hookMethod->getParameters() as $param ) {
$bogusValue = $this->getMockedParamValue( $param );