Debian Codegraph @

tests/phpunit/integration/includes/ExtensionServicesTestBase.php

raw symbols SCIP
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 * Base class for testing ExtensionServices classes.
16 *
17 * Such classes are used in many extensions to access services more easily.
18 * They usually have one method like this for each service they register:
19 *
20 * ```php
21 * public static function getService1( ContainerInterface $services = null ): Service1 {
22 * return ( $services ?: MediaWikiServices::getInstance() )
23 * ->get( 'ExtensionName.Service1' );
24 * }
25 * ```
26 *
27 * To test an ExtensionServices class,
28 * create a subclass of this test base class and specify $className and $serviceNamePrefix.
29 *
30 * @license GPL-2.0-or-later
31 */
32 abstract class ExtensionServicesTestBase extends MediaWikiIntegrationTestCase {
33
34 /**
35 * @var string The name of the ExtensionServices class.
36 * (A fully qualified name, usually specified via ::class syntax.)
37 */
38 protected string $className;
39
40 /**
41 * @var string The prefix of the services in the service wiring.
42 * Usually something like 'ExtensionName.'.
43 * @see ExtensionJsonTestBase::$serviceNamePrefix
44 */
45 protected string $serviceNamePrefix;
46
47 /**
48 * @var string[] An optional list of service names that,
49 * despite starting with the {@link self::$serviceNamePrefix},
50 * have no corresponding getter method on the ExtensionServices class.
51 * This can be used to temporarily support the old name of a renamed service
52 * for backwards compatibility with other extensions.
53 */
54 protected array $serviceNamesWithoutMethods = [];
55
56 /** @dataProvider provideMethods */
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 /** @dataProvider provideMethods */
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 /** @dataProvider provideMethods */
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 // (in PHP 8.0, account for $type being a ReflectionUnionType here)
112 $this->assertInstanceOf( ReflectionNamedType::class, $type );
113 /** @var ReflectionNamedType $type */
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 ); // should not throw
152 }
153 }
154
155 $this->assertTrue( true, 'test did not throw' );
156 }
157
158 }