| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Tests; |
| 6 |
|
| 7 |
use MediaWikiIntegrationTestCase; |
| 8 |
use Psr\Container\ContainerInterface; |
| 9 |
use ReflectionClass; |
| 10 |
use ReflectionMethod; |
| 11 |
use ReflectionNamedType; |
| 12 |
use ReflectionType; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
abstract class ExtensionServicesTestBase extends MediaWikiIntegrationTestCase { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected string $className; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected string $serviceNamePrefix; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected array $serviceNamesWithoutMethods = []; |
| 55 |
|
| 56 |
|
| 57 |
public function testMethodSignature( ReflectionMethod $method ): void { |
| 58 |
$this->assertTrue( $method->isPublic(), |
| 59 |
'service accessor must be public' ); |
| 60 |
$this->assertTrue( $method->isStatic(), |
| 61 |
'service accessor must be static' ); |
| 62 |
$this->assertStringStartsWith( 'get', $method->getName(), |
| 63 |
'service accessor must be a getter' ); |
| 64 |
$this->assertTrue( $method->hasReturnType(), |
| 65 |
'service accessor must declare return type' ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public function testMethodWithDefaultServiceContainer( ReflectionMethod $method ): void { |
| 70 |
$methodName = $method->getName(); |
| 71 |
$serviceName = $this->serviceNamePrefix . substr( $methodName, strlen( 'get' ) ); |
| 72 |
$expectedService = $this->createValue( $method->getReturnType() ); |
| 73 |
$this->setService( $serviceName, $expectedService ); |
| 74 |
|
| 75 |
$actualService = $this->className::$methodName(); |
| 76 |
|
| 77 |
$this->assertSame( $expectedService, $actualService, |
| 78 |
'should return service from MediaWikiServices' ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public function testMethodWithCustomServiceContainer( ReflectionMethod $method ): void { |
| 83 |
$methodName = $method->getName(); |
| 84 |
$serviceName = $this->serviceNamePrefix . substr( $methodName, strlen( 'get' ) ); |
| 85 |
$expectedService = $this->createValue( $method->getReturnType() ); |
| 86 |
$services = $this->createMock( ContainerInterface::class ); |
| 87 |
$services->expects( $this->once() ) |
| 88 |
->method( 'get' ) |
| 89 |
->with( $serviceName ) |
| 90 |
->willReturn( $expectedService ); |
| 91 |
|
| 92 |
$actualService = $this->className::$methodName( $services ); |
| 93 |
|
| 94 |
$this->assertSame( $expectedService, $actualService, |
| 95 |
'should return service from injected container' ); |
| 96 |
} |
| 97 |
|
| 98 |
public function provideMethods(): iterable { |
| 99 |
$reflectionClass = new ReflectionClass( $this->className ); |
| 100 |
$methods = $reflectionClass->getMethods(); |
| 101 |
|
| 102 |
foreach ( $methods as $method ) { |
| 103 |
if ( $method->isConstructor() ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
yield $method->getName() => [ $method ]; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private function createValue( ReflectionType $type ) { |
| 111 |
|
| 112 |
$this->assertInstanceOf( ReflectionNamedType::class, $type ); |
| 113 |
|
| 114 |
if ( $type->allowsNull() ) { |
| 115 |
return null; |
| 116 |
} |
| 117 |
if ( $type->isBuiltin() ) { |
| 118 |
switch ( $type->getName() ) { |
| 119 |
case 'bool': |
| 120 |
return true; |
| 121 |
case 'int': |
| 122 |
return 0; |
| 123 |
case 'float': |
| 124 |
return 0.0; |
| 125 |
case 'string': |
| 126 |
return ''; |
| 127 |
case 'array': |
| 128 |
case 'iterable': |
| 129 |
return []; |
| 130 |
case 'callable': |
| 131 |
return 'is_null'; |
| 132 |
default: |
| 133 |
$this->fail( "unknown builtin type {$type->getName()}" ); |
| 134 |
} |
| 135 |
} |
| 136 |
return $this->createMock( $type->getName() ); |
| 137 |
} |
| 138 |
|
| 139 |
public function testMethodsExist(): void { |
| 140 |
if ( $this->serviceNamePrefix === '' ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$reflectionClass = new ReflectionClass( $this->className ); |
| 145 |
foreach ( $this->getServiceContainer()->getServiceNames() as $serviceName ) { |
| 146 |
if ( in_array( $serviceName, $this->serviceNamesWithoutMethods, true ) ) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
if ( str_starts_with( $serviceName, $this->serviceNamePrefix ) ) { |
| 150 |
$serviceNameSuffix = substr( $serviceName, strlen( $this->serviceNamePrefix ) ); |
| 151 |
$_ = $reflectionClass->getMethod( 'get' . $serviceNameSuffix ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$this->assertTrue( true, 'test did not throw' ); |
| 156 |
} |
| 157 |
|
| 158 |
} |